1
注释PARAM的值

使用JavaPoet我想要注释的一类与具有阵列作为参数值即通行证阵列中JavaPoet

@MyCustom(param = { Bar.class, Another.class }) 
class Foo { 
} 

我使用AnnotationSpec.builder及其addMember()方法的注释:

List<TypeMirror> moduleTypes = new ArrayList<>(map.keySet()); 
AnnotationSpec annotationSpec = AnnotationSpec.builder(MyCustom.class) 
    .addMember("param", "{ $T[] } ", moduleTypes.toArray()) 
    .build(); 
builder.addAnnotation(annotationSpec); 

回答

0

也许不是最佳解决方案,但将数组传递给在JavaPoet注释可以通过以下方式来完成:

List<TypeMirror> moduleTypes = new ArrayList<>(map.keySet()); 
CodeBlock.Builder codeBuilder = CodeBlock.builder(); 
boolean arrayStart = true; 
codeBuilder.add("{ "); 
for (TypeMirror modType: moduleTypes) 
    if (!arrayStart) 
     codeBuilder.add(" , "); 
    arrayStart = false; 
    codeBuilder.add("$T.class", modType); 
codeBuilder.add(" }"); 

AnnotationSpec annotationSpec = AnnotationSpec.builder(MyCustom.class) 
    .addMember("param", codeBuilder.build()) 
    .build(); 
builder.addAnnotation(annotationSpec);