Reputation: 21
I use CodeModel to generate Java code. I expect output like this:
public static final String[] COLUMNS = {ID, CODE, NAME};
I tried:
definedClass.field(JMod.PUBLIC|JMod.STATIC|JMod.FINAL, String[].class, fieldName, JExpr.newArray(codeModel.ref(String.class)));
but I do not know how to "add" values to the array.
Upvotes: 2
Views: 1044
Reputation: 14329
JExpr.newArray() returns JArray, and JArray.add() can be used to add JExpression instances to the initializer. Assuming ID, CODE, and NAME are, say, JExpression instances for local fields, then:
JExpr.newArray(codeModel.ref(String.class)).add(ID).add(CODE).add(NAME)
will generate:
new String[]{ID, CODE, NAME}
Upvotes: 3