CodeModel - How to define annotations with an enum value?

1.4k Views Asked by At

It seems that the java code generator framework CodeModel is not capable of creating annotations which only contain an enum value without a name - unfortunately a very common pattern (which JPA uses, for example):

  @Temporal(TemporalType.TIMESTAMP)
  private Date createDate;

The API documentation only states "TODO How to add enums to the annotations"

Question:
Is there any way of working around this limitation?

2

There are 2 best solutions below

1
On BEST ANSWER

Annotation parameters "without a name" are actually just a shorthand for a default parameter named "value", so these are equivalent:

@Temporal(TemporalType.TIMESTAMP)

@Temporal(value=TemporalType.TIMESTAMP)
1
On

I'd say that comment is out of date. There is a method param(String name, Enum<?> value) on JAnnotationUse which works fine.

Edit: The code would look like the following:

field.annotate(Temporal.class).param("value", TemporalType.TIMESTAMP)

Please note that

@Temporal(TemporalType.TIMESTAMP)

Is just a short for of writing

@Temporal(value=TemporalType.TIMESTAMP)

This short form can be used when the annotation only contains a single parameter.