问题描述
我正在分析的课程:
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);
}
我正试图为此输出。
verifyAgeLimit(): this.name 或 name
verifyAgeLimit(): this.name 或 name
showAddress():此地址或地址
我试图将整个方法转换为字符串,然后读取字符串以查找名称,但是它变得过于复杂且准确性下降。
有什么办法解决吗?
解决方法
已使用变量的名称由NameExpr
节点保存,而字段由FieldAccessExpr
节点保存。为他们添加访问者,否则递归到他们。