Debian Codegraph @

rhino/src/org/mozilla/javascript/ImporterTopLevel.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 // API class
8
9 package org.mozilla.javascript;
10
11 /**
12 * Class ImporterTopLevel
13 *
14 * This class defines a ScriptableObject that can be instantiated
15 * as a top-level ("global") object to provide functionality similar
16 * to Java's "import" statement.
17 * <p>
18 * This class can be used to create a top-level scope using the following code:
19 * <pre>
20 * Scriptable scope = new ImporterTopLevel(cx);
21 * </pre>
22 * Then JavaScript code will have access to the following methods:
23 * <ul>
24 * <li>importClass - will "import" a class by making its unqualified name
25 * available as a property of the top-level scope
26 * <li>importPackage - will "import" all the classes of the package by
27 * searching for unqualified names as classes qualified
28 * by the given package.
29 * </ul>
30 * The following code from the shell illustrates this use:
31 * <pre>
32 * js> importClass(java.io.File)
33 * js> f = new File('help.txt')
34 * help.txt
35 * js> importPackage(java.util)
36 * js> v = new Vector()
37 * []
38 *
39 * @author Norris Boyd
40 */
41 public class ImporterTopLevel extends TopLevel {
42 static final long serialVersionUID = -9095380847465315412L;
43
44 private static final Object IMPORTER_TAG = "Importer";
45
46 public ImporterTopLevel() { }
47
48 public ImporterTopLevel(Context cx) {
49 this(cx, false);
50 }
51
52 public ImporterTopLevel(Context cx, boolean sealed)
53 {
54 initStandardObjects(cx, sealed);
55 }
56
57 @Override
58 public String getClassName()
59 {
60 return (topScopeFlag) ? "global" : "JavaImporter";
61 }
62
63 public static void init(Context cx, Scriptable scope, boolean sealed)
64 {
65 ImporterTopLevel obj = new ImporterTopLevel();
66 obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
67 }
68
69 public void initStandardObjects(Context cx, boolean sealed)
70 {
71 // Assume that Context.initStandardObjects initialize JavaImporter
72 // property lazily so the above init call is not yet called
73 cx.initStandardObjects(this, sealed);
74 topScopeFlag = true;
75 // If seal is true then exportAsJSClass(cx, seal) would seal
76 // this obj. Since this is scope as well, it would not allow
77 // to add variables.
78 IdFunctionObject ctor = exportAsJSClass(MAX_PROTOTYPE_ID, this, false);
79 if (sealed) {
80 ctor.sealObject();
81 }
82 // delete "constructor" defined by exportAsJSClass so "constructor"
83 // name would refer to Object.constructor
84 // and not to JavaImporter.prototype.constructor.
85 delete("constructor");
86 }
87
88 @Override
89 public boolean has(String name, Scriptable start) {
90 return super.has(name, start)
91 || getPackageProperty(name, start) != NOT_FOUND;
92 }
93
94 @Override
95 public Object get(String name, Scriptable start) {
96 Object result = super.get(name, start);
97 if (result != NOT_FOUND)
98 return result;
99 result = getPackageProperty(name, start);
100 return result;
101 }
102
103 private Object getPackageProperty(String name, Scriptable start) {
104 Object result = NOT_FOUND;
105 Object[] elements;
106 synchronized (importedPackages) {
107 elements = importedPackages.toArray();
108 }
109 for (int i=0; i < elements.length; i++) {
110 NativeJavaPackage p = (NativeJavaPackage) elements[i];
111 Object v = p.getPkgProperty(name, start, false);
112 if (v != null && !(v instanceof NativeJavaPackage)) {
113 if (result == NOT_FOUND) {
114 result = v;
115 } else {
116 throw Context.reportRuntimeError2(
117 "msg.ambig.import", result.toString(), v.toString());
118 }
119 }
120 }
121 return result;
122 }
123
124 /**
125 * @deprecated Kept only for compatibility.
126 */
127 @Deprecated
128 public void importPackage(Context cx, Scriptable thisObj, Object[] args,
129 Function funObj)
130 {
131 js_importPackage(args);
132 }
133
134 private Object js_construct(Scriptable scope, Object[] args)
135 {
136 ImporterTopLevel result = new ImporterTopLevel();
137 for (int i = 0; i != args.length; ++i) {
138 Object arg = args[i];
139 if (arg instanceof NativeJavaClass) {
140 result.importClass((NativeJavaClass)arg);
141 } else if (arg instanceof NativeJavaPackage) {
142 result.importPackage((NativeJavaPackage)arg);
143 } else {
144 throw Context.reportRuntimeError1(
145 "msg.not.class.not.pkg", Context.toString(arg));
146 }
147 }
148 // set explicitly prototype and scope
149 // as otherwise in top scope mode BaseFunction.construct
150 // would keep them set to null. It also allow to use
151 // JavaImporter without new and still get properly
152 // initialized object.
153 result.setParentScope(scope);
154 result.setPrototype(this);
155 return result;
156 }
157
158 private Object js_importClass(Object[] args)
159 {
160 for (int i = 0; i != args.length; i++) {
161 Object arg = args[i];
162 if (!(arg instanceof NativeJavaClass)) {
163 throw Context.reportRuntimeError1(
164 "msg.not.class", Context.toString(arg));
165 }
166 importClass((NativeJavaClass)arg);
167 }
168 return Undefined.instance;
169 }
170
171 private Object js_importPackage(Object[] args)
172 {
173 for (int i = 0; i != args.length; i++) {
174 Object arg = args[i];
175 if (!(arg instanceof NativeJavaPackage)) {
176 throw Context.reportRuntimeError1(
177 "msg.not.pkg", Context.toString(arg));
178 }
179 importPackage((NativeJavaPackage)arg);
180 }
181 return Undefined.instance;
182 }
183
184 private void importPackage(NativeJavaPackage pkg)
185 {
186 if(pkg == null) {
187 return;
188 }
189 synchronized (importedPackages) {
190 for (int j = 0; j != importedPackages.size(); j++) {
191 if (pkg.equals(importedPackages.get(j))) {
192 return;
193 }
194 }
195 importedPackages.add(pkg);
196 }
197 }
198
199 private void importClass(NativeJavaClass cl)
200 {
201 String s = cl.getClassObject().getName();
202 String n = s.substring(s.lastIndexOf('.')+1);
203 Object val = get(n, this);
204 if (val != NOT_FOUND && val != cl) {
205 throw Context.reportRuntimeError1("msg.prop.defined", n);
206 }
207 //defineProperty(n, cl, DONTENUM);
208 put(n, this, cl);
209 }
210
211 @Override
212 protected void initPrototypeId(int id)
213 {
214 String s;
215 int arity;
216 switch (id) {
217 case Id_constructor: arity=0; s="constructor"; break;
218 case Id_importClass: arity=1; s="importClass"; break;
219 case Id_importPackage: arity=1; s="importPackage"; break;
220 default: throw new IllegalArgumentException(String.valueOf(id));
221 }
222 initPrototypeMethod(IMPORTER_TAG, id, s, arity);
223 }
224
225 @Override
226 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
227 Scriptable thisObj, Object[] args)
228 {
229 if (!f.hasTag(IMPORTER_TAG)) {
230 return super.execIdCall(f, cx, scope, thisObj, args);
231 }
232 int id = f.methodId();
233 switch (id) {
234 case Id_constructor:
235 return js_construct(scope, args);
236
237 case Id_importClass:
238 return realThis(thisObj, f).js_importClass(args);
239
240 case Id_importPackage:
241 return realThis(thisObj, f).js_importPackage(args);
242 }
243 throw new IllegalArgumentException(String.valueOf(id));
244 }
245
246 private ImporterTopLevel realThis(Scriptable thisObj, IdFunctionObject f)
247 {
248 if (topScopeFlag) {
249 // when used as top scope importPackage and importClass are global
250 // function that ignore thisObj
251 return this;
252 }
253 if (!(thisObj instanceof ImporterTopLevel))
254 throw incompatibleCallError(f);
255 return (ImporterTopLevel)thisObj;
256 }
257
258 // #string_id_map#
259
260 @Override
261 protected int findPrototypeId(String s)
262 {
263 int id;
264 // #generated# Last update: 2007-05-09 08:15:24 EDT
265 L0: { id = 0; String X = null; int c;
266 int s_length = s.length();
267 if (s_length==11) {
268 c=s.charAt(0);
269 if (c=='c') { X="constructor";id=Id_constructor; }
270 else if (c=='i') { X="importClass";id=Id_importClass; }
271 }
272 else if (s_length==13) { X="importPackage";id=Id_importPackage; }
273 if (X!=null && X!=s && !X.equals(s)) id = 0;
274 break L0;
275 }
276 // #/generated#
277 return id;
278 }
279
280 private static final int
281 Id_constructor = 1,
282 Id_importClass = 2,
283 Id_importPackage = 3,
284 MAX_PROTOTYPE_ID = 3;
285
286 // #/string_id_map#
287
288 private ObjArray importedPackages = new ObjArray();
289 private boolean topScopeFlag;
290 }