简体   繁体   English

如何使用javaparser获取类级别的变量声明?

[英]How to get class level variable declarations using javaparser ?

I want to get only the class level variable declarations. 我只想获取类级别的变量声明。 How can i get the declarations using javaparser? 如何使用javaparser获取声明?

public class Login {

    private Keyword browser;
    private String pageTitle = "Login";
}

Using javaparser have to get the details of variable "browser" like the type of browser is "KeyWord" 使用javaparser必须获取变量“ browser”的详细信息,例如浏览器的类型为“ KeyWord”

Not quite sure i understood your question - do you want to get all the field-members of a class? 不太确定我是否理解您的问题-您想让班上所有的现场成员吗? if so you can do it like this: 如果是这样,您可以这样做:

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());
        }  
    }
}

This code example will print in your case: Keyword browser String pageTitle "Login" 在您的情况下,将显示以下代码示例:关键字浏览器字符串页面标题“登录”

I hope this was really your question... if not, please comment. 我希望这确实是您的问题...如果没有,请发表评论。

To update the above answer to the latest version of JavaParser: 要将以上答案更新为最新版本的JavaParser:

CompilationUnit cu = JavaParser.parse("public class Login {\n" +
        "\n" +
        "    private Keyword browser;\n" +
        "    private String pageTitle = \"Login\";\n" +
        "}\n");

for (TypeDeclaration<?> typeDec : cu.getTypes()) {
    for (BodyDeclaration<?> member : typeDec.getMembers()) {
        member.toFieldDeclaration().ifPresent(field -> {
            for (VariableDeclarator variable : field.getVariables()) {
                //Print the field's class typr
                System.out.println(variable.getType());
                //Print the field's name
                System.out.println(variable.getName());
                //Print the field's init value, if not null
                variable.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
            }
        });
    }
}

and a way to get to the field declarations without as much hassle is... 而没有那么麻烦就可以到达字段声明的方法是...

cu.findAll(FieldDeclaration.class).forEach(field -> {
    field.getVariables().forEach(variable -> {
        //Print the field's class typr
        System.out.println(variable.getType());
        //Print the field's name
        System.out.println(variable.getName());
        //Print the field's init value, if not null
        variable.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
    });
});

The functional difference between the two is that the first one only looks in the top level class, and the second one will also look in nested classes. 两者之间的功能差异在于,第一个仅在顶级类中查找,而第二个也将在嵌套类中查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM