2012-03-31 58 views
0

我需要能夠獲得有關源文件的構造函數的信息,例如beging行號,也可能是構造函數中的一些行。我對文件的方法使用了類似的想法,以便能夠獲取開始和結束行號以及方法的名稱。對於這個使用JavaParser的即時消息,如here中所述。有沒有辦法使用JavaParser或其他API獲取有關構造函數的信息?

我找不到能夠爲我的目標使用JavaParser的方法。有沒有辦法能夠獲得構造函數的類似信息?

回答

1

你可以得到有關的信息構造您的方法聲明做同樣的方式:

CompilationUnit cu = JavaParser.parse(file); 
    List<TypeDeclaration> typeDeclarations = cu.getTypes(); 
    for (TypeDeclaration typeDec : typeDeclarations) { 
     List<BodyDeclaration> members = typeDec.getMembers(); 
     if(members != null) { 
      for (BodyDeclaration member : members) { 
       if (member instanceof ConstructorDeclaration) { 
        ConstructorDeclaration constructor = (ConstructorDeclaration) member; 
        //Put your code here 
        //The constructor instance contains all the information about it. 

        constructor.getBeginLine(); //begin line 
        constructor.getBlock(); //constructor body 
       } 
      } 
     } 
    } 
相關問題