Skip to content

Commit

Permalink
fix: Fix method resolution for inner interfaces (#4008)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jun 29, 2021
1 parent fb5a7c0 commit d3f21f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
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
19 changes: 18 additions & 1 deletion src/test/java/spoon/test/ctType/CtTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.ctType.testclasses.X;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -46,6 +45,9 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -267,4 +269,19 @@ public void testTypeDeclarationToReferenceRoundTripInNamedModule() {

assertSame(reFetchedTypeDecl, typeDecl);
}
@Test
public void testGetAllExecutablesOnTypeImplementingNestedInterface() {
// contract: implicit static nested interfaces are correct handled in getAllExecutables.
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();
int expectedNumExecutablesInJDK8 = 13;
int expectedNumExecutablesPostJDK8 = 14;
int numExecutables = type.getAllExecutables().size();
assertThat(numExecutables, anyOf(
equalTo(expectedNumExecutablesInJDK8),
equalTo(expectedNumExecutablesPostJDK8))
);
}
}
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 d3f21f4

Please sign in to comment.