简体   繁体   中英

Java interfaces in annotation value

Why does the java compiler not allow using interfaces in annotations?

I can use enums in an annotation:

public @interface SomeAnnotation {
    SomeEnum[] value() default {};
}

I can implement an interface in an enum:

public enum SomeEnum implements SomeInterface {
   NAME1, NAME2; 
}

but I can't use interfaces in annotations:

public @interface SomeAnnotation {
    SomeInterface[] value() default {};
}

Quite frustrating...

Because an interface is not a valid return type, as per JLS the return type of a method declared in an annotation type must be one of the following, or a compile-time error occurs:

A primitive type

String

Class or an invocation of Class (§4.5)

An enum type

An annotation type

An array type whose component type is one of the preceding types (§10.1).

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