1

我可以使用 JCodeModel 生成注释类 exept 一件事。我不明白如何将其添加到注释中:

@Target(value={METHOD,TYPE,CONSTRUCTOR})

如何将定义值的数组设置为方法的值?我可以使用 JAnnotationUse 类的 param() 方法进行简单的注释,但是我找不到如何将数组设置为值。

4

1 回答 1

0

做类似这样的事情:

JAnnotationUse annotation = (JClass/JMethod/JFieldVar).annotate(Class<? extends java.lang.annotation.Annotation> MyAnnotation.class);
// Check if the value of the parameter of the annotation is an Array.
if (paramAnnotationValue.getClass().isArray()) {
    Object[] paramAnnotationValueArray = (Object[]) paramAnnotationValue;
    JAnnotationArrayMember annotationArrayMember = annotation.paramArray("parameter");
    for (Object paramValue : paramAnnotationValueArray) {
        annotationArrayMember.param((String/Boolean/Class/JType) paramValue);
    }
}
// No Array is normal.
else { 
    (...)
}

生成这样的东西:

@MyAnnotation(parameter = {
    "value_a",
    "value_b"
})
于 2013-02-12T16:36:08.607 回答