1 /*
2  * Copyright (c) 2013, 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 8022343 8007072
27  * @summary Test Class.getAnnotatedSuperclass() returns null/non-null
28  *          AnnotatedType as specified
29  */
30 
31 import java.lang.reflect.AnnotatedType;
32 import java.util.Arrays;
33 
34 public class GetAnnotatedSuperclass {
35     private static final Class<?>[] nullTestData = {
36         Object.class,
37         If.class,
38         Object[].class,
39         void.class,
40         int.class,
41     };
42 
43     private static final Class<?>[] nonNullTestData = {
44         Class.class,
45         GetAnnotatedSuperclass.class,
46         (new If() {}).getClass(),
47         (new Clz() {}).getClass(),
48         (new Object() {}).getClass(),
49     };
50 
51     private static int failed = 0;
52     private static int tests = 0;
53 
main(String[] args)54     public static void main(String[] args) throws Exception {
55         testReturnsNull();
56         testReturnsEmptyAT();
57 
58         if (failed != 0)
59             throw new RuntimeException("Test failed, check log for details");
60         if (tests != 10)
61             throw new RuntimeException("Not all cases ran, failing");
62     }
63 
testReturnsNull()64     private static void testReturnsNull() {
65         for (Class<?> toTest : nullTestData) {
66             tests++;
67 
68             Object res = toTest.getAnnotatedSuperclass();
69 
70             if (res != null) {
71                 failed++;
72                 System.out.println(toTest + ".getAnnotatedSuperclass() returns: "
73                         + res + ", should be null");
74             }
75         }
76     }
77 
testReturnsEmptyAT()78     private static void testReturnsEmptyAT() {
79         for (Class<?> toTest : nonNullTestData) {
80             tests++;
81 
82             AnnotatedType res = toTest.getAnnotatedSuperclass();
83 
84             if (res == null) {
85                 failed++;
86                 System.out.println(toTest + ".getAnnotatedSuperclass() returns 'null' should  be non-null");
87             } else if (res.getAnnotations().length != 0) {
88                 failed++;
89                 System.out.println(toTest + ".getAnnotatedSuperclass() returns: "
90                         + Arrays.asList(res.getAnnotations()) + ", should be an empty AnnotatedType");
91             }
92         }
93     }
94 
95     interface If {}
96 
97     abstract static class Clz {}
98 }
99