Annotation typed array values


#1

  

package java.lang.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
  ElementType[] value();
}
Documented
Retention(value = RetentionPolicy.RUNTIME)
// Error : Target(value = ElementType.FIELD, ElementType.CONSTRUCTOR)
// Error : Target(value = array(ElementType.FIELD, ElementType.CONSTRUCTOR)) // Error : Target(value = array<ElementType>(ElementType.FIELD, ElementType.CONSTRUCTOR))
// Error : Target(value = Array<ElementType>(ElementType.FIELD, ElementType.CONSTRUCTOR))
annotation class TestAnnotation(val a: String, val b: Int)

But, i don’t know write typed array ElementType[] value();

How to write typed array?


#2

Kotlin sees array arguments of Java annotations as vararg automatically, try this:

Target(ElementType.FIELD, ElementType.CONSTRUCTOR)

#3

Thank you Alexander!


#4

I still think array() should work in annotation parameter. See https://youtrack.jetbrains.com/issue/KT-6641 for more discussion.