使用JavaParser類我可以用得到我的方法和字段列表:使用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源的所有變量的列表,而不管範圍如何。
如果你已經找到了你的問題的答案,不要編輯它:隨時回答! –
@Danilo謝謝你,我不知道。 – ialexander
只需編輯您的問題即可刪除解決方案,然後將其作爲答案粘貼回去。 –