简体   繁体   中英

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?

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 ie

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

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

}

不知道为什么,但是如果你给出了DefaultImplStrategy的完整类路径,它可能会起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM