RFR: JDK-8198945: Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter

Werner Dietl wdietl at gmail.com
Tue Jul 3 18:31:31 UTC 2018


Thanks for the quick fix!
The `old.position.location.isEmpty()` doesn't quite do what we would
consider a "top-level" annotation.
Consider:

```
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface Foo {
  int value();
}

class OBox<T> {}

class JDK8198945 {

  class IBox<T> {}

  public void f() {
    new @Foo(1) OBox<@Foo(2) Object>() { };
    new @Foo(3) IBox<@Foo(4) String>() { };
  }
}
```

8198945/webrev.02 only creates the NEW annotation for @Foo(1), but not
for @Foo(3), because the latter has an INNER_TYPE location.
Maybe the right check is that old.position.location doesn't contain
any TYPE_ARGUMENT ?

Minor: in src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java
I would remove the now out-of-date TODO comment:

@@ -1332,7 +1334,6 @@
                 copyNewClassAnnotationsToOwner(tree);
                 // classdecl.sym.type now contains an annotated type, which
                 // is not what we want there.
-                // TODO: should we put this type somewhere in the
superclass/interface?
                 classdecl.sym.type = before;
             }

Best,
cu, WMD.

On Mon, Jul 2, 2018 at 10:34 PM Liam Miller-Cushon <cushon at google.com> wrote:
>
> On Mon, Jul 2, 2018 at 7:14 PM Werner Dietl <wdietl at gmail.com> wrote:
>>
>> Class Test$1 has no type arguments, so I would find having NEW type
>> annotations on a type argument confusing.
>
>
> Ah, right. Thanks for the catch.
>
>>
>> So I would be for either no NEW type annotations for anonymous class
>> instantiations, or duplicating only the top-level type annotations.
>> For method f() this would generate three type annotations:
>>
>>       RuntimeInvisibleTypeAnnotations:
>>         0: #22(): NEW, offset=9
>>           Test$TA
>>         1: #23(): NEW, offset=9, location=[TYPE_ARGUMENT(0)]
>>           Test$TB
>>         2: #22(): NEW, offset=0
>>           Test$TA
>>
>> Duplicating only the top-level type annotations would be my slight
>> preference over no NEW annotations at all.
>
>
> I revised the fix to only duplicate top-level annotations:
> http://cr.openjdk.java.net/~cushon/8198945/webrev.02



-- 
http://www.google.com/profiles/wdietl


More information about the compiler-dev mailing list