blob: d60fcb485b61dc86da4046e119f8ab3e5140ca10 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
17import java.lang.reflect.*;
18import java.io.IOException;
19import java.util.Collections;
Elliott Hughes741b5b72012-01-31 19:18:51 -080020import java.util.ArrayList;
21import java.util.List;
22import java.util.Map;
jeffhao5d1ac922011-09-29 17:41:15 -070023
24/**
25 * Reflection test.
26 */
27public class Main {
Elliott Hughes741b5b72012-01-31 19:18:51 -080028 private static boolean FULL_ACCESS_CHECKS = false; // b/5861201
29 public Main() {}
30 public Main(ArrayList<Integer> stuff) {}
31
jeffhao5d1ac922011-09-29 17:41:15 -070032 void printMethodInfo(Method meth) {
33 Class[] params, exceptions;
34 int i;
35
36 System.out.println("Method name is " + meth.getName());
37 System.out.println(" Declaring class is "
38 + meth.getDeclaringClass().getName());
39 params = meth.getParameterTypes();
40 for (i = 0; i < params.length; i++)
41 System.out.println(" Arg " + i + ": " + params[i].getName());
42 exceptions = meth.getExceptionTypes();
43 for (i = 0; i < exceptions.length; i++)
44 System.out.println(" Exc " + i + ": " + exceptions[i].getName());
45 System.out.println(" Return type is " + meth.getReturnType().getName());
46 System.out.println(" Access flags are 0x"
47 + Integer.toHexString(meth.getModifiers()));
48 //System.out.println(" GenericStr is " + meth.toGenericString());
49 }
50
51 void printFieldInfo(Field field) {
52 System.out.println("Field name is " + field.getName());
53 System.out.println(" Declaring class is "
54 + field.getDeclaringClass().getName());
55 System.out.println(" Field type is " + field.getType().getName());
56 System.out.println(" Access flags are 0x"
57 + Integer.toHexString(field.getModifiers()));
58 }
59
60 private void showStrings(Target instance)
61 throws NoSuchFieldException, IllegalAccessException {
62
63 Class target = Target.class;
64 String one, two, three, four;
65 Field field = null;
66
67 field = target.getField("string1");
68 one = (String) field.get(instance);
69
70 field = target.getField("string2");
71 two = (String) field.get(instance);
72
73 field = target.getField("string3");
74 three = (String) field.get(instance);
75
76 System.out.println(" ::: " + one + ":" + two + ":" + three);
77 }
78
Elliott Hughes741b5b72012-01-31 19:18:51 -080079 public static void checkAccess() {
80 try {
81 Class target = otherpackage.Other.class;
82 Object instance = new otherpackage.Other();
83 Method meth;
84
85 meth = target.getMethod("publicMethod", (Class[]) null);
86 meth.invoke(instance);
87
88 try {
89 meth = target.getMethod("packageMethod", (Class[]) null);
90 System.err.println("succeeded on package-scope method");
91 } catch (NoSuchMethodException nsme) {
92 // good
93 }
94
95
96 instance = otherpackage.Other.getInnerClassInstance();
97 target = instance.getClass();
98 meth = target.getMethod("innerMethod", (Class[]) null);
99 try {
100 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
101 meth.invoke(instance);
102 System.err.println("inner-method invoke unexpectedly worked");
103 } catch (IllegalAccessException iae) {
104 // good
105 }
106
107 Field field = target.getField("innerField");
108 try {
109 int x = field.getInt(instance);
110 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
111 System.err.println("field get unexpectedly worked: " + x);
112 } catch (IllegalAccessException iae) {
113 // good
114 }
115 } catch (Exception ex) {
116 System.out.println("----- unexpected exception -----");
117 ex.printStackTrace();
118 }
119 }
120
jeffhao5d1ac922011-09-29 17:41:15 -0700121 public void run() {
122 Class target = Target.class;
123 Method meth = null;
124 Field field = null;
125 boolean excep;
126
127 try {
128 meth = target.getMethod("myMethod", new Class[] { int.class });
129
130 if (meth.getDeclaringClass() != target)
131 throw new RuntimeException();
132 printMethodInfo(meth);
133
134 meth = target.getMethod("myMethod", new Class[] { float.class });
135 printMethodInfo(meth);
136
137 meth = target.getMethod("myNoargMethod", (Class[]) null);
138 printMethodInfo(meth);
139
140 meth = target.getMethod("myMethod",
141 new Class[] { String[].class, float.class, char.class });
142 printMethodInfo(meth);
143
144 Target instance = new Target();
145 Object[] argList = new Object[] {
146 new String[] { "hi there" },
147 new Float(3.1415926f),
148 new Character('Q')
149 };
150 System.out.println("Before, float is "
151 + ((Float)argList[1]).floatValue());
152
153 Integer boxval;
154 boxval = (Integer) meth.invoke(instance, argList);
155 System.out.println("Result of invoke: " + boxval.intValue());
156
157 System.out.println("Calling no-arg void-return method");
158 meth = target.getMethod("myNoargMethod", (Class[]) null);
159 meth.invoke(instance, (Object[]) null);
160
161 /* try invoking a method that throws an exception */
162 meth = target.getMethod("throwingMethod", (Class[]) null);
163 try {
164 meth.invoke(instance, (Object[]) null);
165 System.out.println("GLITCH: didn't throw");
166 } catch (InvocationTargetException ite) {
167 System.out.println("Invoke got expected exception:");
168 System.out.println(ite.getClass().getName());
169 System.out.println(ite.getCause());
170 }
171 catch (Exception ex) {
172 System.out.println("GLITCH: invoke got wrong exception:");
173 ex.printStackTrace();
174 }
175 System.out.println("");
176
177
178 field = target.getField("string1");
179 if (field.getDeclaringClass() != target)
180 throw new RuntimeException();
181 printFieldInfo(field);
182 String strVal = (String) field.get(instance);
183 System.out.println(" string1 value is '" + strVal + "'");
184
185 showStrings(instance);
186
187 field.set(instance, new String("a new string"));
188 strVal = (String) field.get(instance);
189 System.out.println(" string1 value is now '" + strVal + "'");
190
191 showStrings(instance);
192
193 try {
194 field.set(instance, new Object());
195 System.out.println("WARNING: able to store Object into String");
196 }
197 catch (IllegalArgumentException iae) {
198 System.out.println(" got expected illegal obj store exc");
199 }
200
201
202 try {
203 String four;
204 field = target.getField("string4");
205 four = (String) field.get(instance);
206 System.out.println("WARNING: able to access string4: "
207 + four);
208 }
209 catch (IllegalAccessException iae) {
210 System.out.println(" got expected access exc");
211 }
212 catch (NoSuchFieldException nsfe) {
213 System.out.println(" got the other expected access exc");
214 }
215 try {
216 String three;
217 field = target.getField("string3");
218 three = (String) field.get(this);
219 System.out.println("WARNING: able to get string3 in wrong obj: "
220 + three);
221 }
222 catch (IllegalArgumentException iae) {
223 System.out.println(" got expected arg exc");
224 }
225
226 /*
227 * Try setting a field to null.
228 */
229 String four;
230 field = target.getDeclaredField("string3");
231 field.set(instance, null);
232
233 /*
234 * Do some stuff with long.
235 */
236 long longVal;
237 field = target.getField("pubLong");
238 longVal = field.getLong(instance);
239 System.out.println("pubLong initial value is " +
240 Long.toHexString(longVal));
241 field.setLong(instance, 0x9988776655443322L);
242 longVal = field.getLong(instance);
243 System.out.println("pubLong new value is " +
244 Long.toHexString(longVal));
245
246
247 field = target.getField("superInt");
248 if (field.getDeclaringClass() == target)
249 throw new RuntimeException();
250 printFieldInfo(field);
251 int intVal = field.getInt(instance);
252 System.out.println(" superInt value is " + intVal);
253 Integer boxedIntVal = (Integer) field.get(instance);
254 System.out.println(" superInt boxed is " + boxedIntVal);
255
256 field.set(instance, new Integer(20202));
257 intVal = field.getInt(instance);
258 System.out.println(" superInt value is now " + intVal);
259 field.setShort(instance, (short)30303);
260 intVal = field.getInt(instance);
261 System.out.println(" superInt value (from short) is now " +intVal);
262 field.setInt(instance, 40404);
263 intVal = field.getInt(instance);
264 System.out.println(" superInt value is now " + intVal);
265 try {
266 field.set(instance, new Long(123));
267 System.out.println("FAIL: expected exception not thrown");
268 }
269 catch (IllegalArgumentException iae) {
270 System.out.println(" got expected long->int failure");
271 }
272 try {
273 field.setLong(instance, 123);
274 System.out.println("FAIL: expected exception not thrown");
275 }
276 catch (IllegalArgumentException iae) {
277 System.out.println(" got expected long->int failure");
278 }
279 try {
280 field.set(instance, new String("abc"));
281 System.out.println("FAIL: expected exception not thrown");
282 }
283 catch (IllegalArgumentException iae) {
284 System.out.println(" got expected string->int failure");
285 }
286
287 try {
288 field.getShort(instance);
289 System.out.println("FAIL: expected exception not thrown");
290 }
291 catch (IllegalArgumentException iae) {
292 System.out.println(" got expected int->short failure");
293 }
294
295 field = target.getField("superClassInt");
296 printFieldInfo(field);
297 int superClassIntVal = field.getInt(instance);
298 System.out.println(" superClassInt value is " + superClassIntVal);
299
300 field = target.getField("staticDouble");
301 printFieldInfo(field);
302 double staticDoubleVal = field.getDouble(null);
303 System.out.println(" staticDoubleVal value is " + staticDoubleVal);
304
305 try {
306 field.getLong(instance);
307 System.out.println("FAIL: expected exception not thrown");
308 }
309 catch (IllegalArgumentException iae) {
310 System.out.println(" got expected double->long failure");
311 }
312
313 excep = false;
314 try {
315 field = target.getField("aPrivateInt");
316 printFieldInfo(field);
317 }
318 catch (NoSuchFieldException nsfe) {
319 System.out.println("as expected: aPrivateInt not found");
320 excep = true;
321 }
322 if (!excep)
323 System.out.println("BUG: got aPrivateInt");
324
325
326 field = target.getField("constantString");
327 printFieldInfo(field);
328 String val = (String) field.get(instance);
329 System.out.println(" Constant test value is " + val);
330
331
332 field = target.getField("cantTouchThis");
333 printFieldInfo(field);
334 intVal = field.getInt(instance);
335 System.out.println(" cantTouchThis is " + intVal);
336 try {
337 field.setInt(instance, 99);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700338 System.out.println("ERROR: set-final did not throw exception");
jeffhao5d1ac922011-09-29 17:41:15 -0700339 } catch (IllegalAccessException iae) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700340 System.out.println(" as expected: set-final throws exception");
jeffhao5d1ac922011-09-29 17:41:15 -0700341 }
342 intVal = field.getInt(instance);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700343 System.out.println(" cantTouchThis is still " + intVal);
jeffhao5d1ac922011-09-29 17:41:15 -0700344
Brian Carlstromea46f952013-07-30 01:26:50 -0700345 System.out.println(" " + field + " accessible=" + field.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700346 field.setAccessible(true);
Brian Carlstromea46f952013-07-30 01:26:50 -0700347 System.out.println(" " + field + " accessible=" + field.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700348 field.setInt(instance, 87); // exercise int version
Elliott Hughes582a7d12011-10-10 18:38:42 -0700349 intVal = field.getInt(instance);
350 System.out.println(" cantTouchThis is now " + intVal);
jeffhao5d1ac922011-09-29 17:41:15 -0700351 field.set(instance, 88); // exercise Object version
352 intVal = field.getInt(instance);
353 System.out.println(" cantTouchThis is now " + intVal);
354
355 Constructor<Target> cons;
356 Target targ;
357 Object[] args;
358
359 cons = target.getConstructor(new Class[] { int.class,float.class });
360 args = new Object[] { new Integer(7), new Float(3.3333) };
361 System.out.println("cons modifiers=" + cons.getModifiers());
362 targ = cons.newInstance(args);
363 targ.myMethod(17);
364
Elliott Hughes741b5b72012-01-31 19:18:51 -0800365 } catch (Exception ex) {
jeffhao5d1ac922011-09-29 17:41:15 -0700366 System.out.println("----- unexpected exception -----");
367 ex.printStackTrace();
368 }
369
370 System.out.println("ReflectTest done!");
371 }
372
373 public static void checkType() {
374 Method m;
375
376 try {
377 m = Collections.class.getDeclaredMethod("checkType",
378 Object.class, Class.class);
379 } catch (NoSuchMethodException nsme) {
380 nsme.printStackTrace();
381 return;
382 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 System.out.println(m + " accessible=" + m.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700384 m.setAccessible(true);
Brian Carlstromea46f952013-07-30 01:26:50 -0700385 System.out.println(m + " accessible=" + m.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700386 try {
387 m.invoke(null, new Object(), Object.class);
388 } catch (IllegalAccessException iae) {
389 iae.printStackTrace();
390 return;
391 } catch (InvocationTargetException ite) {
392 ite.printStackTrace();
393 return;
394 }
395
396 try {
Jeff Hao7a549462013-03-18 19:04:24 -0700397 String s = "Should be ignored";
398 m.invoke(s, new Object(), Object.class);
399 } catch (IllegalAccessException iae) {
400 iae.printStackTrace();
401 return;
402 } catch (InvocationTargetException ite) {
403 ite.printStackTrace();
404 return;
405 }
406
407 try {
jeffhao5d1ac922011-09-29 17:41:15 -0700408 System.out.println("checkType invoking null");
409 m.invoke(null, new Object(), int.class);
410 System.out.println("ERROR: should throw InvocationTargetException");
411 } catch (InvocationTargetException ite) {
412 System.out.println("checkType got expected exception");
413 } catch (IllegalAccessException iae) {
414 iae.printStackTrace();
415 return;
416 }
417 }
418
Elliott Hughes923e8b82012-03-23 11:44:07 -0700419 public static void checkClinitForFields() throws Exception {
420 // Loading a class constant shouldn't run <clinit>.
421 System.out.println("calling const-class FieldNoisyInitUser.class");
422 Class niuClass = FieldNoisyInitUser.class;
423 System.out.println("called const-class FieldNoisyInitUser.class");
jeffhao5d1ac922011-09-29 17:41:15 -0700424
Elliott Hughes923e8b82012-03-23 11:44:07 -0700425 // Getting the declared fields doesn't run <clinit>.
426 Field[] fields = niuClass.getDeclaredFields();
427 System.out.println("got fields");
Elliott Hughes741b5b72012-01-31 19:18:51 -0800428
Elliott Hughes923e8b82012-03-23 11:44:07 -0700429 Field field = niuClass.getField("staticField");
430 System.out.println("got field");
431 field.get(null);
432 System.out.println("read field value");
433
434 // FieldNoisyInitUser should now be initialized, but FieldNoisyInit shouldn't be initialized yet.
435 FieldNoisyInitUser niu = new FieldNoisyInitUser();
436 FieldNoisyInit ni = new FieldNoisyInit();
437
438 System.out.println("");
439 }
440
441 public static void checkClinitForMethods() throws Exception {
442 // Loading a class constant shouldn't run <clinit>.
443 System.out.println("calling const-class MethodNoisyInitUser.class");
444 Class niuClass = MethodNoisyInitUser.class;
445 System.out.println("called const-class MethodNoisyInitUser.class");
446
447 // Getting the declared methods doesn't run <clinit>.
448 Method[] methods = niuClass.getDeclaredMethods();
449 System.out.println("got methods");
450
451 Method method = niuClass.getMethod("staticMethod", (Class[]) null);
452 System.out.println("got method");
453 method.invoke(null);
454 System.out.println("invoked method");
455
456 // MethodNoisyInitUser should now be initialized, but MethodNoisyInit shouldn't be initialized yet.
457 MethodNoisyInitUser niu = new MethodNoisyInitUser();
458 MethodNoisyInit ni = new MethodNoisyInit();
459
460 System.out.println("");
jeffhao5d1ac922011-09-29 17:41:15 -0700461 }
462
Elliott Hughes741b5b72012-01-31 19:18:51 -0800463
464 /*
465 * Test some generic type stuff.
466 */
467 public List<String> dummy;
468 public Map<Integer,String> fancyMethod(ArrayList<String> blah) { return null; }
469 public static void checkGeneric() {
470 Field field;
471 try {
472 field = Main.class.getField("dummy");
473 } catch (NoSuchFieldException nsfe) {
474 throw new RuntimeException(nsfe);
475 }
476 Type listType = field.getGenericType();
477 System.out.println("generic field: " + listType);
478
479 Method method;
480 try {
481 method = Main.class.getMethod("fancyMethod",
482 new Class[] { ArrayList.class });
483 } catch (NoSuchMethodException nsme) {
484 throw new RuntimeException(nsme);
485 }
486 Type[] parmTypes = method.getGenericParameterTypes();
487 Type ret = method.getGenericReturnType();
488 System.out.println("generic method " + method.getName() + " params='"
489 + stringifyTypeArray(parmTypes) + "' ret='" + ret + "'");
490
491 Constructor ctor;
492 try {
493 ctor = Main.class.getConstructor(new Class[] { ArrayList.class });
494 } catch (NoSuchMethodException nsme) {
495 throw new RuntimeException(nsme);
496 }
497 parmTypes = ctor.getGenericParameterTypes();
498 System.out.println("generic ctor " + ctor.getName() + " params='"
499 + stringifyTypeArray(parmTypes) + "'");
500 }
501
502 /*
503 * Convert an array of Type into a string. Start with an array count.
504 */
505 private static String stringifyTypeArray(Type[] types) {
506 StringBuilder stb = new StringBuilder();
507 boolean first = true;
508
509 stb.append("[" + types.length + "]");
510
511 for (Type t: types) {
512 if (first) {
513 stb.append(" ");
514 first = false;
515 } else {
516 stb.append(", ");
517 }
518 stb.append(t.toString());
519 }
520
521 return stb.toString();
522 }
523
Brian Carlstromea46f952013-07-30 01:26:50 -0700524 public static void checkUnique() {
525 Field field1, field2;
526 try {
527 field1 = Main.class.getField("dummy");
528 field2 = Main.class.getField("dummy");
529 } catch (NoSuchFieldException nsfe) {
530 throw new RuntimeException(nsfe);
531 }
532 if (field1 == field2) {
533 System.out.println("ERROR: fields shouldn't have reference equality");
534 } else {
535 System.out.println("fields are unique");
536 }
537 if (field1.hashCode() == field2.hashCode() && field1.equals(field2)) {
538 System.out.println("fields are .equals");
539 } else {
540 System.out.println("ERROR: fields fail equality");
541 }
542 Method method1, method2;
543 try {
544 method1 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
545 method2 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
546 } catch (NoSuchMethodException nsme) {
547 throw new RuntimeException(nsme);
548 }
549 if (method1 == method2) {
550 System.out.println("ERROR: methods shouldn't have reference equality");
551 } else {
552 System.out.println("methods are unique");
553 }
554 if (method1.hashCode() == method2.hashCode() && method1.equals(method2)) {
555 System.out.println("methods are .equals");
556 } else {
557 System.out.println("ERROR: methods fail equality");
558 }
559 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800560
Elliott Hughes923e8b82012-03-23 11:44:07 -0700561 public static void main(String[] args) throws Exception {
jeffhao5d1ac922011-09-29 17:41:15 -0700562 Main test = new Main();
563 test.run();
564
Elliott Hughes741b5b72012-01-31 19:18:51 -0800565 checkAccess();
jeffhao5d1ac922011-09-29 17:41:15 -0700566 checkType();
Elliott Hughes923e8b82012-03-23 11:44:07 -0700567 checkClinitForFields();
568 checkClinitForMethods();
Elliott Hughes741b5b72012-01-31 19:18:51 -0800569 checkGeneric();
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 checkUnique();
jeffhao5d1ac922011-09-29 17:41:15 -0700571 }
572}
573
574
575class SuperTarget {
576 public SuperTarget() {
577 System.out.println("SuperTarget constructor ()V");
578 superInt = 1010101;
579 superClassInt = 1010102;
580 }
581
582 public int myMethod(float floatArg) {
583 System.out.println("myMethod (F)I " + floatArg);
584 return 6;
585 }
586
587 public int superInt;
588 public static int superClassInt;
589}
590
591class Target extends SuperTarget {
592 public Target() {
593 System.out.println("Target constructor ()V");
594 }
595
596 public Target(int ii, float ff) {
597 System.out.println("Target constructor (IF)V : ii="
598 + ii + " ff=" + ff);
599 anInt = ii;
600 }
601
602 public int myMethod(int intarg) throws NullPointerException, IOException {
603 System.out.println("myMethod (I)I");
604 System.out.println(" arg=" + intarg + " anInt=" + anInt);
605 return 5;
606 }
607
608 public int myMethod(String[] strarg, float f, char c) {
609 System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !");
610 return 7;
611 }
612
613 public static void myNoargMethod() {
614 System.out.println("myNoargMethod ()V");
615 }
616
617 public void throwingMethod() {
618 System.out.println("throwingMethod");
619 throw new NullPointerException("gratuitous throw!");
620 }
621
622 public void misc() {
623 System.out.println("misc");
624 }
625
626 public int anInt;
627 public String string1 = "hey";
628 public String string2 = "yo";
629 public String string3 = "there";
630 private String string4 = "naughty";
631 public static final String constantString = "a constant string";
632 private int aPrivateInt;
633
634 public final int cantTouchThis = 77;
635
636 public long pubLong = 0x1122334455667788L;
637
638 public static double staticDouble = 3.3;
639}
640
Elliott Hughes923e8b82012-03-23 11:44:07 -0700641class FieldNoisyInit {
642 static {
643 System.out.println("FieldNoisyInit is initializing");
644 //Throwable th = new Throwable();
645 //th.printStackTrace();
646 }
jeffhao5d1ac922011-09-29 17:41:15 -0700647}
648
Elliott Hughes923e8b82012-03-23 11:44:07 -0700649class FieldNoisyInitUser {
650 static {
651 System.out.println("FieldNoisyInitUser is initializing");
652 }
653 public static int staticField;
654 public static FieldNoisyInit noisy;
655}
656
657class MethodNoisyInit {
658 static {
659 System.out.println("MethodNoisyInit is initializing");
660 //Throwable th = new Throwable();
661 //th.printStackTrace();
662 }
663}
664
665class MethodNoisyInitUser {
666 static {
667 System.out.println("MethodNoisyInitUser is initializing");
668 }
669 public static void staticMethod() {}
670 public void createMethodNoisyInit(MethodNoisyInit ni) {}
jeffhao5d1ac922011-09-29 17:41:15 -0700671}