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