[type-annos-observers] Type annotations don't look very Java-like
Alex Buckley
alex.buckley at oracle.com
Thu Sep 19 10:48:59 PDT 2013
On 9/18/2013 8:47 PM, Timo Kinnunen wrote:
> First, please correct me if I'm wrong, but it's my understanding that in
> Java 8 I be able to express
> 1) an annotation on a parameter ONLY
> 2) an annotation on a parameter and its type BOTH
> 3) an annotation on the type of a parameter ONLY
> and that each of these be distinguishable from the others, for
> annotation processing purposes, for example.
1) @Target(ElementType.PARAMETER)
2) @Target({ElementType.PARAMETER, ElementType.TYPE_USE})
3) @Target(ElementType.TYPE_USE)
There is very little syntactic "real estate" in which to annotate formal
parameters. In the case of (2), JSR 308 interprets an annotation as
annotating both the type and the declaration of the parameter. Your use
cases require more expressiveness at the annotation type level, but they
seem rather contrived and no-one brought similar cases to the Expert
Group in over five years. This is not the first time that annotations
have been less flexible than people hoped, and it won't be the last.
> Interestingly, it is possible and required to write @Important
> java.util.List if @Important doesn't have TYPE_USE. Which means adding
> TYPE_USE to an existing annotation in a library can cause compile errors
> when Java 7 code switches to compiling against a Java 8 version of the
> library.
You've found another bug. Adding TYPE_USE to an annotation type's
targets should _not_ break annotations on variable declarations (field,
local, formal parameter) whose types are qualified names, nor on method
declarations whose return types are qualified names.
javac is misinterpreting the package name part of the qualified name as
a "scoping mechanism". From the JSR 308 spec:
@Target({TYPE_USE}) @interface TAnno { }
@Target({FIELD, TYPE_USE}) @interface FTAnno { }
@TAnno java.lang.Object field8; // illegal
@FTAnno java.lang.Object field9; // legal, one field annotation
Alex
More information about the type-annotations-spec-observers
mailing list