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

j.l.Class.getAnnotationsByType behavior still is incorrect

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • None
    • core-libs

      Tested with b112.

      Minimized test:
      -------------------------------------------------------------------------------
      public class Test {

          public static void main(String[] args) {
              // From spec: Namely, @Foo(3) on B is deemed to "override" every @Foo annotation on A. This policy is
              // reified by storing @Foo(1) @Foo(2) inside a container annotation, so they are effectively
              // hidden when B is queried for inherited @Foo annotations.
              // (Foo is repeatable with FooContainer and inheritable)
              Foo[] result1 = B.class.getAnnotationsByType(Foo.class);
              System.out.println("B.class.getAnnotationsByType(Foo.class) count - " + result1.length + ", should be 1 - only [ @Foo(3) ]");

              // From the spec: The contained @Foo annotations on class B override the uncontained
              // @Foo(0) annotation on class A which would otherwise be inherited by class B (since Foo is inheritable).
              // (Foo is repeatable with FooContainer and inheritable)
              Foo[] result2 = D.class.getAnnotationsByType(Foo.class);
              System.out.println("D.class.getAnnotationsByType(Foo.class) count - " + result2.length + ", should be 2 - only [ @Foo(1), @Foo(2) ]");
              Foo[] result3 = E.class.getAnnotationsByType(Foo.class);
              System.out.println("E.class.getAnnotationsByType(Foo.class) count - " + result3.length + ", should be 2 - only [ @Foo(1), @Foo(2) ]");
          }
      }

      @Retention(RetentionPolicy.RUNTIME)
      @Repeatable(FooCont.class)
      @Inherited
      @interface Foo {
          int value();
      }

      @Retention(RetentionPolicy.RUNTIME)
      @Inherited
      @interface FooCont {
          Foo[] value();
      }

      @Foo(1) @Foo(2) class A {}
      @Foo(3) class B extends A {}


      @Foo(0) class C {}
      @Foo(1) @Foo(2) class D extends C {}
      @FooCont({@Foo(1), @Foo(2)}) class E extends C {}
      -------------------------------------------------------------------------------

      Output:
      -------------------------------------------------------------------------------
      B.class.getAnnotationsByType(Foo.class) count - 3, should be 1 - only [ @Foo(3) ]
      D.class.getAnnotationsByType(Foo.class) count - 3, should be 2 - only [ @Foo(1), @Foo(2) ]
      E.class.getAnnotationsByType(Foo.class) count - 3, should be 2 - only [ @Foo(1), @Foo(2) ]
      -------------------------------------------------------------------------------

            alundblad Andreas Lundblad (Inactive)
            evotchen Elena Votchennikova (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: