Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8202471

(ann) Cannot read type annotations on generic receiver type's type variables

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The type annotations on a generic receiver type's type variables are not readable via the reflection API despite this information being present in the class file. It is neither possible to read type annotations of an owner type, if existing.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example code for a demonstration, there is simply no API present.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should be possible to receive the type annotation on the type variable within the code example what is currently impossible as the receiver type is treated as a non-parameterized type.

      Instead, the returned receiver type should be an instance of ParameterizedType and the annotated type an instance of AnnotatedParameterizedType such that annotations of an owner type or on type parameters can be read.

      All the necessary information is present in the class file.
      ACTUAL -
      The parameterized type is treated as a non-generic type.

      ---------- BEGIN SOURCE ----------
      public class Foo<T> {

          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.TYPE_USE)
          @interface SampleTypeVariable { }

          void foo(Foo<@SampleTypeVariable T>this) { }

          public static void main(String[] args) throws Exception {
              Method foo = Foo.class.getDeclaredMethod("foo");

              AnnotatedType receiverType = foo.getAnnotatedReceiverType();
              // No chance to find out that the parameter type T was annotated, despite the annotation being present in the class file.
              System.out.println(receiverType.getType() instanceof ParameterizedType);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None.

      FREQUENCY : always


            jfranck Joel Borggrén-Franck (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: