Java allows enum as values for annotation values. How can I define a kind of generic default enum value for an enum annotation value?
I have considered the following, but it won't compile:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public <T extends Enum<T>> @interface MyAnnotation<T> {
T defaultValue();
}
Is there a solution to this issue or not?
BOUNTY
Is does not seem like there is a direct solution to this Java corner case. So, I am starting a bounty to find the most elegant solution to this issue.
The ideal solution should ideally meet the following criteria:
- One annotation reusable on all enums
- Minimum effort/complexity to retrieve the default enum value as an enum from annotation instances
BEST SOLUTION SO FAR
By Dunes:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {
// By not specifying default,
// we force the user to specify values
Class<? extends Enum<?>> enumClazz();
String defaultValue();
}
...
public enum MyEnumType {
A, B, D, Q;
}
...
// Usage
@MyAnnotation(enumClazz=MyEnumType.class, defaultValue="A");
private MyEnumType myEnumField;
Of course, we can't force the user to specify a valid default value at compile time. However, any annotation pre-processing can verify this with valueOf().
IMPROVEMENT
Arian provides an elegant solution to get rid of clazz in annotated fields:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {
}
...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@MyAnnotation()
public @interface MyEnumAnnotation {
MyEnumType value(); // no default has user define default value
}
...
@MyEnumAnnotation(MyEnum.FOO)
private MyEnumType myValue;
The annotation processor should search for both MyEnumAnnotation on fields for the provided default value.
This requires the creation of one annotation type per enum type, but guarantees compile time checked type safety.
private MyEnum field = MyEnum.DEFAULT;?