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!