[type-annos-observers] type annotations on generic method return type
Alex Buckley
alex.buckley at oracle.com
Thu Feb 6 15:58:15 PST 2014
Second of two mails. No real surprises here.
On 2/6/2014 8:58 AM, Markus Keller wrote:
> What about a generic method declaration, where a type parameter
> declaration "<T>" separates the method modifiers from the return type's
> type annotations?
>
> public @A <T> @B Set<T> foo(T t) { /*...*/ }
>
> => If @A is a TYPE_USE annotation, is it also considered a type annotation
> in this case? Although its syntactic location does _not_ make it apply to
> the return type?
@A is unambiguously parsed as a MethodModifier, but (ignoring the type
parameter section) it could plausibly apply to the declaration of method
foo or the return type of method foo. Which is it? JLS 9.7.4 says:
"If the annotation's type is applicable in type contexts, and not in the
declaration context corresponding to the declaration, then the
annotation is deemed to apply only to the type which is closest to the
annotation."
So, assuming the declaration of A has just @Target(TYPE_USE), @A applies
only to the closest type, Set<T>. (Again ignoring the type parameter
section.) In turn, that makes @A a type annotation:
"A type annotation is an annotation that applies to a type (or any part
of a type), and whose own type is applicable in type contexts (§4.11)."
> BTW: Note that term "closest" in "the type which is closest to the
> annotation" is not defined by the spec. Likewise, type annotations on
> array types are only defined in an informal section.
> "Closest" seems to mean something like "the next following type reference
> that is not in an other annotation (and not in a type parameter
> declaration)".
I don't think "closest" is terribly confusing, even with a type
parameter section, but I agree the text can do better:
"If the annotation's type is applicable in type contexts, and not in the
declaration context corresponding to the declaration, then the
annotation is deemed to apply only to _the type of the declared entity_."
Alex
More information about the type-annotations-spec-observers
mailing list