0

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

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

从这些示例中,预期结果是Query.untilSearchResource.search。但是当我在下面使用这段代码时,我只得到了untilsearch,没有类名。如果我使用 MethodInvocation.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;
        }
4

1 回答 1

2

类似于 java - VariableDeclarationFragment 节点 resolveBindind() 在 eclipse/jdt/ast 中返回 null - 堆栈溢出java - 绑定无法在 eclipse 中通过 AST 处理解析

这是一个简单的 RCP 无头应用程序示例。(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;
            }
        });
    }
}
于 2013-09-22T13:56:47.993 回答