简体   繁体   中英

java annotation enum array default value

I want to write an annotation that hast a EnumArray Field. Default value should be all values of the Enum. This code works but I don't want to specify every enum manually.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CommonScope
{
    ECountry[] countries() default {ECountry.AT, ECountry.DE};

}

I want to do something like this:

ECountry[] countries() default ECountry.values();

Can someone tell me how to achieve this?

Thank you

This is not possible, annotation declaration is very limited. You can not call any methods or use properties, you can only use compile time constant expressions , that are known at compile time by the compiler.

As a workaround, you could add a special enum value like Countries.ALL_COUNTRIES if that makes sense for your application.

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