Using the default class literal value in annotations

I want to annotate some fields of this bean class with the following annotation:

@Target({FIELD}) @Retention(RUNTIME) public @interface Process { Class<? extends ProcessingStrategy> using() default DefaultImplStrategy.class; } 

Without going into too much scope, each annotated property must have a ProcessStrategy defined on it, hence the use () property in the annotation. This is great and works as I would like.

I also want to indicate the default implementation of the strategy that will be used most of the time (the default value is defined below). This works great in Eclipse.

However, when I try to compile this using a regular JDK (called via maven), I get the following error:

 found : java.lang.Class<DefaultImplStrategy> required: java.lang.Class<? extends ProcessingStrategy> 

I guess this is a combination of generics, annotations, class literature and defaults that are to blame here, but I honestly don't know why. I looked at the rules associated with default values ​​in JLS, and I seem to be breaking nothing.

Given that DefaultImplStrategy definitely implements ProcessingStrategy, what am I doing wrong here?

+6
java generics annotations
source share
2 answers

A short version of this is that some combination of maven, Lombok, and default annotations does not play well together. A longer version on the Lombok mailing list .

The solution is relatively simple: fully qualify the default type ie

 @Target({FIELD}) @Retention(RUNTIME) public @interface Process { Class<? extends ProcessingStrategy> using() default com.example.processing.DefaultImplStrategy.class; } 
+3
source share

I don’t know why, but if you give the full class path to DefaultImplStrategy, it will probably work

0
source share

All Articles