Queries about JDK-8035890

Alex Buckley alex.buckley at oracle.com
Fri Mar 28 00:09:17 UTC 2014


Hi Jan,

On 3/27/2014 4:09 PM, Jan Lahoda wrote:
> I have a few more questions on this. Considering these declarations of
> annotation types:
> @Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
> @interface DA { }
> @Target(ElementType.TYPE_USE)
> @interface TA { }
>
> Do I understand correctly that these are supposed to be valid:
> public <T> @DA int foo1() { return 0;}
> public <T> @DA void foo2() { }
> public <T> @TA int foo3() { return 0;}

Yes. The @DA and @TA are part of the MethodDeclaration just like they'd 
be if <T> was not present. So, it reduces to the well-known situation of 
an annotation immediately preceding a type in a method declaration - 
where we have a mechanism for determining whether the annotation is a 
type annotation or a declaration annotation. In other words, the type 
parameter section is invisible for the purpose of ascribing meaning to 
annotations.

> These are supposed to be invalid:
> public class Foo {
>      <T> @DA Foo() { }
>      <T> @TA Foo(int i) { }
> }

Ah, constructor declarations. The EG never discussed allowing 
annotations after the type parameter section there. I guess that was an 
oversight, but as things stand your two ctors are invalid.

> I am unsure about:
> public <T> @TA void foo4() { return 0;}

Illegal. We remembered to handle this one:

"If an annotation appears before a void method declaration and is deemed 
to apply only to the type which is closest to the annotation, a 
compile-time error occurs."

Since TA is TYPE_USE, @TA is deemed to apply only to the type which is 
closest to the annotation - though it turns out to be 'void', so oops. 
All this would apply is @TA appeared before the 'public', since it's 
always been the case that an annotation and the type it means to apply 
to can be separated by other modifiers.

Alex


More information about the type-annotations-dev mailing list