gpt4 book ai didi

java - 如何使用 Eclipse JDT ASTParser 获取方法的类名?

转载 作者:行者123 更新时间:2023-12-02 06:42:58 27 4
gpt4 key购买 nike

我想要做的是获取方法的类名。例如,我想获得一类“直到”和“搜索”方法。这是代码。

Query query = new Query(queryStr).until(dateStr);
QueryResult queryResult = twitter1.search(query);

从这些示例中,预期结果是 Query.untilSearchResource.search。但是当我使用下面的代码时,我只得到了 untilsearch,没有类名。如果我使用 MethodInitation.getExpression(),我可以获得实例的名称:new Query(queryStr) 和 twitter1。但它们并不是我真正想要的。

    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 node resolveBindind() returns null in eclipse/jdt/ast - Stack Overflow或者 java - bindings not resolving with AST processing in eclipse - Stack Overflow

这是一个 RCP headless 应用程序的简单示例。(使用 Java 项目“JavaProject”,其中包含类 Query、QueryResult、SearchResult 作为虚拟)

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;
}
});
}
}

关于java - 如何使用 Eclipse JDT ASTParser 获取方法的类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com