I want to use JavaPoet to generate annotations with a type literal as a value. For instance:
@AutoService(MyService.class) public class GeneratedClass implements MyService { }
I tried all the options that I can think of, but no one works:
TypeSpec.classBuilder("GeneratedClass") .addModifiers(Modifier.PUBLIC) .addSuperinterface(MyService.class) .addAnnotation( AnnotationSpec.builder(AutoService.class).addMember( "value", "$L", MyService.class ).build() )
using $L generates an interface MyService
using $T generates my.package.MyService , which is close but skips the .class part.
using $N produces an error: expected name but was my.package.MyService
How do I get it to generate MyService.class as an annotation value?
source share