1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8146167
  27  * @summary Anonymous type declarations drop supertype type parameter annotations
  28  * @run main AnonymousExtendsTest
  29  */
  30 
  31 import java.lang.annotation.ElementType;
  32 import java.lang.annotation.Retention;
  33 import java.lang.annotation.RetentionPolicy;
  34 import java.lang.annotation.Target;
  35 import java.lang.reflect.AnnotatedParameterizedType;
  36 import java.lang.reflect.AnnotatedType;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 
  41 @SuppressWarnings({ "javadoc", "serial" })
  42 public class AnonymousExtendsTest {
  43 
  44     @Target(ElementType.TYPE_USE)
  45     @Retention(RetentionPolicy.RUNTIME)
  46     public @interface TA {
  47         int value();
  48     }
  49 
  50     public class TestClass extends @TA(1) ArrayList<@TA(2) List<Number>> {
  51     }
  52 
  53     public void testIt() {
  54         checkAnnotations(TestClass.class.getAnnotatedSuperclass(),
  55               "[@AnonymousExtendsTest$TA(1)],[@AnonymousExtendsTest$TA(2)]");
  56         checkAnnotations(new @TA(3) ArrayList<@TA(4) List<Number>>() {
  57                          }.getClass().getAnnotatedSuperclass(),
  58               "[@AnonymousExtendsTest$TA(3)],[@AnonymousExtendsTest$TA(4)]");
  59     }
  60 
  61     public void checkAnnotations(AnnotatedType type, String expected) {
  62         String actual = Arrays.asList(((AnnotatedParameterizedType) type)
  63                                       .getAnnotations())
  64                                       .toString()
  65                                        + "," +
  66                         Arrays.asList(((AnnotatedParameterizedType) type)
  67                                        .getAnnotatedActualTypeArguments()[0].getAnnotations())
  68                                        .toString();
  69 
  70         if (!actual.equals(expected))
  71             throw new AssertionError("Unexpected annotations" + actual);
  72     }
  73 
  74     public static void main(String[] args) {
  75         new AnonymousExtendsTest().testIt();
  76     }
  77 }