[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-type-annotations-spec
Subject:    Re: [type-annos-observers] Annotations on anonymous classes
From:       Alex Buckley <alex.buckley () oracle ! com>
Date:       2013-02-13 1:43:46
Message-ID: 511AEFD2.2030506 () oracle ! com
[Download RAW message or body]

Hi Werner,

Thanks for writing to the -spec-comments list.

On 2/12/2013 4:17 PM, Werner Dietl wrote:
> I have a question about anonymous classes and want to make sure we're
> all on the same page on where type and declaration annotations should
> go.
>
> Let's take this code:
>
>      interface Bound<X extends Object> {}
>
>      void test() {
>          new Bound<String>() { };
>      }
>
> This is equivalent to having:
>
>      interface Bound<X extends Object> {}
>
>      class Anon extends Object implements Bound<String> {}
>
>      void test() {
>          new Anon();
>      }
>
> Now, let's take a declaration annotation @DA and type annotations @TA
> and @TB, and use them like this:
>
>      interface Bound<X extends Object> {}
>
>      void test() {
>          new @DA @TA Bound<@TB String>() { };
>      }
>
> This should be translated to:
>
>      interface Bound<X extends Object> {}
>
>      @DA
>      class Anon extends Object implements @TA Bound<@TB String> {}
>
>      void test() {
>          new @TA Anon();
>      }

The JSR 308 spec has a problem: the 'Creator' production in JLS ch.18 
does not use 'Type', so it is unaffected by JSR 308's addition of 
'Annotations' to 'Type'. In other words, no annotations after 'new' are 
permitted by the spec! Obviously they should be, if their type has TYPE 
or TYPE_USE in @Target.

> Note that I have @TA both on the implemented interface and on the
> instantiation and @DA only on the class declaration.

The JSR 308 should say how these annotations are distributed over Anon, 
since Anon's existence and form are specified by JLS 15.9.1, 15.9.5, and 
13.1.

The application of @DA to the class declaration looks good.

The application of @TA to the type mentioned in the anonymous class 
declaration also looks good. No need to sprinkle @TA over Object.

I assume that if Bound was a class, then 'new @TA Bound<..>() { }' would 
cause @TA to appear on the superclass.

> Also note how @TB is not stored at the instantiation at all (this
> might be surprising; if Bound were a class an instantiation "new @TA
> Bound<@TB String>()" would obviously store both @TA and @TB with the
> instantiation).

My answer here is that @TA should not be left in 'new @TA Anon()'. An 
anonymous class declaration within a class instance creation expression 
has a unique binary name. Therefore, tools which seek annotations on a 
class instance creation expression can easily recognize that "an 
anonymous class was instantiated here", and find the corresponding class 
declaration with potential type-decl _and_ type-use annotations.

I suspect moving _all_ type-decl and type-use annotations to the 
implicitly-declared Anon class is simpler to implement too, versus 
leaving some of them back at the creation expression.

Alex
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic