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
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With