Constructor receiver annotations

Werner Dietl wdietl at gmail.com
Tue Nov 26 14:08:38 PST 2013


Joel, Steve, all,

there has been some discussion about this issue:

https://bugs.openjdk.java.net/browse/JDK-8029012

Let us take this example:

class Outer {
    class Middle {
        class Inner {
            Inner(@TD int p1) {}
            @TC Inner(@TA Outer. at TB Middle Middle.this, @TD Object p2) {}
        }
    }
}

Complete example attached.

The relevant part of the javap output for the first constructor is:

  Outer$Middle$Inner(Outer$Middle, int);
    descriptor: (LOuter$Middle;I)V
    flags:
    Code:
    ...
    RuntimeInvisibleTypeAnnotations:
      0: #15(): METHOD_FORMAL_PARAMETER, param_index=0

As the bug notes, the param_index doesn't match the method descriptor:
the annotation is about the int-parameter, which after creating the
outer-this parameter has index 1.

For the second constructor:

  Outer$Middle$Inner(Outer$Middle, java.lang.Object);
    descriptor: (LOuter$Middle;Ljava/lang/Object;)V
    flags:
    Code:
    ...
    RuntimeInvisibleTypeAnnotations:
      0: #17(): METHOD_RETURN, location=[INNER_TYPE, INNER_TYPE]
      1: #18(): METHOD_RECEIVER, location=[INNER_TYPE]
      2: #19(): METHOD_RECEIVER
      3: #15(): METHOD_FORMAL_PARAMETER, param_index=0

Again, the param_index should be 1.

However, the annotations that apply to the METHOD_RECEIVER actually
also apply to param_index 0 - because in cases like this, the receiver
parameter is explicit.

What should the bytecode look like?
Should the annotations be duplicated on param_index 0?

    RuntimeInvisibleTypeAnnotations:
      0: #17(): METHOD_RETURN, location=[INNER_TYPE, INNER_TYPE]
      1: #18(): METHOD_RECEIVER, location=[INNER_TYPE]
      2: #19(): METHOD_RECEIVER
      3: #18(): METHOD_FORMAL_PARAMETER, param_index=0, location=[INNER_TYPE]
      4: #19(): METHOD_FORMAL_PARAMETER, param_index=0
      5: #15(): METHOD_FORMAL_PARAMETER, param_index=1

Or should they only be present on param_index 0?
What does reflection return in this case?

What should happen if the bytecode is inconsistent about this, e.g.
the METHOD_RECEIVER contains some annotations and param_index 0
contains different annotations?

Is this clearly specified and I'm misreading this?

Thoughts?
cu, WMD.

-- 
http://www.google.com/profiles/wdietl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: T8029012.java
Type: text/x-java
Size: 407 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/type-annotations-dev/attachments/20131126/522e5be1/T8029012.java 


More information about the type-annotations-dev mailing list