File: RepeatedUnitTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (312 lines) | stat: -rw-r--r-- 11,525 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug     7154390 8005712 8007278 8004912
 * @summary Unit test for repeated annotation reflection
 *
 * @compile RepeatedUnitTest.java subpackage/package-info.java subpackage/Container.java subpackage/Containee.java subpackage/NonRepeated.java subpackage/InheritedContainee.java subpackage/InheritedContainer.java subpackage/InheritedNonRepeated.java
 * @run main RepeatedUnitTest
 */

import subpackage.*;

import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;

public class RepeatedUnitTest {
    public static void main(String[] args) throws Exception {
        // PACKAGE ANNOTATIONS
        Class c = Class.forName("subpackage.NonRepeated"); // force package "subpackage" load
        Package p = Package.getPackage("subpackage");
        packageNonRepeated(p);
        packageRepeated(p);
        packageContainer(p);

        // INHERITED/NON-INHERITED ON CLASS
        inheritedMe1();
        inheritedMe2();
        inheritedMe3();
        inheritedMe4();

        inheritedMe5();    // ContainerOnSuperSingleOnSub
        inheritedMe6();    // RepeatableOnSuperSingleOnSub
        inheritedMe7();    // SingleAnnoOnSuperContainerOnSub
        inheritedMe8();    // SingleOnSuperRepeatableOnSub


        // CONSTRUCTOR
        checkMultiplier(Me1.class.getConstructor(new Class[0]), 10);

        // FIELD
        checkMultiplier(Me1.class.getField("foo"), 1);

        // METHOD
        checkMultiplier(Me1.class.getDeclaredMethod("mee", (Class<?>[])null), 100);

        // INNER CLASS
        checkMultiplier(Me1.MiniMee.class, 1000);

        // ENUM ELEMENT
        checkMultiplier(Me1.E.class.getField("EE"), 10000);

        // ENUM
        checkMultiplier(Me1.E.class, 100000);
    }

    static void packageNonRepeated(AnnotatedElement e) {
        NonRepeated nr = e.getAnnotation(NonRepeated.class);
        check(nr.value() == 10);

        check(1 == countAnnotation(e, NonRepeated.class));

        nr = e.getAnnotationsByType(NonRepeated.class)[0];
        check(nr.value() == 10);

        check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));
    }

    static void packageRepeated(AnnotatedElement e) {
        Containee c = e.getAnnotation(Containee.class);
        check(c == null);
        check(2 == countAnnotation(e, Containee.class));

        c = e.getAnnotationsByType(Containee.class)[0];
        check(c.value() == 1);
        c = e.getAnnotationsByType(Containee.class)[1];
        check(c.value() == 2);

        check(0 == containsAnnotationOfType(e.getAnnotations(), Containee.class));
    }

    static void packageContainer(AnnotatedElement e) {
        Container cr = e.getAnnotation(Container.class);
        check(null != cr);
        check(1 == containsAnnotationOfType(e.getAnnotationsByType(Container.class), Container.class));
        check(1 == countAnnotation(e, Container.class));
    }

    static void inheritedMe1() {
        AnnotatedElement e = Me1.class;
        check(null == e.getAnnotation(NonRepeated.class));
        check(e.getAnnotation(InheritedNonRepeated.class).value() == 20);
        check(0 == countAnnotation(e, Containee.class));
        check(4 == countAnnotation(e, InheritedContainee.class));
        check(0 == countAnnotation(e, Container.class));
        check(1 == countAnnotation(e, InheritedContainer.class));
    }

    static void inheritedMe2() {
        AnnotatedElement e = Me2.class;
        check(e.getAnnotation(NonRepeated.class).value() == 100);
        check(e.getAnnotation(InheritedNonRepeated.class).value() == 200);
        check(4 == countAnnotation(e, Containee.class));
        check(4 == countAnnotation(e, InheritedContainee.class));
        check(1 == countAnnotation(e, Container.class));
        check(1 == countAnnotation(e, InheritedContainer.class));
        check(1 == countAnnotation(e, NonRepeated.class));
        check(1 == countAnnotation(e, InheritedNonRepeated.class));

        check(e.getAnnotationsByType(Containee.class)[2].value() == 300);
        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 300);
        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 200);
        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 100);
    }

    static void inheritedMe3() {
        AnnotatedElement e = Me3.class;
        check(null == e.getAnnotation(NonRepeated.class));

        check(0 == countAnnotation(e, Containee.class));
        check(4 == countAnnotation(e, InheritedContainee.class));
        check(0 == countAnnotation(e, Container.class));
        check(1 == countAnnotation(e, InheritedContainer.class));

        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 350);
        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 15);
    }

    static void inheritedMe4() {
        AnnotatedElement e = Me4.class;
        check(e.getAnnotation(NonRepeated.class).value() == 1000);
        check(e.getAnnotation(InheritedNonRepeated.class).value() == 2000);
        check(4 == countAnnotation(e, Containee.class));
        check(4 == countAnnotation(e, InheritedContainee.class));
        check(1 == countAnnotation(e, Container.class));
        check(1 == countAnnotation(e, InheritedContainer.class));
        check(1 == countAnnotation(e, NonRepeated.class));
        check(1 == countAnnotation(e, InheritedNonRepeated.class));

        check(e.getAnnotationsByType(Containee.class)[2].value() == 3000);
        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 3000);
        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 2000);
        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 1000);
    }

    static void inheritedMe5() {
        AnnotatedElement e = Me5.class;
        check(2 == e.getAnnotations().length);
        check(1 == countAnnotation(e, InheritedContainee.class));
    }

    static void inheritedMe6() {
        AnnotatedElement e = Me6.class;
        check(2 == e.getAnnotations().length);
        check(1 == countAnnotation(e, InheritedContainee.class));
    }

    static void inheritedMe7() {
        AnnotatedElement e = Me7.class;
        check(2 == e.getAnnotations().length);
        check(2 == countAnnotation(e, InheritedContainee.class));
    }

    static void inheritedMe8() {
        AnnotatedElement e = Me8.class;
        check(2 == e.getAnnotations().length);
        check(2 == countAnnotation(e, InheritedContainee.class));
    }

    static void checkMultiplier(AnnotatedElement e, int m) {
        // Basic sanity of non-repeating getAnnotation(Class)
        check(e.getAnnotation(NonRepeated.class).value() == 5 * m);

        // Check count of annotations returned from getAnnotationsByType(Class)
        check(4 == countAnnotation(e, Containee.class));
        check(1 == countAnnotation(e, Container.class));
        check(1 == countAnnotation(e, NonRepeated.class));

        // Check contents of array returned from getAnnotationsByType(Class)
        check(e.getAnnotationsByType(Containee.class)[2].value() == 3 * m);
        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 5 * m);

        // Check getAnnotation(Class)
        check(e.getAnnotation(Containee.class) == null);
        check(e.getAnnotation(Container.class) != null);

        // Check count of annotations returned from getAnnotations()
        check(0 == containsAnnotationOfType(e.getAnnotations(), Containee.class));
        check(1 == containsAnnotationOfType(e.getAnnotations(), Container.class));
        check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));
    }

    static void check(Boolean b) {
        if (!b) throw new RuntimeException();
    }

    static int countAnnotation(AnnotatedElement e, Class<? extends Annotation> c) {
        return containsAnnotationOfType(e.getAnnotationsByType(c), c);
    }

    static <A extends Annotation> int containsAnnotationOfType(A[] l, Class<? extends Annotation> a) {
        int count = 0;
        for (Annotation an : l) {
            if (an.annotationType().equals(a))
                count++;
        }
        return count;
    }
}

@NonRepeated @InheritedNonRepeated
@InheritedContainee(1) @InheritedContainee(2) @InheritedContainee(3) @InheritedContainee(4)
@Containee(1) @Containee(2) @Containee(3) @Containee(4)
class Grandma {}

class Mother extends Grandma {}

@NonRepeated(5) @InheritedNonRepeated(15)
@InheritedContainee(150) @InheritedContainee(250) @InheritedContainee(350) @InheritedContainee(450)
@Containee(150) @Containee(250) @Containee(350) @Containee(450)
class Father extends Grandma {}

class Me1 extends Mother {

    @NonRepeated(5)
    @Containee(1) @Containee(2) @Containee(3) @Containee(4)
    public String foo = "";

    @NonRepeated(50)
    @Containee(10) @Containee(20) @Containee(30) @Containee(40)
    public Me1() {
    }

    @NonRepeated(500)
    @Containee(100) @Containee(200) @Containee(300) @Containee(400)
    public void mee() {
    }

    @NonRepeated(5000)
    @Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)
    public class MiniMee {}

    @NonRepeated(500000)
    @Containee(100000) @Containee(200000) @Containee(300000) @Containee(400000)
    public enum E {
        @NonRepeated(50000)
        @Containee(10000) @Containee(20000) @Containee(30000) @Containee(40000)
        EE(),
    }
}

@NonRepeated(100) @InheritedNonRepeated(200)
@InheritedContainee(100) @InheritedContainee(200) @InheritedContainee(300) @InheritedContainee(400)
@Containee(100) @Containee(200) @Containee(300) @Containee(400)
class Me2 extends Mother {}

class Me3 extends Father {}

@NonRepeated(1000) @InheritedNonRepeated(2000)
@InheritedContainee(1000) @InheritedContainee(2000) @InheritedContainee(3000) @InheritedContainee(4000)
@Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)
class Me4 extends Father {}


@InheritedContainer({@InheritedContainee(1), @InheritedContainee(2)})
class SuperOf5 {}

@InheritedContainee(3)
class Me5 extends SuperOf5{}


@InheritedContainee(1) @InheritedContainee(2)
class SuperOf6 {}

@InheritedContainee(3)
class Me6 extends SuperOf6 {}


@InheritedContainee(1)
class SuperOf7 {}

@InheritedContainer({@InheritedContainee(2), @InheritedContainee(3)})
class Me7 extends SuperOf7 {}


@InheritedContainee(1)
class SuperOf8 {}

@InheritedContainee(2) @InheritedContainee(3)
class Me8 extends SuperOf8 {}