diff --git a/src/main/java/org/mozilla/javascript/ImporterTopLevel.java b/src/main/java/org/mozilla/javascript/ImporterTopLevel.java index 28cf9df0..26faa758 100644 --- a/src/main/java/org/mozilla/javascript/ImporterTopLevel.java +++ b/src/main/java/org/mozilla/javascript/ImporterTopLevel.java @@ -128,12 +128,12 @@ public void importPackage(Context cx, Scriptable thisObj, Object[] args, js_importPackage(args); } - private Object js_construct(Scriptable scope, Object[] args) { + private Object js_construct(Context cx, Scriptable scope, Object[] args) { ImporterTopLevel result = new ImporterTopLevel(); for (int i = 0; i != args.length; ++i) { Object arg = args[i]; if (arg instanceof NativeJavaClass) { - result.importClass((NativeJavaClass) arg); + result.importClass(cx, (NativeJavaClass) arg); } else if (arg instanceof NativeJavaPackage) { result.importPackage((NativeJavaPackage) arg); } else { @@ -151,14 +151,14 @@ private Object js_construct(Scriptable scope, Object[] args) { return result; } - private Object js_importClass(Object[] args) { + private Object js_importClass(Context cx, Object[] args) { for (int i = 0; i != args.length; i++) { Object arg = args[i]; if (!(arg instanceof NativeJavaClass)) { throw Context.reportRuntimeError1( "msg.not.class", Context.toString(arg)); } - importClass((NativeJavaClass) arg); + importClass(cx, (NativeJavaClass) arg); } return Undefined.instance; } @@ -189,7 +189,7 @@ private void importPackage(NativeJavaPackage pkg) { } } - private void importClass(NativeJavaClass cl) { + private void importClass(Context cx, NativeJavaClass cl) { String s = cl.getClassObject().getName(); String n = s.substring(s.lastIndexOf('.') + 1); Object val = get(n, this); @@ -198,6 +198,13 @@ private void importClass(NativeJavaClass cl) { } //defineProperty(n, cl, DONTENUM); put(n, this, cl); + + // Need to check if this is a mapped name + String unmappedName = cx.getJavaObjectMappingProvider().unmapClassName(s); + if (unmappedName != null) { + n = unmappedName.substring(unmappedName.lastIndexOf('.') + 1); + put(n, this, cl); + } } @Override @@ -232,10 +239,10 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, int id = f.methodId(); switch (id) { case Id_constructor: - return js_construct(scope, args); + return js_construct(cx, scope, args); case Id_importClass: - return realThis(thisObj, f).js_importClass(args); + return realThis(thisObj, f).js_importClass(cx, args); case Id_importPackage: return realThis(thisObj, f).js_importPackage(args); diff --git a/src/main/java/org/mozilla/javascript/JavaObjectMappingProvider.java b/src/main/java/org/mozilla/javascript/JavaObjectMappingProvider.java index a0e8919f..868e1839 100644 --- a/src/main/java/org/mozilla/javascript/JavaObjectMappingProvider.java +++ b/src/main/java/org/mozilla/javascript/JavaObjectMappingProvider.java @@ -9,7 +9,9 @@ public interface JavaObjectMappingProvider { JavaObjectMappingProvider EMPTY = new Default(); - String remapClassName(String className); + String mapClassName(String className); + + String unmapClassName(String className); void findExtraMethods(Class clazz, Map map, boolean includeProtected, boolean includePrivate); @@ -17,8 +19,12 @@ public interface JavaObjectMappingProvider { class Default implements JavaObjectMappingProvider { @Override - public String remapClassName(String className) - { + public String mapClassName(String className) { + return null; + } + + @Override + public String unmapClassName(String className) { return null; } diff --git a/src/main/java/org/mozilla/javascript/NativeJavaPackage.java b/src/main/java/org/mozilla/javascript/NativeJavaPackage.java index 3f3f7601..6bc6060a 100644 --- a/src/main/java/org/mozilla/javascript/NativeJavaPackage.java +++ b/src/main/java/org/mozilla/javascript/NativeJavaPackage.java @@ -113,9 +113,9 @@ synchronized Object getPkgProperty(String name, Scriptable start, return null; } - String className = (packageName.length() == 0) ? name : packageName + '.' + name; + String className = packageName.isEmpty() ? name : packageName + '.' + name; Context cx = Context.getContext(); - String remappedClassName = cx.getJavaObjectMappingProvider().remapClassName(className); + String remappedClassName = cx.getJavaObjectMappingProvider().mapClassName(className); if (remappedClassName != null) { className = remappedClassName; }