2013-09-22 96 views
0

我想要做的是得到一個方法的類名。例如,我想獲得一個「直到」和「搜索」方法的類。這裏是代碼。如何使用Eclipse JDT ASTParser獲取方法的類名?

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

從這些例子中,預期的結果是Query.untilSearchResource.search。 但是,當我使用下面的代碼,我只得到,直到搜索,沒有類的名稱。如果我使用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; 
     } 
+0

您需要獲取Twitter1的類型,這可以是局部變量,方法參數或字段。獲得類型後,您需要檢查相應的類/ CompilationUnit。如果您在那裏找不到方法,則需要檢查超類和實現的接口。第一個更容易,你需要查看構造函數並以相同的方式檢查Type。 –

回答

2

java - VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast - Stack Overflowjava - bindings not resolving with AST processing in eclipse - Stack Overflow

這裏差不多是一個簡單的例子作爲RCP無頭應用程序。(與Java項目 「JavaProject」,其中包含類查詢,QueryResult中,信息搜索結果爲假)

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