Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implementing methods from superinterfaces of directly implemented interfaces of superclasses. #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/main/java/dev/latvian/mods/rhino/JavaAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.ArrayDeque;
import java.util.Map;

public final class JavaAdapter implements IdFunctionCall {
Expand Down Expand Up @@ -359,16 +360,29 @@ public static byte[] createAdapterCode(ObjToIntMap functionNames, String adapter
static Method[] getOverridableMethods(Class<?> clazz) {
ArrayList<Method> list = new ArrayList<>();
HashSet<String> skip = new HashSet<>();
ArrayDeque<Class<?>> interfaces = new ArrayDeque<>();
HashSet<Class<?>> visitedInterfaces = new HashSet<>();
// Check superclasses before interfaces so we always choose
// implemented methods over abstract ones, even if a subclass
// re-implements an interface already implemented in a superclass
// (e.g. java.util.ArrayList)
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
appendOverridableMethods(c, list, skip);
}
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
for (Class<?> intf : c.getInterfaces()) {
appendOverridableMethods(intf, list, skip);
interfaces.add(intf);
}
}
// Visit interfaces in depth first order.
while (!interfaces.isEmpty()) {
var intf = interfaces.remove();
if (visitedInterfaces.contains(intf)) {
continue;
}
visitedInterfaces.add(intf);
appendOverridableMethods(intf, list, skip);
var subIntf = intf.getInterfaces();
for (int j = subIntf.length -1; j >= 0; j--) {
interfaces.addFirst(subIntf[j]);
}
}
return list.toArray(new Method[0]);
Expand Down