test/tools/javap/output/RepeatingTypeAnnotations.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
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 8005220
27 * @summary javap must display repeating annotations
28 */
29 import java.io.*;
30 import java.util.*;
31
32 /**
33 * This class extends the abstract {@link Tester} test-driver, and
34 * encapusulates a number of test-case classes (i.e. classes extending
35 * this class and annotated with {@code TestCase}).
36 * <p>
37 * By default (no argument), this test runs all test-cases, except
38 * if annotated with {@code ignore}.
39 * <p>
40 * Individual test cases can be executed using a run action.
41 * <p>
42 * Example: @run main RepeatingTypeAnnotations RepeatingTypeAnnotations$TC4
43 * <p>
44 * Note: when specific test-cases are run, additional debug output is
45 * produced to help debugging. Test annotated with {@code ignore}
46 * can be executed explicitly.
47 */
48 public class RepeatingTypeAnnotations extends Tester {
49
50 /**
51 * Main method instantiates test and run test-cases.
52 */
53 public static void main(String... args) throws Exception {
54 Tester tester = new RepeatingTypeAnnotations();
55 tester.run(args);
56 }
57
58 /**
59 * Testcases are classes extending {@code RepeatingTypeAnnotations},
60 * and calling {@link setSrc}, followed by one or more invocations
61 * of {@link verify} in the body of the constructor.
62 */
63 public RepeatingTypeAnnotations() {
64 setSrc(new TestSource(template));
65 }
66
67 /**
68 * Common template for test cases. The line TESTCASE is
69 * replaced with the specific lines of individual tests.
70 */
71 private static final String[] template = {
72 "import java.lang.annotation.*;",
73 "class Test {",
74 " @Repeatable(As.class)",
75 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
76 " @Retention(RetentionPolicy.CLASS)",
77 " @interface A {",
78 " Class f() default int.class;",
79 " }",
80
81 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
82 " @Retention(RetentionPolicy.CLASS)",
83 " @interface As { A[] value(); }",
84
85 " @Repeatable(Bs.class)",
86 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
87 " @Retention(RetentionPolicy.CLASS)",
88 " @interface B {",
89 " Class f() default int.class;",
90 " }",
91
92 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
93 " @Retention(RetentionPolicy.CLASS)",
94 " @interface Bs { B[] value(); }",
95
96 " @Repeatable(Cs.class)",
97 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
98 " @Retention(RetentionPolicy.RUNTIME)",
99 " @interface C {",
100 " Class f() default int.class;",
101 " }",
102
103 " @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})",
104 " @Retention(RetentionPolicy.RUNTIME)",
105 " @interface Cs { C[] value(); }",
106 "TESTCASE",
107 "}"
108 };
109
110 /*
111 * The test cases covers annotation in the following locations:
112 * - static and non-static fields
113 * - local variables
114 * - constructor and method return type and parameter types
115 * - casts in class and method contexts.
116 * For the above locations the test-cases covers:
117 * - single annotation type
118 * - two annotation types with same retention
119 * - two annotation types with different retention
120 * - three annotation types, two of same retention, one different.
121 */
122
123 @TestCase
124 public static class TC1 extends RepeatingTypeAnnotations {
125 public TC1() {
126 setSrc(" /* TC1 */ ",
127 " static String so = \"hello world\";",
128 " public @A @A @A Object o = (@A @A @A String) Test.so;");
129 verify("RuntimeInvisibleTypeAnnotations",
130 "0: #25(#26=[@#27(),@#27(),@#27()]): FIELD",
131 "0: #25(#26=[@#27(),@#27(),@#27()]): CAST, offset=5, type_index=0");
132 }
133 }
134
135 @TestCase
136 public static class TC2 extends RepeatingTypeAnnotations {
137 public TC2() {
138 setSrc(" /* TC2 */ ",
139 " static String so = \"hello world\";",
140 " public @A @B @A Object o = (@B @A @B String) Test.so;");
141 verify("RuntimeInvisibleTypeAnnotations",
142 "0: #25(#26=[@#27(),@#27()]): FIELD",
143 "1: #28(): FIELD",
144 "0: #36(#26=[@#28(),@#28()]): CAST, offset=5, type_index=0",
145 "1: #27(): CAST, offset=5, type_index=0");
146 }
147 }
148
149 @TestCase
150 public static class TC3 extends RepeatingTypeAnnotations {
151 public TC3() {
152 setSrc(" /* TC3 */ ",
153 " static String so = \"hello world\";",
154 " public @A @A @C Object o = (@B @C @B String) Test.so;");
155 verify("RuntimeVisibleTypeAnnotations",
156 "RuntimeInvisibleTypeAnnotations",
157 "0: #25(): FIELD",
158 "0: #27(#28=[@#29(),@#29()]): FIELD",
159 "0: #25(): CAST, offset=5, type_index=0",
160 "0: #37(#28=[@#38(),@#38()]): CAST, offset=5, type_index=0");
161 }
162 }
163
164 @TestCase
165 public static class TC4 extends RepeatingTypeAnnotations {
166 public TC4() {
167 setSrc(" /* TC4 */ ",
168 " static String so = \"hello world\";",
169 " public @A @B @C Object o = (@C @B @A String) Test.so;");
170 verify("RuntimeInvisibleTypeAnnotations",
171 "RuntimeVisibleTypeAnnotations",
172 "0: #25(): FIELD",
173 "0: #27(): FIELD",
174 "1: #28(): FIELD",
175 "0: #25(): CAST, offset=5, type_index=0",
176 "0: #28(): CAST, offset=5, type_index=0",
177 "1: #27(): CAST, offset=5, type_index=0");
178 }
179 }
180
181 @TestCase
182 public static class TC5 extends RepeatingTypeAnnotations {
183 public TC5() {
184 setSrc(" /* TC5 */ ",
185 " static String so = \"hello world\";",
186 " public static @A @A @A Object o = (@B @B @B String) Test.so;");
187 verify("RuntimeInvisibleTypeAnnotations",
188 "0: #25(#26=[@#27(),@#27(),@#27()]): FIELD",
189 "0: #36(#26=[@#37(),@#37(),@#37()]): CAST, offset=5, type_index=0");
190 }
191 }
192
193 @TestCase
194 public static class TC6 extends RepeatingTypeAnnotations {
195 public TC6() {
196 setSrc(" /* TC6 */ ",
197 " static String so = \"hello world\";",
198 " public static @A @B @A Object o = (@B @A @B String) Test.so;");
199 verify("RuntimeInvisibleTypeAnnotations",
200 "0: #25(#26=[@#27(),@#27()]): FIELD",
201 "1: #28(): FIELD",
202 "0: #37(#26=[@#28(),@#28()]): CAST, offset=5, type_index=0",
203 "1: #27(): CAST, offset=5, type_index=0");
204 }
205 }
206
207 @TestCase
208 public static class TC7 extends RepeatingTypeAnnotations {
209 public TC7() {
210 setSrc(" /* TC7 */ ",
211 " static String so = \"hello world\";",
212 " public static @A @A @C Object o = (@B @C @B String) Test.so;");
213 verify("RuntimeVisibleTypeAnnotations",
214 "RuntimeInvisibleTypeAnnotations",
215 "0: #25(): FIELD",
216 "0: #27(#28=[@#29(),@#29()]): FIELD",
217 "0: #25(): CAST, offset=5, type_index=0",
218 "0: #38(#28=[@#39(),@#39()]): CAST, offset=5, type_index=0");
219 }
220 }
221
222 @TestCase
223 public static class TC8 extends RepeatingTypeAnnotations {
224 public TC8() {
225 setSrc(" /* TC8 */ ",
226 " static String so = \"hello world\";",
227 " public static @A @B @C Object o = (@C @B @A String) Test.so;");
228
229 verify("RuntimeVisibleTypeAnnotations",
230 "RuntimeInvisibleTypeAnnotations",
231 "0: #25(): FIELD",
232 "0: #27(): FIELD",
233 "1: #28(): FIELD",
234 "0: #25(): CAST, offset=5, type_index=0",
235 "0: #28(): CAST, offset=5, type_index=0",
236 "1: #27(): CAST, offset=5, type_index=0");
237 }
238 }
239
240 @TestCase
241 public static class TC9 extends RepeatingTypeAnnotations {
242 public TC9() {
243 setSrc(" /* TC9 */ ",
244 " public Test(@A @A @A Object o, @A int i, long l) {",
245 " @A @A @A String ls = (@B @B @B String) o;",
246 " }");
247 verify("RuntimeInvisibleTypeAnnotations",
248 "0: #34(#35=[@#36(),@#36(),@#36()]): CAST, offset=4, type_index=0",
249 "1: #37(#35=[@#38(),@#38(),@#38()]): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
250 "RuntimeInvisibleTypeAnnotations",
251 "0: #37(#35=[@#38(),@#38(),@#38()]): METHOD_FORMAL_PARAMETER, param_index=0",
252 "1: #38(): METHOD_FORMAL_PARAMETER, param_index=1");
253 }
254 }
255
256 @TestCase
257 public static class TC10 extends RepeatingTypeAnnotations {
258 public TC10() {
259 setSrc(" /* TC10 */ ",
260 " public Test(@A @A @B Object o, @A @B int i, long l) {",
261 " @A @A @B String ls = (@B @A @B String) o;",
262 " }");
263 verify("RuntimeInvisibleTypeAnnotations",
264 "0: #34(#35=[@#36(),@#36()]): CAST, offset=4, type_index=0",
265 "1: #37(): CAST, offset=4, type_index=0",
266 "2: #38(#35=[@#37(),@#37()]): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
267 "3: #36(): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
268 "RuntimeInvisibleTypeAnnotations",
269 "0: #38(#35=[@#37(),@#37()]): METHOD_FORMAL_PARAMETER, param_index=0",
270 "1: #36(): METHOD_FORMAL_PARAMETER, param_index=0",
271 "2: #37(): METHOD_FORMAL_PARAMETER, param_index=1",
272 "3: #36(): METHOD_FORMAL_PARAMETER, param_index=1");
273 }
274 }
275
276 @TestCase
277 public static class TC11 extends RepeatingTypeAnnotations {
278 public TC11() {
279 setSrc(" /* TC11 */ ",
280 " public Test(@C @C @A Object o, @A @B int i, long l) {",
281 " @C @C @A String ls = (@A @A @C String) o;",
282 " }");
283 verify("RuntimeVisibleTypeAnnotations",
284 "RuntimeInvisibleTypeAnnotations",
285 "0: #34(): CAST, offset=4, type_index=0",
286 "1: #35(#36=[@#34(),@#34()]): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
287 "0: #38(#36=[@#39(),@#39()]): CAST, offset=4, type_index=0",
288 "1: #39(): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
289 "0: #35(#36=[@#34(),@#34()]): METHOD_FORMAL_PARAMETER, param_index=0",
290 "0: #39(): METHOD_FORMAL_PARAMETER, param_index=0",
291 "1: #39(): METHOD_FORMAL_PARAMETER, param_index=1",
292 "2: #40(): METHOD_FORMAL_PARAMETER, param_index=1");
293 }
294 }
295
296 @TestCase
297 public static class TC12 extends RepeatingTypeAnnotations {
298 public TC12() {
299 setSrc(" /* TC12 */ ",
300 " public Test(@A @B @C Object o, @A @C int i, long l) {",
301 " @A @B @C String ls = (@C @A @B String) o;",
302 " }");
303 verify("RuntimeVisibleTypeAnnotations",
304 "0: #34(): CAST, offset=4, type_index=0",
305 "1: #34(): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
306 "RuntimeInvisibleTypeAnnotations",
307 "0: #36(): CAST, offset=4, type_index=0",
308 "1: #37(): CAST, offset=4, type_index=0",
309 "2: #36(): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
310 "3: #37(): LOCAL_VARIABLE, {start_pc=10, length=1, index=5}",
311 "0: #34(): METHOD_FORMAL_PARAMETER, param_index=0",
312 "1: #34(): METHOD_FORMAL_PARAMETER, param_index=1",
313 "0: #36(): METHOD_FORMAL_PARAMETER, param_index=0",
314 "1: #37(): METHOD_FORMAL_PARAMETER, param_index=0",
315 "2: #36(): METHOD_FORMAL_PARAMETER, param_index=1");
316 }
317 }
318
319 @TestCase
320 public static class TC13 extends RepeatingTypeAnnotations {
321 public TC13() {
322 setSrc(" /* TC13 */ ",
323 " public @A @A @A String foo(@A @A @A Object o, @A int i, long l) {",
324 " @A @A @A String ls = (@B @B @B String) o;",
325 " return (@A @A @A String) o;",
326 " }");
327 verify("RuntimeInvisibleTypeAnnotations",
328 "0: #36(#37=[@#38(),@#38(),@#38()]): CAST, offset=0, type_index=0",
329 "1: #39(#37=[@#40(),@#40(),@#40()]): CAST, offset=6, type_index=0",
330 "2: #39(#37=[@#40(),@#40(),@#40()]): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
331 "RuntimeInvisibleTypeAnnotations",
332 "0: #39(#37=[@#40(),@#40(),@#40()]): METHOD_RETURN",
333 "1: #39(#37=[@#40(),@#40(),@#40()]): METHOD_FORMAL_PARAMETER, param_index=0",
334 "2: #40(): METHOD_FORMAL_PARAMETER, param_index=1");
335 }
336 }
337
338 @TestCase
339 public static class TC14 extends RepeatingTypeAnnotations {
340 public TC14() {
341 setSrc(" /* TC14 */ ",
342 " public @A @B @B String foo(@A @A @B Object o, @A @B int i, long l) {",
343 " @A @A @B String ls = (@B @A @B String) o;",
344 " return (@A @B @B String) o;",
345 " }");
346 verify(
347 "RuntimeInvisibleTypeAnnotations:",
348 "0: #36(#37=[@#38(),@#38()]): CAST, offset=0, type_index=0",
349 "1: #39(): CAST, offset=0, type_index=0",
350 "2: #39(): CAST, offset=6, type_index=0",
351 "3: #36(#37=[@#38(),@#38()]): CAST, offset=6, type_index=0",
352 "4: #40(#37=[@#39(),@#39()]): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
353 "5: #38(): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
354 "RuntimeInvisibleTypeAnnotations:",
355 "0: #39(): METHOD_RETURN",
356 "1: #36(#37=[@#38(),@#38()]): METHOD_RETURN",
357 "2: #40(#37=[@#39(),@#39()]): METHOD_FORMAL_PARAMETER, param_index=0",
358 "3: #38(): METHOD_FORMAL_PARAMETER, param_index=0",
359 "4: #39(): METHOD_FORMAL_PARAMETER, param_index=1",
360 "5: #38(): METHOD_FORMAL_PARAMETER, param_index=1"
361 );
362 }
363 }
364
365 @TestCase
366 public static class TC15 extends RepeatingTypeAnnotations {
367 public TC15() {
368 setSrc(" /* TC15 */ ",
369 " public @A @A @C String foo(@C @C @A Object o, @A @B int i, long l) {",
370 " @C @C @A String ls = (@A @A @C String) o;",
371 " return (@C @B @B String) o;",
372 " }");
373 verify(
374 "RuntimeVisibleTypeAnnotations:",
375 "0: #36(): CAST, offset=0, type_index=0",
376 "1: #36(): CAST, offset=6, type_index=0",
377 "2: #37(#38=[@#36(),@#36()]): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
378 "RuntimeInvisibleTypeAnnotations:",
379 "0: #40(#38=[@#41(),@#41()]): CAST, offset=0, type_index=0",
380 "1: #42(#38=[@#43(),@#43()]): CAST, offset=6, type_index=0",
381 "2: #41(): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
382 "RuntimeVisibleTypeAnnotations:",
383 "0: #36(): METHOD_RETURN",
384 "1: #37(#38=[@#36(),@#36()]): METHOD_FORMAL_PARAMETER, param_index=0",
385 "RuntimeInvisibleTypeAnnotations:",
386 "0: #40(#38=[@#41(),@#41()]): METHOD_RETURN",
387 "1: #41(): METHOD_FORMAL_PARAMETER, param_index=0",
388 "2: #41(): METHOD_FORMAL_PARAMETER, param_index=1",
389 "3: #43(): METHOD_FORMAL_PARAMETER, param_index=1"
390 );
391 }
392 }
393
394 @TestCase
395 public static class TC16 extends RepeatingTypeAnnotations {
396 public TC16() {
397 setSrc(" /* TC16 */ ",
398 " public @A @B @C String foo(@A @B @C Object o, @A @C int i, long l) {",
399 " @A @B @C String ls = (@C @A @B String) o;",
400 " return (@B @A @C String) o;",
401 " }");
402 verify(
403 "RuntimeVisibleTypeAnnotations:",
404 "0: #36(): CAST, offset=0, type_index=0",
405 "1: #36(): CAST, offset=6, type_index=0",
406 "2: #36(): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
407 "RuntimeInvisibleTypeAnnotations:",
408 "0: #38(): CAST, offset=0, type_index=0",
409 "1: #39(): CAST, offset=0, type_index=0",
410 "2: #39(): CAST, offset=6, type_index=0",
411 "3: #38(): CAST, offset=6, type_index=0",
412 "4: #38(): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
413 "5: #39(): LOCAL_VARIABLE, {start_pc=6, length=5, index=5}",
414 "RuntimeVisibleTypeAnnotations:",
415 "0: #36(): METHOD_RETURN",
416 "1: #36(): METHOD_FORMAL_PARAMETER, param_index=0",
417 "2: #36(): METHOD_FORMAL_PARAMETER, param_index=1",
418 "RuntimeInvisibleTypeAnnotations:",
419 "0: #38(): METHOD_RETURN",
420 "1: #39(): METHOD_RETURN",
421 "2: #38(): METHOD_FORMAL_PARAMETER, param_index=0",
422 "3: #39(): METHOD_FORMAL_PARAMETER, param_index=0",
423 "4: #38(): METHOD_FORMAL_PARAMETER, param_index=1"
424 );
425 }
426 }
427 }

mercurial