Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 372
Klaus Ertl Avatar asked Mar 04 '15 09:03

Klaus Ertl


People also ask

What is default value of enum Java?

The default for one who holds a reference to an enum without setting a value would be null (either automatically in case of a class field, or set by the user explicitly).

What is the default enum value type in JPA?

JPA & Hibernate Standard Enum Mappings By default, Hibernate maps an enum to a number. It uses the ordinal value, which is the zero-based position of a value within the definition of the enum. So, the enum value that's defined first gets mapped to 0, the second one to 1 and so on.

How do I change the default value of enum in Kotlin?

Kotlin doesn't assign any default value to Enum constants or enum objects. But you can manually set any value to your enum constant. Let's see an easy example to understand this case. In this enum class, we are passing an Int value to enum constants DOG, CAT and FISH.


1 Answers

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.

like image 113
kapex Avatar answered Sep 18 '22 15:09

kapex