我需要能够获取有关源文件的构造函数的信息,例如开始行号,也许还有构造函数内的一些行。我对文件的方法使用类似的想法,以便能够获取开始和结束行号以及方法的名称。为此,我使用了 JavaParser,如 here 中所述。 .
我找不到能够使用 JavaParser 来实现我的目标的方法。有没有办法能够获取构造函数的类似信息?
最佳答案
您可以像使用方法声明一样获取有关构造函数的信息:
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
}
}
}
}
关于java - 有没有办法使用 JavaParser 或其他 API 获取有关构造函数的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955859/