Class<? extends Foo> bar() default null;
As already mentioned, the Java language specification does not allow null values ββin annotation defaults.
What I mean is to define the DEFAULT_VALUE constants at the top of the annotation definition. Something like:
public static final String DEFAULT_VALUE = "__class-name-here__ default"; public String prefix() default DEFAULT_VALUE;
Then in my code I do something like:
if (myAnnotation.prefix().equals(MyAnnotation.DEFAULT_VALUE)) { ... }
In your case with the class, I would simply define a marker class. Unfortunately, you do not have a constant for this, so you need to do something like:
Class<? extends Foo> bar() default DefaultFoo.class;
Your DefaultFoo class will be just an empty implementation so that the code can:
if (myAnnotation.bar() == DefaultFoo.class) { ... }
Hope this helps someone else.
Gray Jul 07 '17 at 15:42 2017-07-07 15:42
source share