2012-03-31 17 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 
       } 
      } 
     } 
    } 
相关问题