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.
  • Loading branch information
MartinWitt committed Jun 27, 2021
1 parent 5f2e4f5 commit 90e2818
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 @@ -33,6 +34,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -43,6 +45,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 +221,15 @@ 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());
assertEquals(13, 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 90e2818

Please sign in to comment.