我只想获取类级别的变量声明。如何使用 javaparser 获取声明?
public class Login {
private Keyword browser;
private String pageTitle = "Login";
}
使用javaparser必须获取变量“浏览器”的详细信息,例如浏览器的类型是“KeyWord”
不太确定我理解你的问题 - 你想得到一个班级的所有现场成员吗?如果是这样你可以这样做:
CompilationUnit cu = JavaParser.parse(javaFile);
for (TypeDeclaration typeDec : cu.getTypes()) {
List<BodyDeclaration> members = typeDec.getMembers();
if(members != null) {
for (BodyDeclaration member : members) {
//Check just members that are FieldDeclarations
FieldDeclaration field = (FieldDeclaration) member;
//Print the field's class typr
System.out.println(field.getType());
//Print the field's name
System.out.println(field.getVariables().get(0).getId().getName());
//Print the field's init value, if not null
Object initValue = field.getVariables().get(0).getInit();
if(initValue != null) {
System.out.println(field.getVariables().get(0).getInit().toString());
}
}
}
此代码示例将在您的情况下打印:
关键词
浏览器
细绳
页面标题
“登录”
我希望这确实是您的问题...如果不是,请发表评论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)