如何解决如何使用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);
}
我正试图为此输出。
verifyAgeLimit(): this.name 或 name
verifyAgeLimit(): this.name 或 name
showAddress():此地址或地址
我试图将整个方法转换为字符串,然后读取字符串以查找名称,但是它变得过于复杂且准确性下降。
有什么办法解决吗?
解决方法
已使用变量的名称由NameExpr
节点保存,而字段由FieldAccessExpr
节点保存。为他们添加访问者,否则递归到他们。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。