如何使用javaparser获取类的方法中使用的变量名



我正在分析的类:

public class Users{
private String name = "Jacob";
private String address = "711 11th Ave, New York";
private int age = 28;

public String verifyAgeLimit() {
String msg = (this.age < 16) ? " is not allwed to use it." : " is Allwed to use it.";   
return this.name+msg;
}

public String showName() {
return "User name is:"+this.name;
}

public String showAddress() {
return this.address;
}
public String hello() {
return "Hello World";
}
}

我正在尝试使用MethodDeclaration获取变量名,但无法做到

public void visit(MethodDeclaration n, Object arg) {        
System.out.println(n.getName().toString());//It displays the full method
super.visit(n, arg);
}

我正在尝试获得这个is的输出。

verifyAgeLimit((this.namename

verifyAgeLimit((this.namename

showAddress((this.addressaddress

我试图将整个方法转换为sting,并读取字符串以找出名称,但它变得太复杂,而且非常不准确。

有什么办法解决吗?

所用变量的名称由NameExpr节点保存,字段由FieldAccessExpr节点保存。为他们添加一个访问者,或者以其他方式递归到他们。

最新更新