Skip to content

Commit

Permalink
Merge pull request #2920 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix Issue 2909 Improving how to resolve inner classes
  • Loading branch information
jlerbsc committed Nov 14, 2020
2 parents efb242d + b9628bb commit 149eade
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,26 @@ public ResolvedType visit(ThisExpr node, Boolean solveLambdas) {
// Attempt to resolve locally in Compilation unit
Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class);
if (cu.isPresent()) {
// Try to get a top level class declaration by its name
Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className);
if (classByName.isPresent()) {
return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver);
}
// Try to resolve locally from inner class
classByName = getLocalDeclarationFromClassname(cu.get(), className);
if (classByName.isPresent()) {
return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver);
}
}
return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node, className)), typeSolver);

}
return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver);
}

private Optional<ClassOrInterfaceDeclaration> getLocalDeclarationFromClassname(CompilationUnit cu, String className) {
return cu.findAll(ClassOrInterfaceDeclaration.class).stream().filter(cid->cid.getNameAsString().equals(className)).findFirst();
}

@Override
public ResolvedType visit(SuperExpr node, Boolean solveLambdas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class Issue2909Test extends AbstractResolutionTest {

@Test
void test() {
void testResolvingLocallyFromCompleteReferenceToInnerClass() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
Expand All @@ -42,4 +42,32 @@ void test() {
assertEquals("Program.OuterClass",expr.calculateResolvedType().describe());
});
}

@Test
void testResolvingLocallyFromPartialReferenceToInnerClass() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);

String s =
"public class Program {\n" +
"\n" +
" public class OuterClass {\n" +
" int field = 0;\n" +
"\n" +
" public class InnerClass {\n" +
" InnerClass() {\n" +
" OuterClass outer = OuterClass.this;\n" +
" OuterClass.this.field = 1;\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

CompilationUnit cu = StaticJavaParser.parse(s);
List<ThisExpr> exprs = cu.findAll(ThisExpr.class);
exprs.forEach(expr-> {
assertEquals("Program.OuterClass",expr.calculateResolvedType().describe());
});
}
}

0 comments on commit 149eade

Please sign in to comment.