'How to get fully qualified type names for method callers using eclipse JDT?
I am using Eclipse JDT parser to identify method invocations and their caller types. However, in some cases the inferred type is null or it is not fully resolved. I'm not sure if that's the best I can get from JDT parser or is there something missing in my code. How do I modify my code to get the missing types? Here is my attempt so far.
public static void testrun(String str) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setEnvironment(new String[] {dir}, null, null, true);
parser.setUnitName("any_name");
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(MethodDeclaration node) {
System.out.println("\nMethod name:"+node.getName());
Block block = node.getBody();
int modifiers = node.getModifiers();
if ((modifiers & Modifier.ABSTRACT) > 0) {
System.out.println("ABSTRACT");
} else {
block.accept(new ASTVisitor() {
public boolean visit(MethodInvocation node) {
String namestr = node.getName().toString();
System.out.println("Calls: " + node.getName());
Expression expression = node.getExpression();
if (expression != null) {
System.out.println("Expr: " + expression.toString());
ITypeBinding typeBinding = expression.resolveTypeBinding();
if (typeBinding != null) {
//System.out.println("Call: " + node.getName());
//System.out.println("Exprs: " + expression.toString());
System.out.println("Types: " + typeBinding.getQualifiedName());
}
}
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
ITypeBinding type = binding.getDeclaringClass();
if (type != null) {
System.out.println("User-defined class: " + type.getName());
}
} return true;
}
});
}
return true;
}
});
}
Here is the Example input
public void doBackup(File directory) throws Exception {
List parameters = new ArrayList();
parameters.add(new Parameter("key", Key));
if (!directory.exists()) directory.mkdir();
RequestContext rc = RequestContext.getRequestContext();
if (this.authStore != null) {
Auth auth = this.authStore.retrieve(this.nsid);
if (auth == null) this.authorize();
else rc.setAuth(auth);
}
//more code...
}
Here is the Example Output:
Calls:add Expr:parameters //can't infer java.lang types List
Calls:exists Expr:directory //can't infer java.lang types File
Calls:mkdir Expr:directory //can't infer java.lang type File
Calls:getRequestContext Expr:RequestContext // was hoping to get com.aetrion.flickr.RequestContext;
Calls:retrieve Expr:this.authStore //was hoping to get com.aetrion.flickr.util.AuthStore;
Calls:authorize Expr:this Types:Backup
Calls:setAuth Expr: rc //was hoping to get com.aetrion.flickr.RequestContext;
As one can see it does not return Type all the times and Expression does not contain fully qualified name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
