Skip to content

Commit

Permalink
Merge pull request #2095 from maartenc/ScopedAnonymousClass
Browse files Browse the repository at this point in the history
Fix: issue with resolving scoped anonymous inner classes
  • Loading branch information
ftomassetti committed Feb 20, 2019
2 parents 26e04a8 + 600aef8 commit 26115b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
Expand Down Expand Up @@ -36,9 +37,16 @@ public JavaParserAnonymousClassDeclaration(ObjectCreationExpr wrappedNode,
TypeSolver typeSolver) {
this.typeSolver = typeSolver;
this.wrappedNode = wrappedNode;

ClassOrInterfaceType superType = wrappedNode.getType();
String superTypeName = superType.getName().getId();
if (superType.getScope().isPresent()) {
superTypeName = superType.getScope().get().asString() + "." + superTypeName;
}

superTypeDeclaration =
JavaParserFactory.getContext(wrappedNode.getParentNode().get(), typeSolver)
.solveType(wrappedNode.getType().getName().getId())
.solveType(superTypeName)
.getCorrespondingDeclaration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,19 @@ void usingAnonymousClassInnerType() {

assertThat(methodUsage.getQualifiedSignature(), is("java.lang.Enum.toString()"));
}

@Test
void callingScopedAnonymousClassInnerMethod() {
CompilationUnit cu = parseSample("AnonymousClassDeclarations");
ClassOrInterfaceDeclaration aClass = Navigator.demandClass(cu, "AnonymousClassDeclarations");
MethodDeclaration method = Navigator.demandMethod(aClass, "fooBar6");
MethodCallExpr methodCall = Navigator.findMethodCall(method, "innerClassMethod").get();

CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
MethodUsage methodUsage =
JavaParserFacade.get(combinedTypeSolver).solveMethodAsUsage(methodCall);

assertThat(methodUsage.getQualifiedSignature(), is("AnonymousClassDeclarations.DoFn.ProcessContext.innerClassMethod()"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ class AnonymousClassDeclarations {
});
}

void fooBar6() {
new DoFn.ProcessContext() {}.innerClassMethod();
}

}

0 comments on commit 26115b8

Please sign in to comment.