Repeatable annotations one Java <= 7

Alex Buckley alex.buckley at oracle.com
Thu Apr 24 20:56:22 UTC 2014


Assuming you have @Foo(...) on (say) a class declaration, javac needs to 
load the annotation type Foo to check that @Foo's element-value pairs 
are commensurate with the elements declared in Foo. Any meta-annotation 
on Foo will then have its own annotation type loaded; it is unreasonable 
to expect otherwise.

By marking your annotation type as repeatable, you are committing that 
it [and any annotations of that type] will only be compiled on JDK 8 and 
above, where j.l.a.Repeatable can be loaded.

Alex

On 4/24/2014 1:46 PM, Gunnar Morling wrote:
> Hi,
>
> I'd like to mark an annotation type as repeatable via Java 8's
> @Repeatable meta-annotation.
>
> The code accessing the annotation explicitly checks for the container
> annotation, so it works on Java 8 (where the compiler creates an
> instance of the container annotation) and on older versions (where the
> container type must be used explicitly).
>
> Now it's my understanding that annotation types not being present at
> runtime don't cause issues when loading classes annotated with them. But
> at compile time a warning is issued when compiling code using the
> annotation on older Java versions (the compilation still succeeds):
>
>      "Cannot find annotation method 'value()' in type
> 'java.lang.annotation.Repeatable': class file for
> java.lang.annotation.Repeatable not found"
>
> Is there a way for suppressing that specific compiler warning (but keep
> others)? I think this would be helpful to allow library authors making
> existing annotation types repeatable - thus increasing usability on Java
> 8 - while staying compatible with older Java versions in the described way.
>
> Or is this actually a bad pattern and one should create a new repeatable
> variant of such an annotation type and distribute it separately (e.g.
> using a classifier such as "jdk8" in Maven artifact terms)?
>
> Many thanks,
>
> --Gunnar
>


More information about the compiler-dev mailing list