使用 JavaParser,我可以使用以下方法获取我的方法和字段的列表:
//List of all methods
System.out.println("Methods: " + this.toString());
List<TypeDeclaration> types = n.getTypes();
for (TypeDeclaration type : types)
{
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members)
{
if (member instanceof MethodDeclaration)
{
MethodDeclaration method = (MethodDeclaration) member;
System.out.println("Methods: " + method.getName());
}
}
}
//List all field variables.
System.out.println("Field Variables: " + this.toString());
List<TypeDeclaration> f_vars = n.getTypes();
for (TypeDeclaration type : f_vars)
{
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members)
{
if (member instanceof FieldDeclaration)
{
FieldDeclaration myType = (FieldDeclaration) member;
List <VariableDeclarator> myFields = myType.getVariables();
System.out.println("Fields: " + myType.getType() + ":" + myFields.toString());
}
}
}
但我不知道如何获取我的变量列表。我只是想要一个来自 java 源的所有变量的列表,而不管范围如何。