Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeJavaTopPackage.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 /**
10 * This class reflects Java packages into the JavaScript environment. We
11 * lazily reflect classes and subpackages, and use a caching/sharing
12 * system to ensure that members reflected into one JavaPackage appear
13 * in all other references to the same package (as with Packages.java.lang
14 * and java.lang).
15 *
16 * @author Mike Shaver
17 * @see NativeJavaArray
18 * @see NativeJavaObject
19 * @see NativeJavaClass
20 */
21
22 public class NativeJavaTopPackage
23 extends NativeJavaPackage implements Function, IdFunctionCall
24 {
25 static final long serialVersionUID = -1455787259477709999L;
26
27 // we know these are packages so we can skip the class check
28 // note that this is ok even if the package isn't present.
29 private static final String[][] commonPackages = {
30 {"java", "lang", "reflect"},
31 {"java", "io"},
32 {"java", "math"},
33 {"java", "net"},
34 {"java", "util", "zip"},
35 {"java", "text", "resources"},
36 {"java", "applet"},
37 {"javax", "swing"}
38 };
39
40 NativeJavaTopPackage(ClassLoader loader)
41 {
42 super(true, "", loader);
43 }
44
45 public Object call(Context cx, Scriptable scope, Scriptable thisObj,
46 Object[] args)
47 {
48 return construct(cx, scope, args);
49 }
50
51 public Scriptable construct(Context cx, Scriptable scope, Object[] args)
52 {
53 ClassLoader loader = null;
54 if (args.length != 0) {
55 Object arg = args[0];
56 if (arg instanceof Wrapper) {
57 arg = ((Wrapper)arg).unwrap();
58 }
59 if (arg instanceof ClassLoader) {
60 loader = (ClassLoader)arg;
61 }
62 }
63 if (loader == null) {
64 Context.reportRuntimeError0("msg.not.classloader");
65 return null;
66 }
67 NativeJavaPackage pkg = new NativeJavaPackage(true, "", loader);
68 ScriptRuntime.setObjectProtoAndParent(pkg, scope);
69 return pkg;
70 }
71
72 public static void init(Context cx, Scriptable scope, boolean sealed)
73 {
74 ClassLoader loader = cx.getApplicationClassLoader();
75 final NativeJavaTopPackage top = new NativeJavaTopPackage(loader);
76 top.setPrototype(getObjectPrototype(scope));
77 top.setParentScope(scope);
78
79 for (int i = 0; i != commonPackages.length; i++) {
80 NativeJavaPackage parent = top;
81 for (int j = 0; j != commonPackages[i].length; j++) {
82 parent = parent.forcePackage(commonPackages[i][j], scope);
83 }
84 }
85
86 // getClass implementation
87 IdFunctionObject getClass = new IdFunctionObject(top, FTAG, Id_getClass,
88 "getClass", 1, scope);
89
90 // We want to get a real alias, and not a distinct JavaPackage
91 // with the same packageName, so that we share classes and top
92 // that are underneath.
93 String[] topNames = ScriptRuntime.getTopPackageNames();
94 NativeJavaPackage[] topPackages = new NativeJavaPackage[topNames.length];
95 for (int i=0; i < topNames.length; i++) {
96 topPackages[i] = (NativeJavaPackage)top.get(topNames[i], top);
97 }
98
99 // It's safe to downcast here since initStandardObjects takes
100 // a ScriptableObject.
101 ScriptableObject global = (ScriptableObject) scope;
102
103 if (sealed) {
104 getClass.sealObject();
105 }
106 getClass.exportAsScopeProperty();
107 global.defineProperty("Packages", top, ScriptableObject.DONTENUM);
108 for (int i=0; i < topNames.length; i++) {
109 global.defineProperty(topNames[i], topPackages[i],
110 ScriptableObject.DONTENUM);
111 }
112 }
113
114 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
115 Scriptable thisObj, Object[] args)
116 {
117 if (f.hasTag(FTAG)) {
118 if (f.methodId() == Id_getClass) {
119 return js_getClass(cx, scope, args);
120 }
121 }
122 throw f.unknown();
123 }
124
125 private Scriptable js_getClass(Context cx, Scriptable scope, Object[] args)
126 {
127 if (args.length > 0 && args[0] instanceof Wrapper) {
128 Scriptable result = this;
129 Class<?> cl = ((Wrapper) args[0]).unwrap().getClass();
130 // Evaluate the class name by getting successive properties of
131 // the string to find the appropriate NativeJavaClass object
132 String name = cl.getName();
133 int offset = 0;
134 for (;;) {
135 int index = name.indexOf('.', offset);
136 String propName = index == -1
137 ? name.substring(offset)
138 : name.substring(offset, index);
139 Object prop = result.get(propName, result);
140 if (!(prop instanceof Scriptable))
141 break; // fall through to error
142 result = (Scriptable) prop;
143 if (index == -1)
144 return result;
145 offset = index+1;
146 }
147 }
148 throw Context.reportRuntimeError0("msg.not.java.obj");
149 }
150
151 private static final Object FTAG = "JavaTopPackage";
152 private static final int Id_getClass = 1;
153 }
154