使用 JavaPoet 生成注释代码

Annotation Code Gen with JavaPoet

我正在使用 JavaPoet 编写代码生成器,需要在 class

上添加注释

例如:

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

我能做到这一点:

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

AnnotationSpec.Builder 中有一个 addMember 方法,但它似乎无法满足我的要求。

请尝试以这种方式添加注释:

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