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

标签 java eclipse eclipse-jdt

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

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/

相关文章:

android - 更新到 ADT 23 后找不到 annotations.jar

eclipse - 使用 Eclipse 的 JDT,如何从类名中获取 IType?

java - Eclipse JDT : How do I auto-move dependencies of a statement as well, 同时移动语句

java - Feign Hystrix 回退不起作用

java - JCE 无法验证提供商 ABA

java - 第: How to retrieve "href" value from <a> link

java - 在 JUnit 中测试没有 sleep 的潜在死锁

java - 如何在没有 "sources JAR"的 lib 的 Eclipse 项目中附加源代码?

java - Maven:无法检索pluginorg.apache.maven.plugins:maven-install-plugin:2.4或其依赖项之一无法解析

java - 在eclipse中的方法中获取字段类型