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

Anonymous type declarations drop supertype type parameter annotations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8, 9
    • tools
    • b122
    • x86_64
    • linux_ubuntu
    • Verified

      FULL PRODUCT VERSION :
      javac 1.8.0_60

      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Likely platform independent...

      4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

      Ubuntu 12.04.5 LTS - 3.13.0-29-generic

      Windows 7

      A DESCRIPTION OF THE PROBLEM :
      Type annotations placed on type parameters of supertypes are not accessible via reflection. I'm not sure why this is.

      For example, consider the AnnotatedType produced by the following statement:

      new @TestAnnotation ArrayList<@TestAnnotation List<Number>>() {}.getClass().getAnnotatedSuperclass()

      the first annotation will be retained and reflected at runtime, but the second will be unavailable. This only happens with anonymous types.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Simply run the simple test case provided. There are no external dependencies.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both annotations on:

      new @TestAnnotation ArrayList<@TestAnnotation List<Number>>() {}.getClass().getAnnotatedSuperclass()

      should be retained at runtime. The output of the source given below should be:

      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      ACTUAL -
      Only the first annotation on:

      new @TestAnnotation ArrayList<@TestAnnotation List<Number>>() {}.getClass().getAnnotatedSuperclass()

      is retained at runtime. The output of the source given below is:

      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      [@uk.co.strangeskies.reflection.test.FailTest$TestAnnotation()]
      []

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      import java.lang.reflect.AnnotatedParameterizedType;
      import java.lang.reflect.AnnotatedType;
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;

      @SuppressWarnings({ "javadoc", "serial" })
      public class FailTest {
      @Target(ElementType.TYPE_USE)
      @Retention(RetentionPolicy.RUNTIME)
      public @interface TestAnnotation {}

      public class TestClass
      extends @TestAnnotation ArrayList<@TestAnnotation List<Number>> {}

      public void failTest() {
      printAnnotations(TestClass.class.getAnnotatedSuperclass());

      printAnnotations(
      new @TestAnnotation ArrayList<@TestAnnotation List<Number>>() {}
      .getClass().getAnnotatedSuperclass());

      }

      public void printAnnotations(AnnotatedType type) {
      System.out.println(
      Arrays.asList(((AnnotatedParameterizedType) type).getAnnotations()));

      System.out.println(Arrays.asList(
      ((AnnotatedParameterizedType) type).getAnnotatedActualTypeArguments()[0]
      .getAnnotations()));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None known.

            sadayapalam Srikanth Adayapalam (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: