开发者

Using a default class literal value on an annotation

开发者 https://www.devze.com 2023-01-13 11:14 出处:网络
I want to annotate some of the fields of a given bean class with the following annotation: @Target({FIELD})

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 D开发者_C百科efaultImplStrategy.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?


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;

}


Don't know why, but if you give the full class path to DefaultImplStrategy it will probably work

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号