author | vromero |
Sat, 01 Jun 2013 22:09:18 +0100 | |
changeset 18000 | 5d29ce00a7a2 |
parent 17999 | 42ae6fe53718 |
child 21044 | ffba9cdeff1a |
permissions | -rw-r--r-- |
16307 | 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 |
import java.io.*; |
|
25 |
import java.lang.reflect.*; |
|
26 |
||
27 |
/** |
|
28 |
* Test MethodParameter attributs by reflection API |
|
29 |
*/ |
|
30 |
public class ReflectionVisitor extends Tester.Visitor { |
|
31 |
||
32 |
public ReflectionVisitor(Tester tester) { |
|
33 |
super(tester); |
|
34 |
} |
|
35 |
||
36 |
public void error(String msg) { |
|
37 |
super.error("reflection: " + msg); |
|
38 |
} |
|
39 |
||
40 |
public void warn(String msg) { |
|
41 |
super.warn("reflection: " + msg); |
|
42 |
} |
|
43 |
||
44 |
boolean isEnum; |
|
45 |
boolean isInterface; |
|
46 |
boolean isAnon; |
|
47 |
boolean isLocal; |
|
48 |
boolean isMember; |
|
49 |
boolean isStatic; |
|
50 |
boolean isPublic; |
|
51 |
Class clazz; |
|
52 |
StringBuilder sb; |
|
53 |
||
54 |
/** |
|
55 |
* Read class using {@code ClassFile}, and generate a list of methods |
|
56 |
* with parameter names as available in the MethodParameters attribute. |
|
57 |
*/ |
|
58 |
void visitClass(final String cl, final File cfile, final StringBuilder sb) |
|
59 |
throws Exception { |
|
60 |
||
61 |
this.sb = sb; |
|
62 |
clazz = Class.forName(cl); |
|
63 |
isEnum = clazz.isEnum(); |
|
64 |
isInterface = clazz.isInterface(); |
|
65 |
isAnon = clazz.isAnonymousClass(); |
|
66 |
isLocal = clazz.isLocalClass(); |
|
67 |
isMember = clazz.isMemberClass(); |
|
68 |
isStatic = ((clazz.getModifiers() & Modifier.STATIC) != 0); |
|
69 |
isPublic = ((clazz.getModifiers() & Modifier.PUBLIC) != 0); |
|
70 |
||
71 |
sb.append(isStatic ? "static " : "") |
|
72 |
.append(isPublic ? "public " : "") |
|
73 |
.append(isEnum ? "enum " : isInterface ? "interface " : "class ") |
|
74 |
.append(cl).append(" -- ") |
|
75 |
.append(isMember? "member " : "" ) |
|
76 |
.append(isLocal? "local " : "" ) |
|
77 |
.append(isAnon ? "anon" : "") |
|
78 |
.append("\n"); |
|
79 |
||
80 |
for (Constructor c : clazz.getDeclaredConstructors()) { |
|
81 |
testConstructor(c); |
|
82 |
} |
|
83 |
||
84 |
for (Method m :clazz.getDeclaredMethods()) { |
|
85 |
||
86 |
testMethod(m); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
void testConstructor(Constructor c) { |
|
91 |
||
92 |
String prefix = clazz.getName() + "." + c.getName() + "() - "; |
|
93 |
||
94 |
// Parameters must match parameter types |
|
95 |
Parameter params[] = c.getParameters(); |
|
96 |
int paramTypes = c.getParameterTypes().length; |
|
97 |
if (paramTypes != params.length) { |
|
98 |
error(prefix + "number of parameter types (" + paramTypes |
|
99 |
+ ") != number of parameters (" + params.length + ")"); |
|
100 |
return; |
|
101 |
} |
|
102 |
||
103 |
sb.append(clazz.getName()).append(".").append("<init>").append("("); |
|
104 |
String sep = ""; |
|
105 |
||
106 |
// Some paramters are expected |
|
107 |
if (params.length < 2 && isEnum) { |
|
108 |
error(prefix + "enum constuctor, two arguments expected"); |
|
109 |
} else if (params.length < 1 && (isAnon || isLocal || |
|
110 |
(isMember && !isStatic ))) { |
|
111 |
error(prefix + "class constuctor,expected implicit argument"); |
|
112 |
} |
|
113 |
||
114 |
int i = -1; |
|
115 |
String param = null; |
|
116 |
for (Parameter p : c.getParameters()) { |
|
117 |
i++; |
|
118 |
String pname = p.getName(); |
|
119 |
sb.append(sep).append(pname); |
|
120 |
if (p.isImplicit()) sb.append("!"); |
|
121 |
if (p.isSynthetic()) sb.append("!!"); |
|
122 |
sep = ", "; |
|
123 |
||
124 |
// Set expectations |
|
125 |
String expect = null; |
|
126 |
boolean allowImplicit = false; |
|
127 |
boolean allowSynthetic = false; |
|
128 |
if (isEnum) { |
|
129 |
if (i == 0) { |
|
130 |
expect = "\\$enum\\$name"; |
|
131 |
allowSynthetic = true; |
|
132 |
} else if(i == 1) { |
|
133 |
expect = "\\$enum\\$ordinal"; |
|
134 |
allowSynthetic = true; |
|
135 |
} |
|
136 |
} else if (i == 0) { |
|
137 |
if (isAnon) { |
|
138 |
allowImplicit = true; |
|
139 |
} else if (isLocal) { |
|
140 |
allowImplicit = true; |
|
141 |
expect = "this\\$[0-n]*"; |
|
142 |
} else if ((isMember && !isStatic)) { |
|
143 |
allowImplicit = true; |
|
144 |
if (!isPublic) { |
|
145 |
// some but not all non-public inner classes |
|
146 |
// have synthetic argument. For now we give |
|
147 |
// the test a bit of slack and allow either. |
|
148 |
allowSynthetic = true; |
|
149 |
} |
|
150 |
expect = "this\\$[0-n]*"; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
// Check expected flags |
|
155 |
if (p.isSynthetic() && p.isImplicit()) { |
|
156 |
error(prefix + "param[" + i + "]='" + pname + |
|
157 |
"' both isImplicit() and isSynthetic()"); |
|
158 |
break; |
|
159 |
} |
|
160 |
if (allowImplicit && allowSynthetic && |
|
161 |
!(p.isSynthetic() || p.isImplicit())) { |
|
162 |
error(prefix + "param[" + i + "]='" + pname + |
|
163 |
"' isImplicit() or isSynthetic() expected"); |
|
164 |
break; |
|
165 |
} |
|
166 |
||
167 |
if (allowImplicit && !allowSynthetic && !p.isImplicit()) { |
|
168 |
error(prefix + "param[" + i + "]='" + pname + |
|
169 |
"' isImplicit() expected"); |
|
170 |
break; |
|
171 |
} |
|
172 |
if (!allowImplicit && allowSynthetic && !p.isSynthetic()) { |
|
173 |
error(prefix + "param[" + i + "]='" + pname + |
|
174 |
"' isSynthetic() expected"); |
|
175 |
break; |
|
176 |
} |
|
177 |
||
178 |
if (!allowImplicit && p.isImplicit()) { |
|
179 |
error(prefix + "param[" + i + "]='" + pname + |
|
180 |
"' isImplicit() unexpected"); |
|
181 |
break; |
|
182 |
} |
|
183 |
||
184 |
if (!allowSynthetic && p.isSynthetic()) { |
|
185 |
error(prefix + "param[" + i + "]='" + pname + |
|
186 |
"' isSynthetic() unexpected"); |
|
187 |
break; |
|
188 |
} |
|
189 |
||
190 |
// Check expected names |
|
191 |
if (expect != null) { |
|
192 |
if (pname.matches(expect)) continue; |
|
193 |
error(prefix + "param[" + i + "]='" + pname + |
|
194 |
"' expected '" + expect + "'"); |
|
195 |
break; |
|
196 |
} |
|
197 |
||
198 |
// Test naming convention for explicit parameters. |
|
199 |
boolean fidelity = !isAnon; |
|
200 |
if (param != null && fidelity) { |
|
201 |
char ch = param.charAt(0); |
|
202 |
expect = (++ch) + param; |
|
203 |
} |
|
204 |
||
205 |
if (pname != null && fidelity) { |
|
206 |
param = pname; |
|
207 |
} |
|
208 |
||
209 |
if (expect != null && !expect.equals(pname)) { |
|
210 |
error(prefix + "param[" + i + "]='" + pname + |
|
211 |
"' expected '" + expect + "'"); |
|
212 |
break; |
|
213 |
} |
|
214 |
} |
|
215 |
if (c.isSynthetic()) { |
|
216 |
sb.append(")!!\n"); |
|
217 |
} else { |
|
218 |
sb.append(")\n"); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
void testMethod(Method m) { |
|
223 |
||
224 |
String prefix = clazz.getName() + "." + m.getName() + "() - "; |
|
225 |
||
226 |
// Parameters must match parameter types |
|
227 |
int paramTypes = m.getParameterTypes().length; |
|
228 |
int params = m.getParameters().length; |
|
229 |
if (paramTypes != params) { |
|
230 |
error(prefix + "number of parameter types (" + paramTypes |
|
231 |
+ ") != number of parameters (" + params + ")"); |
|
232 |
return; |
|
233 |
} |
|
234 |
||
235 |
sb.append(clazz.getName()).append(".").append(m.getName()).append("("); |
|
236 |
String sep = ""; |
|
237 |
String param = null; |
|
238 |
int i = -1; |
|
239 |
// For methods we expect all parameters to follow |
|
240 |
// the test-case design pattern, except synthetic methods. |
|
241 |
for (Parameter p : m.getParameters()) { |
|
242 |
i++; |
|
243 |
if (param == null) { |
|
244 |
param = p.getName(); |
|
245 |
sb.append(sep).append(param); |
|
246 |
} else { |
|
247 |
char c = param.charAt(0); |
|
248 |
String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param); |
|
249 |
param = p.getName(); |
|
250 |
sb.append(sep).append(param); |
|
18000
5d29ce00a7a2
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
vromero
parents:
17999
diff
changeset
|
251 |
if (!m.isBridge() && !expect.equals(param)) { |
16307 | 252 |
error(prefix + "param[" + i + "]='" |
253 |
+ param + "' expected '" + expect + "'"); |
|
254 |
break; |
|
255 |
} |
|
256 |
} |
|
257 |
sep = ", "; |
|
258 |
} |
|
259 |
if (m.isSynthetic()) { |
|
260 |
sb.append(")!!\n"); |
|
261 |
} else { |
|
262 |
sb.append(")\n"); |
|
263 |
} |
|
264 |
} |
|
265 |
} |