Skip to content

Commit

Permalink
Merge pull request #52 from tomprince/superinterfaces
Browse files Browse the repository at this point in the history
Allow implementing methods from superinterfaces of directly implemented interfaces of superclasses.
  • Loading branch information
LatvianModder authored Feb 12, 2025
2 parents a146022 + 6eb33ba commit 54379ec
Showing 1 changed file with 17 additions and 3 deletions.
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 @@ -17,6 +17,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 @@ -358,16 +359,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

0 comments on commit 54379ec

Please sign in to comment.