问题
What I am trying to do is to get a class name of a method. For example, I want to get a class of 'until' and 'search' methods. Here are the code.
Query query = new Query(queryStr).until(dateStr);
QueryResult queryResult = twitter1.search(query);
From these examples, the expected results are Query.until and SearchResource.search. But when I used this code below, I only got until and search, no class name. If I use MethodInvocation.getExpression(), I can get the names of the instances:new Query(queryStr) and twitter1. But they are not what I really want.
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(MethodDeclaration node){
System.out.println("Declaration of '"+node.getName()+"' at line"
+ cu.getLineNumber(node.getStartPosition()));
if (node.getName().toString().equals("testSearch")){
Block block =node.getBody();
block.accept(new ASTVisitor() {
public boolean visit(MethodInvocation node) {
//System.out.println(node.getExpression());
System.out.println("Name: " + node.getName());
return true;
}
});
}
return true;
}
回答1:
Similar to java - VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast - Stack Overflow or java - bindings not resolving with AST processing in eclipse - Stack Overflow
Here is a simple example as RCP headless app.(with Java project "JavaProject" which contains classes Query, QueryResult, SearchResult as dummy)
package test;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
public class Test {
String str = "package javaproject;" // package for all classes
+ "class Dummy {" //
+ " void testSearch(String queryStr, String dateStr, SearchResources twitter1) {" //
+ " Query query = new Query(queryStr).until(dateStr);" //
+ " QueryResult queryResult = twitter1.search(query);" //
+ " }" //
+ "}";
public void testrun() {
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setEnvironment( // apply classpath
new String[] { "C:\\eclipse\\workspace\\JavaProject\\bin" }, //
null, null, true);
parser.setUnitName("any_name");
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(MethodDeclaration node) {
if (node.getName().getIdentifier().equals("testSearch")) {
Block block = node.getBody();
block.accept(new ASTVisitor() {
public boolean visit(MethodInvocation node) {
System.out.println("Name: " + 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("Type: " + typeBinding.getName());
}
}
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
ITypeBinding type = binding.getDeclaringClass();
if (type != null) {
System.out.println("Decl: " + type.getName());
}
}
return true;
}
});
}
return true;
}
});
}
}
来源:https://stackoverflow.com/questions/18939857/how-to-get-a-class-name-of-a-method-by-using-eclipse-jdt-astparser