[type-annos-observers] Desugaring of anonymous classes
Srikanth S Adayapalam
srikanth_sankaran at in.ibm.com
Fri Aug 23 02:33:37 PDT 2013
> From: Alex Buckley <alex.buckley at oracle.com>
> Sent by: type-annotations-spec-experts-bounces at openjdk.java.net
>
> Mike, Srikanth,
>
> Returning to this topic, there are two scenarios where I would like your
> comments:
Hi Alex,
First of all, apologies for the delay in responding.
> 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.
> 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.
> 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.
Srikanth,
More information about the type-annotations-spec-observers
mailing list