author | ccheung |
Fri, 04 Oct 2013 21:00:43 -0700 | |
changeset 20384 | 49bfb2ec8f8f |
parent 17860 | 1ad3f2d9b4eb |
child 39959 | 3c8f8c98ad2a |
permissions | -rw-r--r-- |
15601 | 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 |
|
17860
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
26 |
* @bug 8007320 8014709 |
15601 | 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 { |
|
17860
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
77 |
public @TypeAnno("constructor") ConstMethodTest() { } |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
78 |
|
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
79 |
public ConstMethodTest(int i) { |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
80 |
// needs a second unannotated constructor |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
81 |
} |
15601 | 82 |
|
83 |
private static void check(boolean b) { |
|
84 |
if (!b) |
|
85 |
throw new RuntimeException(); |
|
86 |
} |
|
87 |
private static void fail(String msg) { |
|
88 |
System.err.println(msg); |
|
89 |
throw new RuntimeException(); |
|
90 |
} |
|
91 |
private static void equal(Object x, Object y) { |
|
92 |
if (x == null ? y == null : x.equals(y)) { |
|
93 |
} else { |
|
94 |
fail(x + " not equal to " + y); |
|
95 |
} |
|
96 |
} |
|
97 |
private static final String[] parameter_names = { |
|
98 |
"parameter", "parameter2", "x" |
|
99 |
}; |
|
100 |
||
101 |
// Declare a function with everything in it. |
|
102 |
@MyAnnotation(name="someName", value="Hello World") |
|
103 |
static <T> void kitchenSinkFunc(@Named(value="aName") String parameter, |
|
104 |
@Named("bName") String parameter2, |
|
105 |
@ScalarTypesWithDefault T x) |
|
106 |
throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException, |
|
107 |
NullPointerException, |
|
108 |
@TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException { |
|
109 |
int i, j, k; |
|
110 |
try { |
|
111 |
System.out.println("calling kitchenSinkFunc " + parameter); |
|
112 |
throw new OkException(); // to see stack trace with line numbers |
|
113 |
} catch (Exception e) { |
|
114 |
e.printStackTrace(); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
private static void test1() throws Throwable { |
|
119 |
for (Method m : ConstMethodTest.class.getDeclaredMethods()) { |
|
120 |
if (m.getName().equals("kitchenSinkFunc")) { |
|
121 |
Annotation[][] ann = m.getParameterAnnotations(); |
|
122 |
equal(ann.length, 3); |
|
123 |
Annotation foo = ann[0][0]; |
|
124 |
Annotation bar = ann[1][0]; |
|
125 |
equal(foo.toString(), "@Named(value=aName)"); |
|
126 |
equal(bar.toString(), "@Named(value=bName)"); |
|
127 |
check(foo.equals(foo)); |
|
128 |
check(bar.equals(bar)); |
|
129 |
check(! foo.equals(bar)); |
|
130 |
// method annotations |
|
131 |
Annotation[] ann2 = m.getAnnotations(); |
|
132 |
equal(ann2.length, 1); |
|
133 |
Annotation mann = ann2[0]; |
|
134 |
equal(mann.toString(), "@MyAnnotation(date=today, name=someName, value=Hello World)"); |
|
135 |
// Test Method parameter names |
|
136 |
Parameter[] parameters = m.getParameters(); |
|
137 |
if(parameters == null) |
|
138 |
throw new Exception("getParameters should never be null"); |
|
139 |
for(int i = 0; i < parameters.length; i++) { |
|
140 |
Parameter p = parameters[i]; |
|
141 |
equal(parameters[i].getName(), parameter_names[i]); |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
17860
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
147 |
private static void testConstructor() throws Exception { |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
148 |
for (Constructor c : ConstMethodTest.class.getDeclaredConstructors()) { |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
149 |
Annotation[] aa = c.getAnnotatedReturnType().getAnnotations(); |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
150 |
if (c.getParameterTypes().length == 1) { // should be un-annotated |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
151 |
check(aa.length == 0); |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
152 |
} else if (c.getParameterTypes().length == 0) { //should be annotated |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
153 |
check(aa.length == 1); |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
154 |
check(((TypeAnno)aa[0]).value().equals("constructor")); |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
155 |
} else { |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
156 |
//should not happen |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
157 |
check(false); |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
158 |
} |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
159 |
} |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
160 |
} |
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
161 |
|
15601 | 162 |
public static void main(java.lang.String[] unused) throws Throwable { |
163 |
// pass 5 so kitchenSinkFunc is instantiated with an int |
|
164 |
kitchenSinkFunc("parameter", "param2", 5); |
|
165 |
test1(); |
|
17860
1ad3f2d9b4eb
8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents:
15601
diff
changeset
|
166 |
testConstructor(); |
15601 | 167 |
} |
168 |
}; |
|
169 |