2

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.

1 Answers1

3

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}
erisco
  • 14,154
  • 2
  • 40
  • 45