Use of an annotation without @Target meta-annotation on a module declaration

Alex Buckley alex.buckley at
Fri Aug 30 01:44:59 UTC 2019

On 8/29/2019 5:35 PM, Werner Dietl wrote:
> Since Java 8, the JavaDoc for java.lang.annotation.Target contains:
> "If an @Target meta-annotation is not present on an annotation type T,
> then an annotation of type T may be written as a modifier for any
> declaration except a type parameter declaration."
> In Java 9, the additional constant MODULE was added to
> java.lang.annotation.ElementType for module declarations.
> Should an annotation without @Target meta-annotation be usable on a
> module declaration or not?

The API spec of j.l.a.Target agrees with JLS which defines the 
meaning of @Target when it appears (and when it doesn't) on annotation 
type declarations. Without @Target on its declaration, an annotation 
type is applicable on module declarations. Below, ecj is right and javac 
is wrong.

JSR 308 introduced the "all declaration contexts except type parameter 
declarations" rule in an attempt to constrain no- at Target to SE 7 
contexts only. As the Java language gets more kinds of declarations -- 
modules, records, values -- the open-ended wording of the rule ("all") 
is at odds with its closed-ended intent. The ongoing maintenance burden 
for javac and the API spec and the JLS is unwelcome. Frankly, I think 
the SE 7-only intent was misguided; no- at Target should have meant "all 
declaration contexts", end of story, no more threads like this ever 
again. Any objections to changing the rule in a future SE release?


> Take a file
> @pkg.Anno
> module moduletest {}
> and a file pgk/
> package pkg;
> public @interface Anno {}
> The above specification would seem to allow the use of @Anno on
> moduletest and ecj (tested with version 3.16.0) compiles the test case
> above.
> However, javac version 11.0.1 gives the following error:
> error: annotation type not applicable to this kind
> of declaration
> @pkg.Anno
> ^
> 1 error
> The javac error goes away when adding a @Target(ElementType.MODULE)
> meta-annotation.
> Is this a bug in javac or is this a problem with the JavaDoc?
> Best,
> cu, WMD.

More information about the compiler-dev mailing list