blob: 5c6ca135b76854449b1b4d77edeb47c8e534bbd0 [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;
Piotr Jastrzebski68b539e2014-08-06 12:02:48 +010021import java.util.Arrays;
Elliott Hughes741b5b72012-01-31 19:18:51 -080022import java.util.List;
23import java.util.Map;
Piotr Jastrzebski68b539e2014-08-06 12:02:48 +010024import java.util.Set;
jeffhao5d1ac922011-09-29 17:41:15 -070025
26/**
27 * Reflection test.
28 */
29public class Main {
Elliott Hughes741b5b72012-01-31 19:18:51 -080030 private static boolean FULL_ACCESS_CHECKS = false; // b/5861201
31 public Main() {}
32 public Main(ArrayList<Integer> stuff) {}
33
jeffhao5d1ac922011-09-29 17:41:15 -070034 void printMethodInfo(Method meth) {
35 Class[] params, exceptions;
36 int i;
37
38 System.out.println("Method name is " + meth.getName());
39 System.out.println(" Declaring class is "
40 + meth.getDeclaringClass().getName());
41 params = meth.getParameterTypes();
42 for (i = 0; i < params.length; i++)
43 System.out.println(" Arg " + i + ": " + params[i].getName());
44 exceptions = meth.getExceptionTypes();
45 for (i = 0; i < exceptions.length; i++)
46 System.out.println(" Exc " + i + ": " + exceptions[i].getName());
47 System.out.println(" Return type is " + meth.getReturnType().getName());
48 System.out.println(" Access flags are 0x"
49 + Integer.toHexString(meth.getModifiers()));
50 //System.out.println(" GenericStr is " + meth.toGenericString());
51 }
52
53 void printFieldInfo(Field field) {
54 System.out.println("Field name is " + field.getName());
55 System.out.println(" Declaring class is "
56 + field.getDeclaringClass().getName());
57 System.out.println(" Field type is " + field.getType().getName());
58 System.out.println(" Access flags are 0x"
59 + Integer.toHexString(field.getModifiers()));
60 }
61
62 private void showStrings(Target instance)
63 throws NoSuchFieldException, IllegalAccessException {
64
65 Class target = Target.class;
66 String one, two, three, four;
67 Field field = null;
68
69 field = target.getField("string1");
70 one = (String) field.get(instance);
71
72 field = target.getField("string2");
73 two = (String) field.get(instance);
74
75 field = target.getField("string3");
76 three = (String) field.get(instance);
77
78 System.out.println(" ::: " + one + ":" + two + ":" + three);
79 }
80
Elliott Hughes741b5b72012-01-31 19:18:51 -080081 public static void checkAccess() {
82 try {
83 Class target = otherpackage.Other.class;
84 Object instance = new otherpackage.Other();
85 Method meth;
86
87 meth = target.getMethod("publicMethod", (Class[]) null);
88 meth.invoke(instance);
89
90 try {
91 meth = target.getMethod("packageMethod", (Class[]) null);
92 System.err.println("succeeded on package-scope method");
93 } catch (NoSuchMethodException nsme) {
94 // good
95 }
96
97
98 instance = otherpackage.Other.getInnerClassInstance();
99 target = instance.getClass();
100 meth = target.getMethod("innerMethod", (Class[]) null);
101 try {
102 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
103 meth.invoke(instance);
104 System.err.println("inner-method invoke unexpectedly worked");
105 } catch (IllegalAccessException iae) {
106 // good
107 }
108
109 Field field = target.getField("innerField");
110 try {
111 int x = field.getInt(instance);
112 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
113 System.err.println("field get unexpectedly worked: " + x);
114 } catch (IllegalAccessException iae) {
115 // good
116 }
117 } catch (Exception ex) {
118 System.out.println("----- unexpected exception -----");
119 ex.printStackTrace();
120 }
121 }
122
jeffhao5d1ac922011-09-29 17:41:15 -0700123 public void run() {
124 Class target = Target.class;
125 Method meth = null;
126 Field field = null;
127 boolean excep;
128
129 try {
130 meth = target.getMethod("myMethod", new Class[] { int.class });
131
132 if (meth.getDeclaringClass() != target)
133 throw new RuntimeException();
134 printMethodInfo(meth);
135
136 meth = target.getMethod("myMethod", new Class[] { float.class });
137 printMethodInfo(meth);
138
139 meth = target.getMethod("myNoargMethod", (Class[]) null);
140 printMethodInfo(meth);
141
142 meth = target.getMethod("myMethod",
143 new Class[] { String[].class, float.class, char.class });
144 printMethodInfo(meth);
145
146 Target instance = new Target();
147 Object[] argList = new Object[] {
148 new String[] { "hi there" },
149 new Float(3.1415926f),
150 new Character('Q')
151 };
152 System.out.println("Before, float is "
153 + ((Float)argList[1]).floatValue());
154
155 Integer boxval;
156 boxval = (Integer) meth.invoke(instance, argList);
157 System.out.println("Result of invoke: " + boxval.intValue());
158
159 System.out.println("Calling no-arg void-return method");
160 meth = target.getMethod("myNoargMethod", (Class[]) null);
161 meth.invoke(instance, (Object[]) null);
162
163 /* try invoking a method that throws an exception */
164 meth = target.getMethod("throwingMethod", (Class[]) null);
165 try {
166 meth.invoke(instance, (Object[]) null);
167 System.out.println("GLITCH: didn't throw");
168 } catch (InvocationTargetException ite) {
169 System.out.println("Invoke got expected exception:");
170 System.out.println(ite.getClass().getName());
171 System.out.println(ite.getCause());
172 }
173 catch (Exception ex) {
174 System.out.println("GLITCH: invoke got wrong exception:");
175 ex.printStackTrace();
176 }
177 System.out.println("");
178
179
180 field = target.getField("string1");
181 if (field.getDeclaringClass() != target)
182 throw new RuntimeException();
183 printFieldInfo(field);
184 String strVal = (String) field.get(instance);
185 System.out.println(" string1 value is '" + strVal + "'");
186
187 showStrings(instance);
188
189 field.set(instance, new String("a new string"));
190 strVal = (String) field.get(instance);
191 System.out.println(" string1 value is now '" + strVal + "'");
192
193 showStrings(instance);
194
195 try {
196 field.set(instance, new Object());
197 System.out.println("WARNING: able to store Object into String");
198 }
199 catch (IllegalArgumentException iae) {
200 System.out.println(" got expected illegal obj store exc");
201 }
202
203
204 try {
205 String four;
206 field = target.getField("string4");
207 four = (String) field.get(instance);
208 System.out.println("WARNING: able to access string4: "
209 + four);
210 }
211 catch (IllegalAccessException iae) {
212 System.out.println(" got expected access exc");
213 }
214 catch (NoSuchFieldException nsfe) {
215 System.out.println(" got the other expected access exc");
216 }
217 try {
218 String three;
219 field = target.getField("string3");
220 three = (String) field.get(this);
221 System.out.println("WARNING: able to get string3 in wrong obj: "
222 + three);
223 }
224 catch (IllegalArgumentException iae) {
225 System.out.println(" got expected arg exc");
226 }
227
228 /*
229 * Try setting a field to null.
230 */
231 String four;
232 field = target.getDeclaredField("string3");
233 field.set(instance, null);
234
235 /*
236 * Do some stuff with long.
237 */
238 long longVal;
239 field = target.getField("pubLong");
240 longVal = field.getLong(instance);
241 System.out.println("pubLong initial value is " +
242 Long.toHexString(longVal));
243 field.setLong(instance, 0x9988776655443322L);
244 longVal = field.getLong(instance);
245 System.out.println("pubLong new value is " +
246 Long.toHexString(longVal));
247
248
249 field = target.getField("superInt");
250 if (field.getDeclaringClass() == target)
251 throw new RuntimeException();
252 printFieldInfo(field);
253 int intVal = field.getInt(instance);
254 System.out.println(" superInt value is " + intVal);
255 Integer boxedIntVal = (Integer) field.get(instance);
256 System.out.println(" superInt boxed is " + boxedIntVal);
257
258 field.set(instance, new Integer(20202));
259 intVal = field.getInt(instance);
260 System.out.println(" superInt value is now " + intVal);
261 field.setShort(instance, (short)30303);
262 intVal = field.getInt(instance);
263 System.out.println(" superInt value (from short) is now " +intVal);
264 field.setInt(instance, 40404);
265 intVal = field.getInt(instance);
266 System.out.println(" superInt value is now " + intVal);
267 try {
268 field.set(instance, new Long(123));
269 System.out.println("FAIL: expected exception not thrown");
270 }
271 catch (IllegalArgumentException iae) {
272 System.out.println(" got expected long->int failure");
273 }
274 try {
275 field.setLong(instance, 123);
276 System.out.println("FAIL: expected exception not thrown");
277 }
278 catch (IllegalArgumentException iae) {
279 System.out.println(" got expected long->int failure");
280 }
281 try {
282 field.set(instance, new String("abc"));
283 System.out.println("FAIL: expected exception not thrown");
284 }
285 catch (IllegalArgumentException iae) {
286 System.out.println(" got expected string->int failure");
287 }
288
289 try {
290 field.getShort(instance);
291 System.out.println("FAIL: expected exception not thrown");
292 }
293 catch (IllegalArgumentException iae) {
294 System.out.println(" got expected int->short failure");
295 }
296
297 field = target.getField("superClassInt");
298 printFieldInfo(field);
299 int superClassIntVal = field.getInt(instance);
300 System.out.println(" superClassInt value is " + superClassIntVal);
301
302 field = target.getField("staticDouble");
303 printFieldInfo(field);
304 double staticDoubleVal = field.getDouble(null);
305 System.out.println(" staticDoubleVal value is " + staticDoubleVal);
306
307 try {
308 field.getLong(instance);
309 System.out.println("FAIL: expected exception not thrown");
310 }
311 catch (IllegalArgumentException iae) {
312 System.out.println(" got expected double->long failure");
313 }
314
315 excep = false;
316 try {
317 field = target.getField("aPrivateInt");
318 printFieldInfo(field);
319 }
320 catch (NoSuchFieldException nsfe) {
321 System.out.println("as expected: aPrivateInt not found");
322 excep = true;
323 }
324 if (!excep)
325 System.out.println("BUG: got aPrivateInt");
326
327
328 field = target.getField("constantString");
329 printFieldInfo(field);
330 String val = (String) field.get(instance);
331 System.out.println(" Constant test value is " + val);
332
333
334 field = target.getField("cantTouchThis");
335 printFieldInfo(field);
336 intVal = field.getInt(instance);
337 System.out.println(" cantTouchThis is " + intVal);
338 try {
339 field.setInt(instance, 99);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700340 System.out.println("ERROR: set-final did not throw exception");
jeffhao5d1ac922011-09-29 17:41:15 -0700341 } catch (IllegalAccessException iae) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700342 System.out.println(" as expected: set-final throws exception");
jeffhao5d1ac922011-09-29 17:41:15 -0700343 }
344 intVal = field.getInt(instance);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700345 System.out.println(" cantTouchThis is still " + intVal);
jeffhao5d1ac922011-09-29 17:41:15 -0700346
Brian Carlstromea46f952013-07-30 01:26:50 -0700347 System.out.println(" " + field + " accessible=" + field.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700348 field.setAccessible(true);
Brian Carlstromea46f952013-07-30 01:26:50 -0700349 System.out.println(" " + field + " accessible=" + field.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700350 field.setInt(instance, 87); // exercise int version
Elliott Hughes582a7d12011-10-10 18:38:42 -0700351 intVal = field.getInt(instance);
352 System.out.println(" cantTouchThis is now " + intVal);
jeffhao5d1ac922011-09-29 17:41:15 -0700353 field.set(instance, 88); // exercise Object version
354 intVal = field.getInt(instance);
355 System.out.println(" cantTouchThis is now " + intVal);
356
357 Constructor<Target> cons;
358 Target targ;
359 Object[] args;
360
361 cons = target.getConstructor(new Class[] { int.class,float.class });
362 args = new Object[] { new Integer(7), new Float(3.3333) };
363 System.out.println("cons modifiers=" + cons.getModifiers());
364 targ = cons.newInstance(args);
365 targ.myMethod(17);
366
Jeff Hao63f5b9e2014-04-23 14:51:46 -0700367 try {
368 Thrower thrower = Thrower.class.newInstance();
369 System.out.println("ERROR: Class.newInstance did not throw exception");
370 } catch (UnsupportedOperationException uoe) {
371 System.out.println("got expected exception for Class.newInstance");
372 } catch (Exception e) {
373 System.out.println("ERROR: Class.newInstance got unexpected exception: " +
374 e.getClass().getName());
375 }
376
377 try {
378 Constructor<Thrower> constructor = Thrower.class.getDeclaredConstructor();
379 Thrower thrower = constructor.newInstance();
380 System.out.println("ERROR: Constructor.newInstance did not throw exception");
381 } catch (InvocationTargetException ite) {
382 System.out.println("got expected exception for Constructor.newInstance");
383 } catch (Exception e) {
384 System.out.println("ERROR: Constructor.newInstance got unexpected exception: " +
385 e.getClass().getName());
386 }
387
Elliott Hughes741b5b72012-01-31 19:18:51 -0800388 } catch (Exception ex) {
jeffhao5d1ac922011-09-29 17:41:15 -0700389 System.out.println("----- unexpected exception -----");
390 ex.printStackTrace();
391 }
392
393 System.out.println("ReflectTest done!");
394 }
395
396 public static void checkType() {
397 Method m;
398
399 try {
400 m = Collections.class.getDeclaredMethod("checkType",
401 Object.class, Class.class);
402 } catch (NoSuchMethodException nsme) {
403 nsme.printStackTrace();
404 return;
405 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700406 System.out.println(m + " accessible=" + m.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700407 m.setAccessible(true);
Brian Carlstromea46f952013-07-30 01:26:50 -0700408 System.out.println(m + " accessible=" + m.isAccessible());
jeffhao5d1ac922011-09-29 17:41:15 -0700409 try {
410 m.invoke(null, new Object(), Object.class);
411 } catch (IllegalAccessException iae) {
412 iae.printStackTrace();
413 return;
414 } catch (InvocationTargetException ite) {
415 ite.printStackTrace();
416 return;
417 }
418
419 try {
Jeff Hao7a549462013-03-18 19:04:24 -0700420 String s = "Should be ignored";
421 m.invoke(s, new Object(), Object.class);
422 } catch (IllegalAccessException iae) {
423 iae.printStackTrace();
424 return;
425 } catch (InvocationTargetException ite) {
426 ite.printStackTrace();
427 return;
428 }
429
430 try {
jeffhao5d1ac922011-09-29 17:41:15 -0700431 System.out.println("checkType invoking null");
432 m.invoke(null, new Object(), int.class);
433 System.out.println("ERROR: should throw InvocationTargetException");
434 } catch (InvocationTargetException ite) {
435 System.out.println("checkType got expected exception");
436 } catch (IllegalAccessException iae) {
437 iae.printStackTrace();
438 return;
439 }
440 }
441
Elliott Hughes923e8b82012-03-23 11:44:07 -0700442 public static void checkClinitForFields() throws Exception {
443 // Loading a class constant shouldn't run <clinit>.
444 System.out.println("calling const-class FieldNoisyInitUser.class");
445 Class niuClass = FieldNoisyInitUser.class;
446 System.out.println("called const-class FieldNoisyInitUser.class");
jeffhao5d1ac922011-09-29 17:41:15 -0700447
Elliott Hughes923e8b82012-03-23 11:44:07 -0700448 // Getting the declared fields doesn't run <clinit>.
449 Field[] fields = niuClass.getDeclaredFields();
450 System.out.println("got fields");
Elliott Hughes741b5b72012-01-31 19:18:51 -0800451
Elliott Hughes923e8b82012-03-23 11:44:07 -0700452 Field field = niuClass.getField("staticField");
453 System.out.println("got field");
454 field.get(null);
455 System.out.println("read field value");
456
457 // FieldNoisyInitUser should now be initialized, but FieldNoisyInit shouldn't be initialized yet.
458 FieldNoisyInitUser niu = new FieldNoisyInitUser();
459 FieldNoisyInit ni = new FieldNoisyInit();
460
461 System.out.println("");
462 }
463
464 public static void checkClinitForMethods() throws Exception {
465 // Loading a class constant shouldn't run <clinit>.
466 System.out.println("calling const-class MethodNoisyInitUser.class");
467 Class niuClass = MethodNoisyInitUser.class;
468 System.out.println("called const-class MethodNoisyInitUser.class");
469
470 // Getting the declared methods doesn't run <clinit>.
471 Method[] methods = niuClass.getDeclaredMethods();
472 System.out.println("got methods");
473
474 Method method = niuClass.getMethod("staticMethod", (Class[]) null);
475 System.out.println("got method");
476 method.invoke(null);
477 System.out.println("invoked method");
478
479 // MethodNoisyInitUser should now be initialized, but MethodNoisyInit shouldn't be initialized yet.
480 MethodNoisyInitUser niu = new MethodNoisyInitUser();
481 MethodNoisyInit ni = new MethodNoisyInit();
482
483 System.out.println("");
jeffhao5d1ac922011-09-29 17:41:15 -0700484 }
485
Elliott Hughes741b5b72012-01-31 19:18:51 -0800486
487 /*
488 * Test some generic type stuff.
489 */
490 public List<String> dummy;
491 public Map<Integer,String> fancyMethod(ArrayList<String> blah) { return null; }
492 public static void checkGeneric() {
493 Field field;
494 try {
495 field = Main.class.getField("dummy");
496 } catch (NoSuchFieldException nsfe) {
497 throw new RuntimeException(nsfe);
498 }
499 Type listType = field.getGenericType();
500 System.out.println("generic field: " + listType);
501
502 Method method;
503 try {
504 method = Main.class.getMethod("fancyMethod",
505 new Class[] { ArrayList.class });
506 } catch (NoSuchMethodException nsme) {
507 throw new RuntimeException(nsme);
508 }
509 Type[] parmTypes = method.getGenericParameterTypes();
510 Type ret = method.getGenericReturnType();
511 System.out.println("generic method " + method.getName() + " params='"
512 + stringifyTypeArray(parmTypes) + "' ret='" + ret + "'");
513
514 Constructor ctor;
515 try {
516 ctor = Main.class.getConstructor(new Class[] { ArrayList.class });
517 } catch (NoSuchMethodException nsme) {
518 throw new RuntimeException(nsme);
519 }
520 parmTypes = ctor.getGenericParameterTypes();
521 System.out.println("generic ctor " + ctor.getName() + " params='"
522 + stringifyTypeArray(parmTypes) + "'");
523 }
524
525 /*
526 * Convert an array of Type into a string. Start with an array count.
527 */
528 private static String stringifyTypeArray(Type[] types) {
529 StringBuilder stb = new StringBuilder();
530 boolean first = true;
531
532 stb.append("[" + types.length + "]");
533
534 for (Type t: types) {
535 if (first) {
536 stb.append(" ");
537 first = false;
538 } else {
539 stb.append(", ");
540 }
541 stb.append(t.toString());
542 }
543
544 return stb.toString();
545 }
546
Brian Carlstromea46f952013-07-30 01:26:50 -0700547 public static void checkUnique() {
548 Field field1, field2;
549 try {
550 field1 = Main.class.getField("dummy");
551 field2 = Main.class.getField("dummy");
552 } catch (NoSuchFieldException nsfe) {
553 throw new RuntimeException(nsfe);
554 }
555 if (field1 == field2) {
556 System.out.println("ERROR: fields shouldn't have reference equality");
557 } else {
558 System.out.println("fields are unique");
559 }
560 if (field1.hashCode() == field2.hashCode() && field1.equals(field2)) {
561 System.out.println("fields are .equals");
562 } else {
563 System.out.println("ERROR: fields fail equality");
564 }
565 Method method1, method2;
566 try {
567 method1 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
568 method2 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
569 } catch (NoSuchMethodException nsme) {
570 throw new RuntimeException(nsme);
571 }
572 if (method1 == method2) {
573 System.out.println("ERROR: methods shouldn't have reference equality");
574 } else {
575 System.out.println("methods are unique");
576 }
577 if (method1.hashCode() == method2.hashCode() && method1.equals(method2)) {
578 System.out.println("methods are .equals");
579 } else {
580 System.out.println("ERROR: methods fail equality");
581 }
582 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800583
Piotr Jastrzebski68b539e2014-08-06 12:02:48 +0100584 public static void checkParametrizedTypeEqualsAndHashCode() {
585 Method method1;
586 Method method2;
587 Method method3;
588 try {
589 method1 = ParametrizedTypeTest.class.getDeclaredMethod("aMethod", Set.class);
590 method2 = ParametrizedTypeTest.class.getDeclaredMethod("aMethod", Set.class);
591 method3 = ParametrizedTypeTest.class.getDeclaredMethod("aMethodIdentical", Set.class);
592 } catch (NoSuchMethodException nsme) {
593 throw new RuntimeException(nsme);
594 }
595
596 List<Type> types1 = Arrays.asList(method1.getGenericParameterTypes());
597 List<Type> types2 = Arrays.asList(method2.getGenericParameterTypes());
598 List<Type> types3 = Arrays.asList(method3.getGenericParameterTypes());
599
600 Type type1 = types1.get(0);
601 Type type2 = types2.get(0);
602 Type type3 = types3.get(0);
603
604 if (type1 instanceof ParameterizedType) {
605 System.out.println("type1 is a ParameterizedType");
606 }
607 if (type2 instanceof ParameterizedType) {
608 System.out.println("type2 is a ParameterizedType");
609 }
610 if (type3 instanceof ParameterizedType) {
611 System.out.println("type3 is a ParameterizedType");
612 }
613
614 if (type1.equals(type2)) {
615 System.out.println("type1("+type1+") equals type2("+type2+")");
616 } else {
617 System.out.println("type1("+type1+") does not equal type2("+type2+")");
618 }
619
620 if (type1.equals(type3)) {
621 System.out.println("type1("+type1+") equals type3("+type3+")");
622 } else {
623 System.out.println("type1("+type1+") does not equal type3("+type3+")");
624 }
625 if (type1.hashCode() == type2.hashCode()) {
626 System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode");
627 } else {
628 System.out.println(
629 "type1("+type1+") hashCode does not equal type2("+type2+") hashCode");
630 }
631
632 if (type1.hashCode() == type3.hashCode()) {
633 System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode");
634 } else {
635 System.out.println(
636 "type1("+type1+") hashCode does not equal type3("+type3+") hashCode");
637 }
638 }
639
640 public static void checkGenericArrayTypeEqualsAndHashCode() {
641 Method method1;
642 Method method2;
643 Method method3;
644 try {
645 method1 = GenericArrayTypeTest.class.getDeclaredMethod("aMethod", Object[].class);
646 method2 = GenericArrayTypeTest.class.getDeclaredMethod("aMethod", Object[].class);
647 method3 = GenericArrayTypeTest.class.getDeclaredMethod("aMethodIdentical", Object[].class);
648 } catch (NoSuchMethodException nsme) {
649 throw new RuntimeException(nsme);
650 }
651
652 List<Type> types1 = Arrays.asList(method1.getGenericParameterTypes());
653 List<Type> types2 = Arrays.asList(method2.getGenericParameterTypes());
654 List<Type> types3 = Arrays.asList(method3.getGenericParameterTypes());
655
656 Type type1 = types1.get(0);
657 Type type2 = types2.get(0);
658 Type type3 = types3.get(0);
659
660 if (type1 instanceof GenericArrayType) {
661 System.out.println("type1 is a GenericArrayType");
662 }
663 if (type2 instanceof GenericArrayType) {
664 System.out.println("type2 is a GenericArrayType");
665 }
666 if (type3 instanceof GenericArrayType) {
667 System.out.println("type3 is a GenericArrayType");
668 }
669
670 if (type1.equals(type2)) {
671 System.out.println("type1("+type1+") equals type2("+type2+")");
672 } else {
673 System.out.println("type1("+type1+") does not equal type2("+type2+")");
674 }
675
676 if (type1.equals(type3)) {
677 System.out.println("type1("+type1+") equals type3("+type3+")");
678 } else {
679 System.out.println("type1("+type1+") does not equal type3("+type3+")");
680 }
681 if (type1.hashCode() == type2.hashCode()) {
682 System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode");
683 } else {
684 System.out.println(
685 "type1("+type1+") hashCode does not equal type2("+type2+") hashCode");
686 }
687
688 if (type1.hashCode() == type3.hashCode()) {
689 System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode");
690 } else {
691 System.out.println(
692 "type1("+type1+") hashCode does not equal type3("+type3+") hashCode");
693 }
694 }
695
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700696 private static void checkGetDeclaredConstructor() {
697 try {
698 Method.class.getDeclaredConstructor().setAccessible(true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700699 System.out.print("Didn't get an exception from Method.class.getDeclaredConstructor().setAccessible");
700 } catch (SecurityException e) {
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700701 } catch (NoSuchMethodException e) {
702 } catch (Exception e) {
703 System.out.print(e);
704 }
705 try {
706 Field.class.getDeclaredConstructor().setAccessible(true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700707 System.out.print("Didn't get an exception from Field.class.getDeclaredConstructor().setAccessible");
708 } catch (SecurityException e) {
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700709 } catch (NoSuchMethodException e) {
710 } catch (Exception e) {
711 System.out.print(e);
712 }
713 try {
714 Class.class.getDeclaredConstructor().setAccessible(true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700715 System.out.print("Didn't get an exception from Class.class.getDeclaredConstructor().setAccessible");
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700716 } catch (SecurityException e) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700717 } catch (NoSuchMethodException e) {
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700718 } catch (Exception e) {
719 System.out.print(e);
720 }
721 }
722
Elliott Hughes923e8b82012-03-23 11:44:07 -0700723 public static void main(String[] args) throws Exception {
jeffhao5d1ac922011-09-29 17:41:15 -0700724 Main test = new Main();
725 test.run();
726
Mathieu Chartier3ed4c542014-08-08 15:10:11 -0700727 checkGetDeclaredConstructor();
Elliott Hughes741b5b72012-01-31 19:18:51 -0800728 checkAccess();
jeffhao5d1ac922011-09-29 17:41:15 -0700729 checkType();
Elliott Hughes923e8b82012-03-23 11:44:07 -0700730 checkClinitForFields();
731 checkClinitForMethods();
Elliott Hughes741b5b72012-01-31 19:18:51 -0800732 checkGeneric();
Brian Carlstromea46f952013-07-30 01:26:50 -0700733 checkUnique();
Piotr Jastrzebski68b539e2014-08-06 12:02:48 +0100734 checkParametrizedTypeEqualsAndHashCode();
735 checkGenericArrayTypeEqualsAndHashCode();
jeffhao5d1ac922011-09-29 17:41:15 -0700736 }
737}
738
739
740class SuperTarget {
741 public SuperTarget() {
742 System.out.println("SuperTarget constructor ()V");
743 superInt = 1010101;
744 superClassInt = 1010102;
745 }
746
747 public int myMethod(float floatArg) {
748 System.out.println("myMethod (F)I " + floatArg);
749 return 6;
750 }
751
752 public int superInt;
753 public static int superClassInt;
754}
755
756class Target extends SuperTarget {
757 public Target() {
758 System.out.println("Target constructor ()V");
759 }
760
761 public Target(int ii, float ff) {
762 System.out.println("Target constructor (IF)V : ii="
763 + ii + " ff=" + ff);
764 anInt = ii;
765 }
766
767 public int myMethod(int intarg) throws NullPointerException, IOException {
768 System.out.println("myMethod (I)I");
769 System.out.println(" arg=" + intarg + " anInt=" + anInt);
770 return 5;
771 }
772
773 public int myMethod(String[] strarg, float f, char c) {
774 System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !");
775 return 7;
776 }
777
778 public static void myNoargMethod() {
779 System.out.println("myNoargMethod ()V");
780 }
781
782 public void throwingMethod() {
783 System.out.println("throwingMethod");
784 throw new NullPointerException("gratuitous throw!");
785 }
786
787 public void misc() {
788 System.out.println("misc");
789 }
790
791 public int anInt;
792 public String string1 = "hey";
793 public String string2 = "yo";
794 public String string3 = "there";
795 private String string4 = "naughty";
796 public static final String constantString = "a constant string";
797 private int aPrivateInt;
798
799 public final int cantTouchThis = 77;
800
801 public long pubLong = 0x1122334455667788L;
802
803 public static double staticDouble = 3.3;
804}
805
Elliott Hughes923e8b82012-03-23 11:44:07 -0700806class FieldNoisyInit {
807 static {
808 System.out.println("FieldNoisyInit is initializing");
809 //Throwable th = new Throwable();
810 //th.printStackTrace();
811 }
jeffhao5d1ac922011-09-29 17:41:15 -0700812}
813
Elliott Hughes923e8b82012-03-23 11:44:07 -0700814class FieldNoisyInitUser {
815 static {
816 System.out.println("FieldNoisyInitUser is initializing");
817 }
818 public static int staticField;
819 public static FieldNoisyInit noisy;
820}
821
822class MethodNoisyInit {
823 static {
824 System.out.println("MethodNoisyInit is initializing");
825 //Throwable th = new Throwable();
826 //th.printStackTrace();
827 }
828}
829
830class MethodNoisyInitUser {
831 static {
832 System.out.println("MethodNoisyInitUser is initializing");
833 }
834 public static void staticMethod() {}
835 public void createMethodNoisyInit(MethodNoisyInit ni) {}
jeffhao5d1ac922011-09-29 17:41:15 -0700836}
Jeff Hao63f5b9e2014-04-23 14:51:46 -0700837
838class Thrower {
839 public Thrower() throws UnsupportedOperationException {
840 throw new UnsupportedOperationException();
841 }
842}
Piotr Jastrzebski68b539e2014-08-06 12:02:48 +0100843
844class ParametrizedTypeTest {
845 public void aMethod(Set<String> names) {}
846 public void aMethodIdentical(Set<String> names) {}
847}
848
849class GenericArrayTypeTest<T> {
850 public void aMethod(T[] names) {}
851 public void aMethodIdentical(T[] names) {}
852}