Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation Code Gen with JavaPoet

I am writing a code generator using JavaPoet and need to put an annotation on a class

For example :

@RequestMapping("/api")
public class SomeResource {
   // rest of the code elided
}

I am able to get this far:

TypeSpec spec = TypeSpec
   .classBuilder("SomeResource")
     .addAnnotation(AnnotationSpec.builder(RequestMapping.class)
     // what should go here?
     .build())
   .build();

There is an addMember method in the AnnotationSpec.Builder but that does not appear to do what I want.

like image 858
nvalada Avatar asked Sep 01 '15 10:09

nvalada


1 Answers

Please try adding annotation this way:

    TypeSpec spec = TypeSpec.classBuilder("SomeResource")
            .addAnnotation(
                    AnnotationSpec.builder(RequestMapping.class)
                    .addMember("value", "$S", "/api")
                    .build())
            .build();
like image 133
rpozarickij Avatar answered Oct 15 '22 13:10

rpozarickij