Problem with type annotations on a type parameter
Jonathan Gibbons
jonathan.gibbons at oracle.com
Thu Apr 25 11:10:33 PDT 2013
Werner,
Update, good news, and bad news.
I've attached a useful little debugging class for printing out details
of type annotations in a tree.
So, yes, the position is never being set. I also note that since it is
an anno on a type parameter, it /must/ be set before TransTypes, which
is the pass that does erasure. During erasure, all declarations of and
references to type parameters in the tree are removed.
As for a hint as to the cause, it may be related to the way that
JCNewClass works -- within the tree node, there is a "hidden" reference
to the class def for the anon inner class, if any (in field
JCNewClass.def.) This anon inner class is treated specially (e.g.
Attr.visitNewClass), until Lower (Lower.visitNewClass, c. 2850), when it
is "promoted" to being a first-class class def. Note that Lower comes
after TransTypes, so by the time the anon inner class gets promoted, it
will already have been erased.
So, the bad news .. or at least, a complication for you, if you haven't
already considered this ...
I know that some of the type anno positions involve byte code offsets --
i.e. for type annos within a method body. But byte-code offsets are
not computed until Gen, which occurs after erasure (TransTypes). So
what about annotations on type parameters within a method body -- do you
handle them? e.g.
Object m() {
List<@NonNull String> list = new ArrayList<@NonNull String>();
return list;
}
-- Jon
On 04/04/2013 06:02 PM, Werner Dietl wrote:
> Jon, Steve, all,
>
> I'm debugging some strange behavior and I would appreciate a quick
> look by somebody else. I'm probably missing something very obvious.
>
> Take the attached class that contains a class nested within an
> anonymous class.
> Compile with the type-annotations javac and you'll get this output:
>
> ClassWriter: Position UNKNOWN in type annotation: @TA
> ClassWriter: Position UNKNOWN in type annotation: @TA
>
> That's bad, as it means that the type annotation is not written. We
> confirm this by running javap:
>
> javap -v NestedTest\$1\$Inner.class
>
> Nothing on the type parameter.
>
> The strange thing is that this works if we remove the declaration of
> field i1.
> It also works if instead of a method type parameter we declare a
> method parameter, or a class type parameter, or if the class is
> outside of the anonymous class.
>
> Does anybody understand the dependency between field i1 and the type
> parameter X?
>
> Steve: could you expand the tests in
> tools/javac/annotations/typeAnnotations/referenceinfos/ to ensure that
> annotations in anonymous classes and lambdas work as expected?
> Please also add a failing test for this.
>
> Thanks,
> cu, WMD.
>
> --
> http://www.google.com/profiles/wdietl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TypeAnnoPosChecker.java
Type: text/x-java
Size: 1532 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/type-annotations-dev/attachments/20130425/3b4664d3/TypeAnnoPosChecker.java
More information about the type-annotations-dev
mailing list