8031
|
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 8007320
|
|
27 * @summary Test all optional fields in ConstMethod
|
|
28 * @compile -g -parameters ConstMethodTest.java
|
|
29 * @run main ConstMethodTest
|
|
30 */
|
|
31
|
|
32 import java.util.*;
|
|
33 import java.lang.annotation.*;
|
|
34 import java.lang.reflect.*;
|
|
35 import java.io.Serializable;
|
|
36
|
|
37 @Retention(RetentionPolicy.RUNTIME)
|
|
38 @interface MyAnnotation {
|
|
39 public String name();
|
|
40 public String value();
|
|
41 public String date() default "today";
|
|
42 }
|
|
43
|
|
44 @Target(ElementType.TYPE_USE)
|
|
45 @Retention(RetentionPolicy.RUNTIME)
|
|
46 @interface TypeAnno {
|
|
47 String value();
|
|
48 }
|
|
49
|
|
50 @Target(ElementType.TYPE_USE)
|
|
51 @Retention(RetentionPolicy.RUNTIME)
|
|
52 @interface TypeAnno2 {
|
|
53 String value();
|
|
54 }
|
|
55
|
|
56 @Retention(RetentionPolicy.RUNTIME)
|
|
57 @Target(ElementType.PARAMETER)
|
|
58 @interface Named {
|
|
59 String value();
|
|
60 }
|
|
61
|
|
62 @Retention(RetentionPolicy.RUNTIME)
|
|
63 @interface ScalarTypesWithDefault {
|
|
64 byte b() default 11;
|
|
65 short s() default 12;
|
|
66 int i() default 13;
|
|
67 long l() default 14;
|
|
68 char c() default 'V';
|
|
69 }
|
|
70
|
|
71 // Some exception class
|
|
72 class OkException extends RuntimeException {};
|
|
73
|
|
74
|
|
75 @MyAnnotation(name="someName", value = "Hello World")
|
|
76 public class ConstMethodTest {
|
|
77
|
|
78 private static void check(boolean b) {
|
|
79 if (!b)
|
|
80 throw new RuntimeException();
|
|
81 }
|
|
82 private static void fail(String msg) {
|
|
83 System.err.println(msg);
|
|
84 throw new RuntimeException();
|
|
85 }
|
|
86 private static void equal(Object x, Object y) {
|
|
87 if (x == null ? y == null : x.equals(y)) {
|
|
88 } else {
|
|
89 fail(x + " not equal to " + y);
|
|
90 }
|
|
91 }
|
|
92 private static final String[] parameter_names = {
|
|
93 "parameter", "parameter2", "x"
|
|
94 };
|
|
95
|
|
96 // Declare a function with everything in it.
|
|
97 @MyAnnotation(name="someName", value="Hello World")
|
|
98 static <T> void kitchenSinkFunc(@Named(value="aName") String parameter,
|
|
99 @Named("bName") String parameter2,
|
|
100 @ScalarTypesWithDefault T x)
|
|
101 throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException,
|
|
102 NullPointerException,
|
|
103 @TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException {
|
|
104 int i, j, k;
|
|
105 try {
|
|
106 System.out.println("calling kitchenSinkFunc " + parameter);
|
|
107 throw new OkException(); // to see stack trace with line numbers
|
|
108 } catch (Exception e) {
|
|
109 e.printStackTrace();
|
|
110 }
|
|
111 }
|
|
112
|
|
113 private static void test1() throws Throwable {
|
|
114 for (Method m : ConstMethodTest.class.getDeclaredMethods()) {
|
|
115 if (m.getName().equals("kitchenSinkFunc")) {
|
|
116 Annotation[][] ann = m.getParameterAnnotations();
|
|
117 equal(ann.length, 3);
|
|
118 Annotation foo = ann[0][0];
|
|
119 Annotation bar = ann[1][0];
|
|
120 equal(foo.toString(), "@Named(value=aName)");
|
|
121 equal(bar.toString(), "@Named(value=bName)");
|
|
122 check(foo.equals(foo));
|
|
123 check(bar.equals(bar));
|
|
124 check(! foo.equals(bar));
|
|
125 // method annotations
|
|
126 Annotation[] ann2 = m.getAnnotations();
|
|
127 equal(ann2.length, 1);
|
|
128 Annotation mann = ann2[0];
|
|
129 equal(mann.toString(), "@MyAnnotation(date=today, name=someName, value=Hello World)");
|
|
130 // Test Method parameter names
|
|
131 Parameter[] parameters = m.getParameters();
|
|
132 if(parameters == null)
|
|
133 throw new Exception("getParameters should never be null");
|
|
134 for(int i = 0; i < parameters.length; i++) {
|
|
135 Parameter p = parameters[i];
|
|
136 equal(parameters[i].getName(), parameter_names[i]);
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140 }
|
|
141
|
|
142 public static void main(java.lang.String[] unused) throws Throwable {
|
|
143 // pass 5 so kitchenSinkFunc is instantiated with an int
|
|
144 kitchenSinkFunc("parameter", "param2", 5);
|
|
145 test1();
|
|
146 }
|
|
147 };
|
|
148
|