Use of an annotation without @Target meta-annotation on a module declaration
Michael Ernst
mernst at cs.washington.edu
Fri Sep 20 03:32:32 UTC 2019
I agree that the JLS uses unfortunate wording, and it is a bug that the
wording was not updated for Java 9. We have an opportunity to fix it now.
When an annotation has no @TargetType meta-annotation, here are 5 different
things that it could mean:
* Only Java 7 declaration contexts (not modules).
Rationale: backward compatibility for existing annotations that were
poorly written (without a @Target meta-annotation).
This is the current *intent* of the specification authors (but not what
is written in the specification).
This is implemented in javac.
* All decl contexts except type parameter declarations (this includes
modules)
This is what is currently written in the specification.
No one thinks this is a useful specification.
* All Java 8 contexts except TYPE_USE.
This is another description of what is currently written in the
specification.
It is also not what we want.
* All declaration contexts.
This is definitely better than all of the foregoing.
It is a behavior change from the current specification, but a small one
that affects poorly-written programs that have no @Target meta-annotation.
This is Alex's proposal.
* All contexts.
This is the simplest and clearest specification. It is the most
intuitive and easiest to remember, and therefore least likely to lead to
problems. Those qualities make it attractive.
It is a behavior change from the current specification, but a small one
that affects poorly-written programs that have no @Target meta-annotation.
I propose that we adopt this meaning.
From: Alex Buckley <alex.buckley at oracle.com>
> Date: Thu, Aug 29, 2019 at 9:45 PM
> Subject: Re: Use of an annotation without @Target meta-annotation on a
> module declaration
> To: compiler-dev <compiler-dev at openjdk.java.net>
>
>
> 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 9.6.4.1 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?
>
> Alex
>
> > 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20190919/b231a604/attachment-0001.html>
More information about the compiler-dev
mailing list