简体   繁体   中英

JCodeModel and arrays

I can generate annotation class using JCodeModel exept one thing. I can't understand how to add this to annotation:

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

How to set array of defined values as value of value method? I can use param() method of JAnnotationUse class for simple annotations, but how to set array as value I can't find.

Make something similar to this:

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 { 
    (...)
}

Something like this is generated:

@MyAnnotation(parameter = {
    "value_a",
    "value_b"
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM