Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeJavaPackage.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.io.IOException;
10 import java.io.ObjectInputStream;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 /**
15 * This class reflects Java packages into the JavaScript environment. We
16 * lazily reflect classes and subpackages, and use a caching/sharing
17 * system to ensure that members reflected into one JavaPackage appear
18 * in all other references to the same package (as with Packages.java.lang
19 * and java.lang).
20 *
21 * @author Mike Shaver
22 * @see NativeJavaArray
23 * @see NativeJavaObject
24 * @see NativeJavaClass
25 */
26
27 public class NativeJavaPackage extends ScriptableObject
28 {
29 static final long serialVersionUID = 7445054382212031523L;
30
31 NativeJavaPackage(boolean internalUsage, String packageName,
32 ClassLoader classLoader)
33 {
34 this.packageName = packageName;
35 this.classLoader = classLoader;
36 }
37
38 /**
39 * @deprecated NativeJavaPackage is an internal class, do not use
40 * it directly.
41 */
42 @Deprecated
43 public NativeJavaPackage(String packageName, ClassLoader classLoader) {
44 this(false, packageName, classLoader);
45 }
46
47 /**
48 * @deprecated NativeJavaPackage is an internal class, do not use
49 * it directly.
50 */
51 @Deprecated
52 public NativeJavaPackage(String packageName) {
53 this(false, packageName,
54 Context.getCurrentContext().getApplicationClassLoader());
55 }
56
57 @Override
58 public String getClassName() {
59 return "JavaPackage";
60 }
61
62 @Override
63 public boolean has(String id, Scriptable start) {
64 return true;
65 }
66
67 @Override
68 public boolean has(int index, Scriptable start) {
69 return false;
70 }
71
72 @Override
73 public void put(String id, Scriptable start, Object value) {
74 // Can't add properties to Java packages. Sorry.
75 }
76
77 @Override
78 public void put(int index, Scriptable start, Object value) {
79 throw Context.reportRuntimeError0("msg.pkg.int");
80 }
81
82 @Override
83 public Object get(String id, Scriptable start) {
84 return getPkgProperty(id, start, true);
85 }
86
87 @Override
88 public Object get(int index, Scriptable start) {
89 return NOT_FOUND;
90 }
91
92 // set up a name which is known to be a package so we don't
93 // need to look for a class by that name
94 NativeJavaPackage forcePackage(String name, Scriptable scope)
95 {
96 Object cached = super.get(name, this);
97 if (cached != null && cached instanceof NativeJavaPackage) {
98 return (NativeJavaPackage) cached;
99 } else {
100 String newPackage = packageName.length() == 0
101 ? name
102 : packageName + "." + name;
103 NativeJavaPackage pkg = new NativeJavaPackage(true, newPackage, classLoader);
104 ScriptRuntime.setObjectProtoAndParent(pkg, scope);
105 super.put(name, this, pkg);
106 return pkg;
107 }
108 }
109
110 synchronized Object getPkgProperty(String name, Scriptable start,
111 boolean createPkg)
112 {
113 Object cached = super.get(name, start);
114 if (cached != NOT_FOUND)
115 return cached;
116 if (negativeCache != null && negativeCache.contains(name)) {
117 // Performance optimization: see bug 421071
118 return null;
119 }
120
121 String className = (packageName.length() == 0)
122 ? name : packageName + '.' + name;
123 Context cx = Context.getContext();
124 ClassShutter shutter = cx.getClassShutter();
125 Scriptable newValue = null;
126 if (shutter == null || shutter.visibleToScripts(className)) {
127 Class<?> cl = null;
128 if (classLoader != null) {
129 cl = Kit.classOrNull(classLoader, className);
130 } else {
131 cl = Kit.classOrNull(className);
132 }
133 if (cl != null) {
134 WrapFactory wrapFactory = cx.getWrapFactory();
135 newValue = wrapFactory.wrapJavaClass(cx, getTopLevelScope(this), cl);
136 newValue.setPrototype(getPrototype());
137 }
138 }
139 if (newValue == null) {
140 if (createPkg) {
141 NativeJavaPackage pkg;
142 pkg = new NativeJavaPackage(true, className, classLoader);
143 ScriptRuntime.setObjectProtoAndParent(pkg, getParentScope());
144 newValue = pkg;
145 } else {
146 // add to negative cache
147 if (negativeCache == null)
148 negativeCache = new HashSet<String>();
149 negativeCache.add(name);
150 }
151 }
152 if (newValue != null) {
153 // Make it available for fast lookup and sharing of
154 // lazily-reflected constructors and static members.
155 super.put(name, start, newValue);
156 }
157 return newValue;
158 }
159
160 @Override
161 public Object getDefaultValue(Class<?> ignored) {
162 return toString();
163 }
164
165 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
166 in.defaultReadObject();
167 this.classLoader = Context.getCurrentContext().getApplicationClassLoader();
168 }
169
170 @Override
171 public String toString() {
172 return "[JavaPackage " + packageName + "]";
173 }
174
175 @Override
176 public boolean equals(Object obj) {
177 if(obj instanceof NativeJavaPackage) {
178 NativeJavaPackage njp = (NativeJavaPackage)obj;
179 return packageName.equals(njp.packageName) &&
180 classLoader == njp.classLoader;
181 }
182 return false;
183 }
184
185 @Override
186 public int hashCode() {
187 return packageName.hashCode() ^
188 (classLoader == null ? 0 : classLoader.hashCode());
189 }
190
191 private String packageName;
192 private transient ClassLoader classLoader;
193 private Set<String> negativeCache = null;
194 }