[type-annos-observers] Desugaring of anonymous classes
Alex Buckley
alex.buckley at oracle.com
Fri Aug 23 12:04:56 PDT 2013
Hi Srikanth,
On 8/23/2013 2:33 AM, Srikanth S Adayapalam wrote:
> > 1. Type annotation in 'new' expression for anonymous class
> >
> > Consider this code:
> >
> > @Retention(RetentionPolicy.RUNTIME)
> > @Target(ElementType.TYPE_USE)
> > @interface X {}
> >
> > class C { void m() { new @X Foo() {}; } }
> >
> > The @X annotation is stored as a target_type=CLASS_EXTENDS structure in
> > the RuntimeVisibleTypeAnnotations attribute of Foo$1.class. That's fine.
>
> I am fine with this behavior, though as there is nothing in the latest
> specification that calls for such a behavior, we should make sure to
> spell this out, if this is the behavior we want.
Foo is, of course, the direct superclass of the anonymous class (JLS
15.9.1), but the 'extends' clause is hard to see in the code above. The
wording for target_type=CLASS_EXTENDS (0x10) refers to a "type in
extends clause of class or interface declaration" so it's not clear that
the @X annotation should be stored this way. To clarify, the wording for
target_type=CLASS_EXTENDS should say:
"type in extends clause of class or interface declaration (including the
direct superclass of an anonymous class declaration), ..."
> > The @X annotation is not stored as a target_type=NEW structure in the
> > RuntimeVisibleTypeAnnotations attribute of m's method_info in C.class.
> > That's an oversight by javac, especially since the first mail in this
> > thread said javac copied type annotations "to the class declaration and
> > to its instantiation (new expression)".
>
> At the moment, eclipse compiler's behavior is the exact opposite of what
> you mention as javac's behavior. We emit the annotation with target_type
> being NEW but not target_type=CLASS_EXTENDS.
I will file a bug for javac to emit the target_type=NEW attribute. Then
if ecj can emit the target_type=CLASS_EXTENDS, everything will be good.
> > 2. Declaration annotation for anonymous class in 'new' expression
> >
> > Take the code above and change the annotation type's target to be TYPE:
> >
> > @Retention(RetentionPolicy.RUNTIME)
> > @Target(ElementType.TYPE)
> > @interface X {}
> >
> > class C { void m() { new @X Foo() {}; } }
> >
> > The @X annotation is stored in the RuntimeVisibleAnnotations structure
> > of Foo$1.class. This is a neat trick, since anonymous class declarations
> > could not be annotated in SE 7, but it is irregular because annotations
> > on declarations traditionally appear as modifiers. If a 'new' expression
>
> This is more irregular than neat and I would be inclined to suggest that
> we pull back such behavior. At the moment again, as you call out there is
> nothing in the specification that calls for such a behavior.
Thanks for this comment. I will file a bug for javac to remove the
RuntimeVisibleAnnotations attribute. In the recent JLS/JVMS draft for
JSR 308, the above code is illegal: a 'new' expression is a "type
context", and "It is a compile-time error if an annotation of type T
decorates a type in a type context (4.11), but T is not applicable to
type uses." ... the annotation type X is not applicable to type uses
because "The constants ... TYPE ... correspond to the declaration
contexts ...".
Alex
More information about the type-annotations-spec-observers
mailing list