javaparser / javaparser/javaparser
Solve method declarations in anonymous inner classes
- Dominant language
- Java
- Stars
- 6.2k
- Forks
- 1.3k
- Avg merge
- 17h 36m
- Merged PRs (30d)
- 28
Description
When solving for method declarations in anonymous inner classes, the result is not what I want.
The result is:
```
Main.main(java.lang.String[])
java.io.PrintStream.println(java.lang.String)
"Main.main(java.lang.String[])" -> "java.io.PrintStream.println(java.lang.String)"
java.lang.Runnable.run()
"Main.main(java.lang.String[])" -> "java.lang.Runnable.run()"
Main.Anonymous-9904eed3-5b31-434d-9b07-63e5b069a3a2.run()
java.io.PrintStream.println(java.lang.String)
"Main.Anonymous-9904eed3-5b31-434d-9b07-63e5b069a3a2.run()" -> "java.io.PrintStream.println(java.lang.String)"
```
Why have such results as Anonymous-9904eed3-5b31-434d-9b07-63e5b069a3a2 when solving the method declaration "run"?However, when solving the methodcallexpr "runnable.run()", the result is java.lang.Runnable.run().
This is a testcode:
```
package testcode;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
public class Main {
public static void main(String[] args) {
String sourcecode= "public class Main {\n" +
"\n" +
" public static void main(String[] args) {\n" +
" Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"This is an anonymous inner class.\");\n" +
" }\n" +
" };\n" +
"\n" +
" runnable.run();\n" +
"\n" +
" }\n" +
"\n" +
"}";
CombinedTypeSolver myTypeSolver = new CombinedTypeSolver();
myTypeSolver.add(new ReflectionTypeSolver());
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(myTypeSolver);
StaticJavaParser
.getConfiguration()
.setSymbolResolver(symbolSolver);
CompilationUnit cu = StaticJavaParser.parse(sourcecode);
List methods = cu.findAll(MethodDeclaration.class);
for (MethodDeclaration method : methods) {
String fullCallerMethodName = method.resolve().getQualifiedSignature();
System.out.println(fullCallerMethodName);
List methodCallees = method.findAll(MethodCallExpr.class);
for (MethodCallExpr callee : methodCallees) {
String calleeMethodName = callee.resolve().getQualifiedSignature();
System.out.println(calleeMethodName);
System.out.println("\"" + fullCallerMethodName + "\"" + " -> " + "\"" + calleeMethodName + "\"");
}
}
}
}
```
Contributor guide
Research direction
Start with the supplied Main reproduction and compare MethodDeclaration.resolve() with MethodCallExpr.resolve() for the anonymous Runnable's run method. Read the JavaSymbolSolver resolution entry points and run the reproduction to trace how the anonymous class is named. Done means resolving the declaration produces a consistent, useful method signature without the generated Anonymous identifier.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100