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     8027170
27 * @summary getAnnotationsByType needs to take the class hierarchy into account
28 *          when determining which annotations are associated with a given
29 *          class.
30 * @run main InheritedAssociatedAnnotations
31 */
32
33import java.lang.annotation.*;
34import java.lang.reflect.*;
35import java.util.Arrays;
36
37public class InheritedAssociatedAnnotations {
38
39    public static void main(String[] args) {
40        checkAssociated(A3.class);
41        checkAssociated(B3.class);
42        checkAssociated(C3.class);
43        checkAssociated(D3.class);
44    }
45
46    private static void checkAssociated(AnnotatedElement ae) {
47        Ann[] actual = ae.getAnnotationsByType(Ann.class);
48        Ann[] expected = ae.getAnnotation(ExpectedAssociated.class).value();
49
50        if (!Arrays.equals(actual, expected)) {
51            throw new RuntimeException(String.format(
52                    "Test failed for %s: Expected %s but got %s.",
53                    ae,
54                    Arrays.toString(expected),
55                    Arrays.toString(actual)));
56        }
57    }
58
59}
60
61@Retention(RetentionPolicy.RUNTIME)
62@interface ExpectedAssociated {
63    Ann[] value();
64}
65
66
67@Inherited
68@Retention(RetentionPolicy.RUNTIME)
69@Target(ElementType.TYPE)
70@Repeatable(AnnCont.class)
71@interface Ann {
72    int value();
73}
74
75@Inherited
76@Retention(RetentionPolicy.RUNTIME)
77@Target(ElementType.TYPE)
78@interface AnnCont {
79    Ann[] value();
80}
81
82
83@Ann(10)
84class A1 {}
85
86@Ann(20)
87class A2 extends A1 {}
88
89@ExpectedAssociated({@Ann(20)})
90class A3 extends A2 {}
91
92
93@Ann(10) @Ann(11)
94class B1 {}
95
96@Ann(20)
97class B2 extends B1 {}
98
99@ExpectedAssociated({@Ann(20)})
100class B3 extends B2 {}
101
102
103@Ann(10)
104class C1 {}
105
106@Ann(20) @Ann(21)
107class C2 extends C1 {}
108
109@ExpectedAssociated({@Ann(20), @Ann(21)})
110class C3 extends C2 {}
111
112
113@Ann(10) @Ann(11)
114class D1 {}
115
116@Ann(20) @Ann(21)
117class D2 extends D1 {}
118
119@ExpectedAssociated({@Ann(20), @Ann(21)})
120class D3 extends D2 {}
121