Gen annotation code with JavaPoet

I am writing a code generator using JavaPoet and I need to put the annotation in a class

For instance:

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

I can go this far:

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

An addMember method exists in AnnotationSpec.Builder, but it doesn't seem to do what I want.

+5
source share
1 answer

Try adding an annotation as follows:

  TypeSpec spec = TypeSpec.classBuilder("SomeResource") .addAnnotation( AnnotationSpec.builder(RequestMapping.class) .addMember("value", "$S", "/api") .build()) .build(); 
+6
source

All Articles