Resolving overloaded methods with ASTVisitor in Eclipse JDT
I am currently working on an academic project which employs an ASTVisitor to create a basic calltree.
For this purpose it is needed to associate the invocation of a method with its declaration.
EDIT: Problem is solved to a large extent: The described error appears only JUnit-Tests, but but not in the standalone-plugin. It seemingly has to do with the unit-testing workflow of ASTVisitors. I will further investigate the reason in some time and post the answer here.
The code below shows a minimal visitor which prints invocations and associated declarations to console:
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
/**
* Visits all method invocations and prints the declaration of the caller to
* console.
*/
public class InvocationLoggerASTVisitor extends ASTVisitor {
@Override
public boolean visit(MethodInvocation methodInvocation) {
if (methodInvocation.resolveMethodBinding() != null) {
IMethodBinding declarationOfInvokedMethod = methodInvocation
.resolveMethodBinding().getMethodDeclaration();
System.out.println(String.format(
"invocation of \"%s\" is resolved to declaration \"%s\"",
methodInvocation, declarationOfInvokedMethod));
}
return super.visit(methodInvocation);
}
}
The visitor works for some tested scenarios. But strangely, when applying it to compilation units which contain overloaded methods, some invocations get associated with wrong declarations. Lets visit the following code:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class OverloadedMethodsAndRawTypes implements ICallGraphTestSource {
void f(HashSet objects) {
f(new ArrayDeque(objects));
}
void f(ArrayDeque objects) {
f(new ArrayList(objects));
}
void f(ArrayList objects) {
f(new HashSet(objects));
}
}
When visiting this compilation unit, the console output is:
invocation of "f(new ArrayDeque(objects))" is resolved to declaration "void f(HashSet) "
invocation of "f(new ArrayList(objects))" is resolved to declaration "void f(HashSet )"
invocation of "f(new HashSet(list))" is resolved to declaration "void f(HashSet) "
I would expect this output:
invocation of "f(new ArrayDeque(objects))" is resolved to declaration "void f(ArrayDeque) "
invocation of "f(new ArrayList(objects))" is resolved to declaration "void f(ArrayList )"
invocation of "f(new HashSet(list))" is resolved to declaration "void f(HashSet) "
I've noticed, that invocations of overloaded methods always resolve to the first occurring declaration matching the invoked method name, which seems not right to me.
To prove, that method overloading is to blame, i attach the same code without method overloading:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class RawTypes implements ICallGraphTestSource {
void f_set(HashSet objects) {
f_deque(new ArrayDeque(objects));
}
void f_deque(ArrayDeque objects) {
f_list(new ArrayList(objects));
}
void f_list(ArrayList objects) {
f_set(new HashSet(objects));
}
}
When visited, it yields the correct output:
invocation of "f_deque(new ArrayDeque(objects))" is resolved to declaration "void f_deque(ArrayDeque) "
invocation of "f_list(new ArrayList(objects))" is resolved to declaration "void f_list(ArrayList) "
invocation of "f_set(new HashSet(list))" is resolved to declaration "void f_set(HashSet) "
My ASTParser is configured as following:
public static ASTNode getAST(ICompilationUnit compilationUnit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setProject(getJavaProject());
return parser.createAST(null);
}
EDIT: The described setup does not work in JUnit-Tests, but it does work as standalone-plugin. T开发者_如何学Che reason must be therefor in the getJavaProject-Method, where a temporary project is created.
Since the erroneous behaviour only happens in test cases, the creation of the AST seems to be not set up properly.
I found a testing setup which works:
For each test I read the CompilationUnit
as text from the file system and create a new project programmatically, following an approach derived from a description here.
As you see in the ASTParser configuration in the original question, an IJavaProject
-instance is passed using parser.setProject(IJavaProject)
, holding an environment to resolve invocations properly.
This instance has to be configured with a classpath:
/**
* @param project
* a project with JavaNature
* @return a java project with classpath set to the default JRELibrary
* @throws JavaModelException
*/
private static IJavaProject createJavaProject(IProject project)
throws JavaModelException {
IJavaProject javaProject = JavaCore.create(project);
javaProject.setRawClasspath(PreferenceConstants.getDefaultJRELibrary(),
null);
return javaProject;
}
In my case I need only the default JRE-library. In your case there may be need to augment the classpath. In any case, this solves the problem.
精彩评论