How to get class level variable declarations using javaparser ?

后端 未结 2 1409
说谎
说谎 2021-01-25 03:44

I want to get only the class level variable declarations. How can i get the declarations using javaparser?

public class Login {

    private Keyword browser;
            


        
相关标签:
2条回答
  • 2021-01-25 03:48

    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.

    0 讨论(0)
  • 2021-01-25 04:06

    To update the above answer to the latest version of 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.

    0 讨论(0)
提交回复
热议问题