Skip to content

Commit

Permalink
Add support for unmapping mapped class names
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 committed Jun 29, 2024
1 parent f61b5a4 commit 7c7c509
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
21 changes: 14 additions & 7 deletions src/main/java/org/mozilla/javascript/ImporterTopLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
public interface JavaObjectMappingProvider {
JavaObjectMappingProvider EMPTY = new Default();

String remapClassName(String className);
String mapClassName(String className);

String unmapClassName(String className);

void findExtraMethods(Class<?> clazz, Map<MethodSignature, RenameableMethod> map, boolean includeProtected, boolean includePrivate);

void findExtraFields(Class<?> clazz, List<RenameableField> list, boolean includeProtected, boolean includePrivate);

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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mozilla/javascript/NativeJavaPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7c7c509

Please sign in to comment.