Skip to content

Commit

Permalink
bug(*): fix method resolution for inner interfaces
Browse files Browse the repository at this point in the history
This fixes the method CtType#getAllExecutables and handles inner now
interfaces correctly as implicitly static.

Co-authored-by: I-Al-Istannen <i-al-istannen@users.noreply.github.com>
  • Loading branch information
MartinWitt and I-Al-Istannen committed Jun 27, 2021
1 parent 5f2e4f5 commit 86615d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/visitor/ClassTypingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ private CtTypeReference<?> getEnclosingType(CtTypeReference<?> typeRef) {
if (declType == null) {
return null;
}
if (type.isInterface()) {
// Case: we have a declaring type and the inner type is an interface. So the nested interface is implicitly static.
return null;
}
if (declType.isInterface()) {
//nested types of interfaces are static
return null;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/spoon/test/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
Expand All @@ -43,6 +44,7 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.createFactory;
Expand Down Expand Up @@ -218,4 +220,16 @@ public void test_addFormalCtTypeParameterAt_throwsOutOfBoundsException_whenPosit
assertThrows(IndexOutOfBoundsException.class,
() -> method.addFormalCtTypeParameterAt(1, typeParam));
}

@Test
public void testGetAllMethodsInnerClassExtended() {
// contract: implicit static nested interfaces are correct handled in getAllExecutables and dont throw an error.
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/extendsStaticInnerType");
CtModel model = launcher.buildModel();
CtType<?> type = model.getAllTypes().stream().filter(v -> v.getSimpleName().contains("BarBaz")).findAny().get();
assertDoesNotThrow(() -> type.getAllExecutables());
// on jdk8 there are 14 types on newer 13 because a method in Object.java was removed
assertTrue(13 == type.getAllExecutables().size() || 14 == type.getAllExecutables().size());
}
}
10 changes: 10 additions & 0 deletions src/test/resources/extendsStaticInnerType/BarBaz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package extendsStaticInnerType;

public class BarBaz implements FooBar.Crashy {

@Override
public String foo() {
return "bar!";
}

}
9 changes: 9 additions & 0 deletions src/test/resources/extendsStaticInnerType/FooBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package extendsStaticInnerType;

public class FooBar {

public interface Crashy {

String foo();
}
}

0 comments on commit 86615d5

Please sign in to comment.