6

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
GaryF
  • 23,950
  • 10
  • 60
  • 73
  • What is the JDK version? – Péter Török Aug 24 '10 at 15:57
  • In IntelliJ with jdk1.5.0_19 it compiles fine. – Péter Török Aug 24 '10 at 16:10
  • Hmm... It works in Eclipse and IntelliJ, and I've just tried something similar (I don't have the original code to hand) using command-line javac and it works fine. I can only assume that the build process is going wrong somewhere. I'll look at it tomorrow and post what I figure out. – GaryF Aug 24 '10 at 22:35
  • Looks somewhat related to: http://www.mail-archive.com/users@maven.apache.org/msg108865.html ... but still not found the specifics. – GaryF Aug 25 '10 at 09:11
  • Small update: it looks like Project Lombok is involved. If I remove Lombok from the project entirely, Maven compiles. If I don't, it gives the error above. Shot in the dark here is that the lombok annotation processor is not being picked up correctly using javac. – GaryF Sep 09 '10 at 13:47

2 Answers2

3

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;

}
GaryF
  • 23,950
  • 10
  • 60
  • 73
  • It's also happening in my ant-based project which doesn't use Lombok, so it must be a problem with the jdk itself – Aldo Aug 17 '11 at 10:38
  • @Aldo while I don't need this myself now (beyond the workaround I have), I'd love to hear what the issue is if you figure it out. – GaryF Aug 17 '11 at 20:44
0

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

btoddb
  • 9
  • 1