Question on remapping type annotations when casts are discarded by the compiler

Andy Clement andrew.clement at gmail.com
Wed Jul 10 14:04:09 PDT 2013


I'm in one of the corners of the JSR308 spec, Section 3.3.10, which reads:

===

If the compiler eliminates an annotated cast, it is required to retain the
annotations on the cast in the class file (if the annotation type has at
least RetentionPolicy.CLASS retention). When a cast is removed, the
compiler may need to adjust (the locations of) the annotations, to account
for the relationship between the expression’s type and the casted-to type.
Consider:

  class C<S, T> { ... }
  class D<A, B> extends C<B, A> { ... }
  ...
  ... (C<@A1 X, @A2 Y>) myD ...

The compiler may leave out the upcast, but in that case it must record that
@A1 is attached to the second type argument of D, even though it was
originally attached to the first type argument of C.

===
I have this (hopefully sane?) set of declarations:

class C<X,Y,Z> {}

class D<A,B> extends C<B,String,A> {}

D<Integer,Float> d = new D<Integer,Float>();

C<Float,String,Integer> o2 = (C<@Anno1 Float, at Anno2 String, @Anno3
Integer>) d;

If the cast were not discarded, the type anno attributes would look like a
bit like:

@Anno1 TYPE_ARGUMENT(0)
@Anno2 TYPE_ARGUMENT(1)
@Anno3 TYPE_ARGUMENT(2)

But the cast is discarded so I need to remap the annotation positions based
on the spec section above, mapping them to where they exist on D.

@Anno1 becomes TYPE_ARGUMENT(1)
@Anno3 becomes TYPE_ARGUMENT(0)

What happens to @Anno2?

thanks
Andy Clement


More information about the type-annotations-spec-comments mailing list