Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a default class literal value on an annotation

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

@Target({FIELD})
@Retention(RUNTIME)
public @interface Process {

    Class<? extends ProcessingStrategy> using() default DefaultImplStrategy.class;

}

Without going into the domain too much, each annotated property needs to have a ProcessingStrategy defined on it, hence the using() property on the annotation. That's fine and works the way I'd like it to.

I also want to specify a default implementation of the strategy, to be used most of the time (the default defined below). This works fine in Eclipse.

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

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

I'm guessing it's some combination of generics, annotations, class literals and defaulting that are at fault here but I honestly do not know why. I've had a look at the rules around default values in the JLS and I don't seem to be violating anything.

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

like image 244
GaryF Avatar asked Aug 24 '10 15:08

GaryF


People also ask

How to give default Value for@ Value?

To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.

What is the use of @interface annotation?

The @interface element is used to declare an annotation.

What is the annotation used to define attributes for an element?

We can also explicitly specify the attributes in a @Test annotation. Test attributes are the test specific, and they are specified at the right next to the @Test annotation.


1 Answers

The short version of this is that some combination of maven, Lombok and default annotations don't play nicely together. The longer version is on the Lombok mailing list.

The solution is relatively simple: fully qualify the default Type i.e.

@Target({FIELD})
@Retention(RUNTIME)
public @interface Process {

    Class<? extends ProcessingStrategy> using() default com.example.processing.DefaultImplStrategy.class;

}
like image 71
GaryF Avatar answered Nov 01 '22 12:11

GaryF