0

Hi I am relatively new to JCodeModel. I have the follwoing annotation over my class

@JsonSubTypes({ @JsonSubTypes.Type(value = com.abc.MyClass.class, name = "MyClass")})
class MyClass {

}

How can I create Annotation for the following using JCodeModel. Thanks.

prit kalra
  • 319
  • 4
  • 18
  • Possible duplicate: https://stackoverflow.com/questions/19853943/java-codemodel-annotate-a-method-or-class – Mebin Joe Feb 28 '19 at 07:15
  • I want to know how to provide array as argument without any name inside the annotation. The sample you shared doesn't showcase an array as parameter of annotation. – prit kalra Feb 28 '19 at 07:19
  • you mean arguments for JCodeModel ? – Mebin Joe Feb 28 '19 at 07:25
  • Yes, the argument is of type Array. I don't see any such support without mentioning the name of the paramArray. – prit kalra Feb 28 '19 at 07:51
  • Yea. I believe you can't achieve it directly using any constructor. Maybe you could try custom setter and getter methods and converting or casting to array – Mebin Joe Feb 28 '19 at 07:53

1 Answers1

0

Just annotate your JClass, and to add an array parameter, use the paramArray() method:

JDefinedClass testClass = codeModel._class("MyClass");

JAnnotationUse jsonSubTypes = testClass.annotate(JsonSubTypes.class);

jsonSubTypes.paramArray("value")
    .annotate(JsonSubTypes.Type.class)
        .param("value", MyClass.class)
        .param("name", "MyClass");

Generates:

@JsonSubTypes({
    @JsonSubTypes.Type(value = MyClass.class, name = "MyClass")
})
public class MyClass {


}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75