1 // Copyright 2006 The Android Open Source Project 2 3 import java.lang.reflect.*; 4 import java.io.IOException; 5 import java.util.Collections; 6 7 /** 8 * Reflection test. 9 */ 10 public 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); 275 System.out.println("ERROR: set-final succeeded"); 276 } catch (IllegalAccessException iae) { 277 System.out.println(" got expected set-final failure"); 278 } 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 284 field.set(instance, 88); // exercise Object version 285 intVal = field.getInt(instance); 286 System.out.println(" cantTouchThis is now " + intVal); 287 288 Constructor<Target> cons; 289 Target targ; 290 Object[] args; 291 292 cons = target.getConstructor(new Class[] { int.class,float.class }); 293 args = new Object[] { new Integer(7), new Float(3.3333) }; 294 System.out.println("cons modifiers=" + cons.getModifiers()); 295 targ = cons.newInstance(args); 296 targ.myMethod(17); 297 298 } 299 catch (Exception ex) { 300 System.out.println("----- unexpected exception -----"); 301 ex.printStackTrace(); 302 } 303 304 System.out.println("ReflectTest done!"); 305 } 306 307 public static void checkType() { 308 Method m; 309 310 try { 311 m = Collections.class.getDeclaredMethod("checkType", 312 Object.class, Class.class); 313 } catch (NoSuchMethodException nsme) { 314 nsme.printStackTrace(); 315 return; 316 } 317 318 m.setAccessible(true); 319 try { 320 m.invoke(null, new Object(), Object.class); 321 } catch (IllegalAccessException iae) { 322 iae.printStackTrace(); 323 return; 324 } catch (InvocationTargetException ite) { 325 ite.printStackTrace(); 326 return; 327 } 328 329 try { 330 System.out.println("checkType invoking null"); 331 m.invoke(null, new Object(), int.class); 332 System.out.println("ERROR: should throw InvocationTargetException"); 333 } catch (InvocationTargetException ite) { 334 System.out.println("checkType got expected exception"); 335 } catch (IllegalAccessException iae) { 336 iae.printStackTrace(); 337 return; 338 } 339 } 340 341 public static void checkInit() { 342 Class niuClass = NoisyInitUser.class; 343 Method[] methods; 344 345 methods = niuClass.getDeclaredMethods(); 346 System.out.println("got methods"); 347 /* neither NoisyInit nor NoisyInitUser should be initialized yet */ 348 NoisyInitUser niu = new NoisyInitUser(); 349 NoisyInit ni = new NoisyInit(); 350 } 351 352 public static void main(String[] args) { 353 Main test = new Main(); 354 test.run(); 355 356 checkType(); 357 checkInit(); 358 } 359 } 360 361 362 class SuperTarget { 363 public SuperTarget() { 364 System.out.println("SuperTarget constructor ()V"); 365 superInt = 1010101; 366 superClassInt = 1010102; 367 } 368 369 public int myMethod(float floatArg) { 370 System.out.println("myMethod (F)I " + floatArg); 371 return 6; 372 } 373 374 public int superInt; 375 public static int superClassInt; 376 } 377 378 class Target extends SuperTarget { 379 public Target() { 380 System.out.println("Target constructor ()V"); 381 } 382 383 public Target(int ii, float ff) { 384 System.out.println("Target constructor (IF)V : ii=" 385 + ii + " ff=" + ff); 386 anInt = ii; 387 } 388 389 public int myMethod(int intarg) throws NullPointerException, IOException { 390 System.out.println("myMethod (I)I"); 391 System.out.println(" arg=" + intarg + " anInt=" + anInt); 392 return 5; 393 } 394 395 public int myMethod(String[] strarg, float f, char c) { 396 System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !"); 397 return 7; 398 } 399 400 public static void myNoargMethod() { 401 System.out.println("myNoargMethod ()V"); 402 } 403 404 public void throwingMethod() { 405 System.out.println("throwingMethod"); 406 throw new NullPointerException("gratuitous throw!"); 407 } 408 409 public void misc() { 410 System.out.println("misc"); 411 } 412 413 public int anInt; 414 public String string1 = "hey"; 415 public String string2 = "yo"; 416 public String string3 = "there"; 417 private String string4 = "naughty"; 418 public static final String constantString = "a constant string"; 419 private int aPrivateInt; 420 421 public final int cantTouchThis = 77; 422 423 public long pubLong = 0x1122334455667788L; 424 425 public static double staticDouble = 3.3; 426 } 427 428 class NoisyInit { 429 static { 430 System.out.println("NoisyInit is initializing"); 431 //Throwable th = new Throwable(); 432 //th.printStackTrace(); 433 } 434 } 435 436 class NoisyInitUser { 437 static { 438 System.out.println("NoisyInitUser is initializing"); 439 } 440 public void createNoisyInit(NoisyInit ni) {} 441 } 442