| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class NativeJavaTopPackage |
| 23 |
extends NativeJavaPackage implements Function, IdFunctionCall |
| 24 |
{ |
| 25 |
static final long serialVersionUID = -1455787259477709999L; |
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 87 |
IdFunctionObject getClass = new IdFunctionObject(top, FTAG, Id_getClass, |
| 88 |
"getClass", 1, scope); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 100 |
|
| 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 |
|
| 131 |
|
| 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; |
| 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 |
|