blob: 7b6036b82f4843fdc28fbdcdfac6f82a635a31a4 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001// Copyright 2006 The Android Open Source Project
2
3import java.lang.reflect.*;
4import java.io.IOException;
5import java.util.Collections;
6
7/**
8 * Reflection test.
9 */
10public class Main {
11 void printMethodInfo(Method meth) {
12 Class[] params, exceptions;
13 int i;
14
15 System.out.println("Method name is " + meth.getName());
16 System.out.println(" Declaring class is "
17 + meth.getDeclaringClass().getName());
18 params = meth.getParameterTypes();
19 for (i = 0; i < params.length; i++)
20 System.out.println(" Arg " + i + ": " + params[i].getName());
21 exceptions = meth.getExceptionTypes();
22 for (i = 0; i < exceptions.length; i++)
23 System.out.println(" Exc " + i + ": " + exceptions[i].getName());
24 System.out.println(" Return type is " + meth.getReturnType().getName());
25 System.out.println(" Access flags are 0x"
26 + Integer.toHexString(meth.getModifiers()));
27 //System.out.println(" GenericStr is " + meth.toGenericString());
28 }
29
30 void printFieldInfo(Field field) {
31 System.out.println("Field name is " + field.getName());
32 System.out.println(" Declaring class is "
33 + field.getDeclaringClass().getName());
34 System.out.println(" Field type is " + field.getType().getName());
35 System.out.println(" Access flags are 0x"
36 + Integer.toHexString(field.getModifiers()));
37 }
38
39 private void showStrings(Target instance)
40 throws NoSuchFieldException, IllegalAccessException {
41
42 Class target = Target.class;
43 String one, two, three, four;
44 Field field = null;
45
46 field = target.getField("string1");
47 one = (String) field.get(instance);
48
49 field = target.getField("string2");
50 two = (String) field.get(instance);
51
52 field = target.getField("string3");
53 three = (String) field.get(instance);
54
55 System.out.println(" ::: " + one + ":" + two + ":" + three);
56 }
57
58 public void run() {
59 Class target = Target.class;
60 Method meth = null;
61 Field field = null;
62 boolean excep;
63
64 try {
65 meth = target.getMethod("myMethod", new Class[] { int.class });
66
67 if (meth.getDeclaringClass() != target)
68 throw new RuntimeException();
69 printMethodInfo(meth);
70
71 meth = target.getMethod("myMethod", new Class[] { float.class });
72 printMethodInfo(meth);
73
74 meth = target.getMethod("myNoargMethod", (Class[]) null);
75 printMethodInfo(meth);
76
77 meth = target.getMethod("myMethod",
78 new Class[] { String[].class, float.class, char.class });
79 printMethodInfo(meth);
80
81 Target instance = new Target();
82 Object[] argList = new Object[] {
83 new String[] { "hi there" },
84 new Float(3.1415926f),
85 new Character('Q')
86 };
87 System.out.println("Before, float is "
88 + ((Float)argList[1]).floatValue());
89
90 Integer boxval;
91 boxval = (Integer) meth.invoke(instance, argList);
92 System.out.println("Result of invoke: " + boxval.intValue());
93
94 System.out.println("Calling no-arg void-return method");
95 meth = target.getMethod("myNoargMethod", (Class[]) null);
96 meth.invoke(instance, (Object[]) null);
97
98 /* try invoking a method that throws an exception */
99 meth = target.getMethod("throwingMethod", (Class[]) null);
100 try {
101 meth.invoke(instance, (Object[]) null);
102 System.out.println("GLITCH: didn't throw");
103 } catch (InvocationTargetException ite) {
104 System.out.println("Invoke got expected exception:");
105 System.out.println(ite.getClass().getName());
106 System.out.println(ite.getCause());
107 }
108 catch (Exception ex) {
109 System.out.println("GLITCH: invoke got wrong exception:");
110 ex.printStackTrace();
111 }
112 System.out.println("");
113
114
115 field = target.getField("string1");
116 if (field.getDeclaringClass() != target)
117 throw new RuntimeException();
118 printFieldInfo(field);
119 String strVal = (String) field.get(instance);
120 System.out.println(" string1 value is '" + strVal + "'");
121
122 showStrings(instance);
123
124 field.set(instance, new String("a new string"));
125 strVal = (String) field.get(instance);
126 System.out.println(" string1 value is now '" + strVal + "'");
127
128 showStrings(instance);
129
130 try {
131 field.set(instance, new Object());
132 System.out.println("WARNING: able to store Object into String");
133 }
134 catch (IllegalArgumentException iae) {
135 System.out.println(" got expected illegal obj store exc");
136 }
137
138
139 try {
140 String four;
141 field = target.getField("string4");
142 four = (String) field.get(instance);
143 System.out.println("WARNING: able to access string4: "
144 + four);
145 }
146 catch (IllegalAccessException iae) {
147 System.out.println(" got expected access exc");
148 }
149 catch (NoSuchFieldException nsfe) {
150 System.out.println(" got the other expected access exc");
151 }
152 try {
153 String three;
154 field = target.getField("string3");
155 three = (String) field.get(this);
156 System.out.println("WARNING: able to get string3 in wrong obj: "
157 + three);
158 }
159 catch (IllegalArgumentException iae) {
160 System.out.println(" got expected arg exc");
161 }
162
163 /*
164 * Try setting a field to null.
165 */
166 String four;
167 field = target.getDeclaredField("string3");
168 field.set(instance, null);
169
170 /*
171 * Do some stuff with long.
172 */
173 long longVal;
174 field = target.getField("pubLong");
175 longVal = field.getLong(instance);
176 System.out.println("pubLong initial value is " +
177 Long.toHexString(longVal));
178 field.setLong(instance, 0x9988776655443322L);
179 longVal = field.getLong(instance);
180 System.out.println("pubLong new value is " +
181 Long.toHexString(longVal));
182
183
184 field = target.getField("superInt");
185 if (field.getDeclaringClass() == target)
186 throw new RuntimeException();
187 printFieldInfo(field);
188 int intVal = field.getInt(instance);
189 System.out.println(" superInt value is " + intVal);
190 Integer boxedIntVal = (Integer) field.get(instance);
191 System.out.println(" superInt boxed is " + boxedIntVal);
192
193 field.set(instance, new Integer(20202));
194 intVal = field.getInt(instance);
195 System.out.println(" superInt value is now " + intVal);
196 field.setShort(instance, (short)30303);
197 intVal = field.getInt(instance);
198 System.out.println(" superInt value (from short) is now " +intVal);
199 field.setInt(instance, 40404);
200 intVal = field.getInt(instance);
201 System.out.println(" superInt value is now " + intVal);
202 try {
203 field.set(instance, new Long(123));
204 System.out.println("FAIL: expected exception not thrown");
205 }
206 catch (IllegalArgumentException iae) {
207 System.out.println(" got expected long->int failure");
208 }
209 try {
210 field.setLong(instance, 123);
211 System.out.println("FAIL: expected exception not thrown");
212 }
213 catch (IllegalArgumentException iae) {
214 System.out.println(" got expected long->int failure");
215 }
216 try {
217 field.set(instance, new String("abc"));
218 System.out.println("FAIL: expected exception not thrown");
219 }
220 catch (IllegalArgumentException iae) {
221 System.out.println(" got expected string->int failure");
222 }
223
224 try {
225 field.getShort(instance);
226 System.out.println("FAIL: expected exception not thrown");
227 }
228 catch (IllegalArgumentException iae) {
229 System.out.println(" got expected int->short failure");
230 }
231
232 field = target.getField("superClassInt");
233 printFieldInfo(field);
234 int superClassIntVal = field.getInt(instance);
235 System.out.println(" superClassInt value is " + superClassIntVal);
236
237 field = target.getField("staticDouble");
238 printFieldInfo(field);
239 double staticDoubleVal = field.getDouble(null);
240 System.out.println(" staticDoubleVal value is " + staticDoubleVal);
241
242 try {
243 field.getLong(instance);
244 System.out.println("FAIL: expected exception not thrown");
245 }
246 catch (IllegalArgumentException iae) {
247 System.out.println(" got expected double->long failure");
248 }
249
250 excep = false;
251 try {
252 field = target.getField("aPrivateInt");
253 printFieldInfo(field);
254 }
255 catch (NoSuchFieldException nsfe) {
256 System.out.println("as expected: aPrivateInt not found");
257 excep = true;
258 }
259 if (!excep)
260 System.out.println("BUG: got aPrivateInt");
261
262
263 field = target.getField("constantString");
264 printFieldInfo(field);
265 String val = (String) field.get(instance);
266 System.out.println(" Constant test value is " + val);
267
268
269 field = target.getField("cantTouchThis");
270 printFieldInfo(field);
271 intVal = field.getInt(instance);
272 System.out.println(" cantTouchThis is " + intVal);
273 try {
274 field.setInt(instance, 99);
Elliott Hughes582a7d12011-10-10 18:38:42 -0700275 System.out.println(" setAccessible is always true");
jeffhao5d1ac922011-09-29 17:41:15 -0700276 } catch (IllegalAccessException iae) {
Elliott Hughes582a7d12011-10-10 18:38:42 -0700277 System.out.println("ERROR: set-final failed");
jeffhao5d1ac922011-09-29 17:41:15 -0700278 }
279 intVal = field.getInt(instance);
280 System.out.println(" cantTouchThis is now " + intVal);
281
282 field.setAccessible(true);
283 field.setInt(instance, 87); // exercise int version
Elliott Hughes582a7d12011-10-10 18:38:42 -0700284 intVal = field.getInt(instance);
285 System.out.println(" cantTouchThis is now " + intVal);
jeffhao5d1ac922011-09-29 17:41:15 -0700286 field.set(instance, 88); // exercise Object version
287 intVal = field.getInt(instance);
288 System.out.println(" cantTouchThis is now " + intVal);
289
290 Constructor<Target> cons;
291 Target targ;
292 Object[] args;
293
294 cons = target.getConstructor(new Class[] { int.class,float.class });
295 args = new Object[] { new Integer(7), new Float(3.3333) };
296 System.out.println("cons modifiers=" + cons.getModifiers());
297 targ = cons.newInstance(args);
298 targ.myMethod(17);
299
300 }
301 catch (Exception ex) {
302 System.out.println("----- unexpected exception -----");
303 ex.printStackTrace();
304 }
305
306 System.out.println("ReflectTest done!");
307 }
308
309 public static void checkType() {
310 Method m;
311
312 try {
313 m = Collections.class.getDeclaredMethod("checkType",
314 Object.class, Class.class);
315 } catch (NoSuchMethodException nsme) {
316 nsme.printStackTrace();
317 return;
318 }
319
320 m.setAccessible(true);
321 try {
322 m.invoke(null, new Object(), Object.class);
323 } catch (IllegalAccessException iae) {
324 iae.printStackTrace();
325 return;
326 } catch (InvocationTargetException ite) {
327 ite.printStackTrace();
328 return;
329 }
330
331 try {
332 System.out.println("checkType invoking null");
333 m.invoke(null, new Object(), int.class);
334 System.out.println("ERROR: should throw InvocationTargetException");
335 } catch (InvocationTargetException ite) {
336 System.out.println("checkType got expected exception");
337 } catch (IllegalAccessException iae) {
338 iae.printStackTrace();
339 return;
340 }
341 }
342
343 public static void checkInit() {
344 Class niuClass = NoisyInitUser.class;
345 Method[] methods;
346
347 methods = niuClass.getDeclaredMethods();
348 System.out.println("got methods");
349 /* neither NoisyInit nor NoisyInitUser should be initialized yet */
350 NoisyInitUser niu = new NoisyInitUser();
351 NoisyInit ni = new NoisyInit();
352 }
353
354 public static void main(String[] args) {
355 Main test = new Main();
356 test.run();
357
358 checkType();
359 checkInit();
360 }
361}
362
363
364class SuperTarget {
365 public SuperTarget() {
366 System.out.println("SuperTarget constructor ()V");
367 superInt = 1010101;
368 superClassInt = 1010102;
369 }
370
371 public int myMethod(float floatArg) {
372 System.out.println("myMethod (F)I " + floatArg);
373 return 6;
374 }
375
376 public int superInt;
377 public static int superClassInt;
378}
379
380class Target extends SuperTarget {
381 public Target() {
382 System.out.println("Target constructor ()V");
383 }
384
385 public Target(int ii, float ff) {
386 System.out.println("Target constructor (IF)V : ii="
387 + ii + " ff=" + ff);
388 anInt = ii;
389 }
390
391 public int myMethod(int intarg) throws NullPointerException, IOException {
392 System.out.println("myMethod (I)I");
393 System.out.println(" arg=" + intarg + " anInt=" + anInt);
394 return 5;
395 }
396
397 public int myMethod(String[] strarg, float f, char c) {
398 System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !");
399 return 7;
400 }
401
402 public static void myNoargMethod() {
403 System.out.println("myNoargMethod ()V");
404 }
405
406 public void throwingMethod() {
407 System.out.println("throwingMethod");
408 throw new NullPointerException("gratuitous throw!");
409 }
410
411 public void misc() {
412 System.out.println("misc");
413 }
414
415 public int anInt;
416 public String string1 = "hey";
417 public String string2 = "yo";
418 public String string3 = "there";
419 private String string4 = "naughty";
420 public static final String constantString = "a constant string";
421 private int aPrivateInt;
422
423 public final int cantTouchThis = 77;
424
425 public long pubLong = 0x1122334455667788L;
426
427 public static double staticDouble = 3.3;
428}
429
430class NoisyInit {
431 static {
432 System.out.println("NoisyInit is initializing");
433 //Throwable th = new Throwable();
434 //th.printStackTrace();
435 }
436}
437
438class NoisyInitUser {
439 static {
440 System.out.println("NoisyInitUser is initializing");
441 }
442 public void createNoisyInit(NoisyInit ni) {}
443}