简体   繁体   中英

Adding a String field to a new compilationUnit with JavaParser

This might be a stupid question, but i want to acchive what the subject states. I want to add a new String field to a newly declared classOrInterfaceobject in a new new compilationUnit. But from what i can tell from the sourcefiles, that option is not possible. The primitiveClass only holds enums for all the other primitives, Long, char, bytes etc.

Am i missing something? Or have the developers forgot about the String option?

SOLVED Thanks to Riduidels answer, i managed to crack the code, so to speak :) The thing was to create a new ClassOrInterfaceType and calling it String, simple enough. Though, i must say, that the people behind JavaParser should look into adding a enum for String as they have for the other Primitives. Working code:

public static void main(String[] args){
    // TODO Auto-generated method stub
     // creates the compilation unit
    CompilationUnit cu = createCU();


    // prints the created compilation unit
    System.out.println(cu.toString());
}

/**
 * creates the compilation unit
 */
private static CompilationUnit createCU() {
    CompilationUnit cu = new CompilationUnit();
    // set the package
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));

    // create the type declaration 
    ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
    ASTHelper.addTypeDeclaration(cu, type); // create a field
    FieldDeclaration field = ASTHelper.createFieldDeclaration(ModifierSet.PUBLIC, new ClassOrInterfaceType("String"),"test");

    ASTHelper.addMember(type, field);



    return cu;
}

Thanks Riduidel!

Well, that's quite normal : JavaParser type hierarchy is really close to what you have in a Java source file. And in a source file, you don't put your strings directly in the file, but rather in a class declared in a file.

This is rather well described in JavaParser section Creating a CompilationUnit from scratch , which content can be addapted to become

public class ClassCreator {

    public static void main(String[] args) throws Exception {
        // creates the compilation unit
        CompilationUnit cu = createCU();

        // prints the created compilation unit
        System.out.println(cu.toString());
    }

    /**
     * creates the compilation unit
     */
    private static CompilationUnit createCU() {
        CompilationUnit cu = new CompilationUnit();
        // set the package
        cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));

        // create the type declaration 
        ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
        ASTHelper.addTypeDeclaration(cu, type);

        // create a field
        FieldDeclaration field = new FieldDeclaration(ModifierSet.PUBLIC, new ClassOrInterface(String.class.getName()), new VariableDeclarator(new VariableDeclaratorId("variableName")))
        ASTHelper.addMember(type, field);
        return cu;
    }
}

And this will create a file containing a class in package java.parser.test named GeneratedClass containing a simple field named GeneratedClass (although I didn't compiled the above code to ensure its correctness).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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