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

Werner Dietl wdietl at gmail.com
Fri Aug 30 00:35:20 UTC 2019


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?

Take a file module-info.java:

@pkg.Anno
module moduletest {}

and a file pgk/Anno.java:

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:

module-info.java:1: 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.

-- 
https://ece.uwaterloo.ca/~wdietl/


More information about the compiler-dev mailing list