Implement support for new Package(<classLoader>)

This commit is contained in:
nboyd%atg.com 2002-04-21 01:50:43 +00:00
parent 1c4fee9b74
commit d3bdf13043
2 changed files with 57 additions and 4 deletions

View File

@ -68,10 +68,50 @@ public class NativeJavaPackage extends ScriptableObject {
"java.applet",
};
public static class TopLevelPackage extends NativeJavaPackage
implements Function
{
public TopLevelPackage() {
super("");
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
Object[] args)
throws JavaScriptException
{
return construct(cx, scope, args);
}
public Scriptable construct(Context cx, Scriptable scope, Object[] args)
throws JavaScriptException
{
ClassLoader loader = getClassLoaderFromArgs(args);
if (loader == null) {
Context.reportRuntimeError0("msg.not.classloader");
return null;
}
return new NativeJavaPackage("", loader);
}
private ClassLoader getClassLoaderFromArgs(Object[] args) {
if (args.length < 1) {
return null;
}
Object arg = args[0];
if (arg instanceof Wrapper) {
arg = ((Wrapper)arg).unwrap();
}
if (!(arg instanceof ClassLoader)) {
return null;
}
return (ClassLoader) arg;
}
}
public static Scriptable init(Scriptable scope)
throws PropertyException
{
NativeJavaPackage packages = new NativeJavaPackage("");
NativeJavaPackage packages = new NativeJavaPackage.TopLevelPackage();
packages.setPrototype(getObjectPrototype(scope));
packages.setParentScope(scope);
@ -119,7 +159,7 @@ public class NativeJavaPackage extends ScriptableObject {
String newPackage = packageName.length() == 0
? id
: packageName + "." + id;
pkg = new NativeJavaPackage(newPackage);
pkg = new NativeJavaPackage(newPackage, classLoader);
pkg.setParentScope(this);
pkg.setPrototype(this.prototype);
super.put(id, this, pkg);
@ -132,6 +172,11 @@ public class NativeJavaPackage extends ScriptableObject {
this.packageName = packageName;
}
public NativeJavaPackage(String packageName, ClassLoader classLoader) {
this.packageName = packageName;
this.classLoader = classLoader;
}
public String getClassName() {
return "JavaPackage";
}
@ -172,13 +217,16 @@ public class NativeJavaPackage extends ScriptableObject {
try {
if (ss != null && !ss.visibleToScripts(newPackage))
throw new ClassNotFoundException();
Class newClass = ScriptRuntime.loadClassName(newPackage);
Class newClass = classLoader != null
? classLoader.loadClass(newPackage)
: ScriptRuntime.loadClassName(newPackage);
newValue = NativeJavaClass.wrap(getTopLevelScope(this), newClass);
newValue.setParentScope(this);
newValue.setPrototype(this.prototype);
} catch (ClassNotFoundException ex) {
if (createPkg) {
NativeJavaPackage pkg = new NativeJavaPackage(newPackage);
NativeJavaPackage pkg = new NativeJavaPackage(newPackage,
classLoader);
pkg.setParentScope(this);
pkg.setPrototype(this.prototype);
newValue = pkg;
@ -233,4 +281,5 @@ public class NativeJavaPackage extends ScriptableObject {
}
private String packageName;
private ClassLoader classLoader;
}

View File

@ -155,6 +155,10 @@ msg.constructor.ambiguous =\
msg.conversion.not.allowed =\
Cannot convert {0} to {1}
# NativeJavaPackage
msg.not.classloader =\
Constructor for "Packages" expects argument of type java.lang.Classloader
# NativeRegExp
msg.bad.quant =\
Invalid quantifier {0}