如何使用Eclipse JDT ASTParser获取方法的类名? [英] How to get a class name of a method by using Eclipse JDT ASTParser?
问题描述
我想要做的是获取一个方法的类名。
例如,我想得到一个'直到'和'搜索'方法的类。以下是代码。
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);
从这些示例中,预期的结果是 Query.until 和 SearchResource.search 即可。
但是当我使用下面的代码,我只有直到和搜索,没有类名称。如果我使用MethodInvocation.getExpression(),我可以获取实例的名称:new Query(queryStr)和twitter1。但是它们不是我真正想要的。
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;
}
推荐答案
类似于
java - VariableDeclarationFragment节点resolveBindind()在eclipse / jdt / ast中返回null - 堆栈溢出
或
java - 绑定不能用eclipse中的AST处理解析 - 堆栈溢出
以下是RCP无头版应用程序的简单示例(Java项目JavaProject,其中包含Query,QueryResult,SearchResult为哑) p>
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;
}
});
}
}
这篇关于如何使用Eclipse JDT ASTParser获取方法的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!