Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | import java.lang.reflect.*; |
| 18 | import java.io.IOException; |
| 19 | import java.util.Collections; |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
Piotr Jastrzebski | 68b539e | 2014-08-06 12:02:48 +0100 | [diff] [blame] | 21 | import java.util.Arrays; |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.Map; |
Piotr Jastrzebski | 68b539e | 2014-08-06 12:02:48 +0100 | [diff] [blame] | 24 | import java.util.Set; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Reflection test. |
| 28 | */ |
| 29 | public class Main { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 30 | private static boolean FULL_ACCESS_CHECKS = false; // b/5861201 |
| 31 | public Main() {} |
| 32 | public Main(ArrayList<Integer> stuff) {} |
| 33 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 34 | 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 Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 81 | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 123 | 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), |
Fredrik Roubert | 3152c82 | 2015-07-28 00:14:08 +0200 | [diff] [blame] | 150 | new Character('\u2714') |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | /* |
Mathieu Chartier | 4e06778 | 2015-05-13 13:13:24 -0700 | [diff] [blame] | 236 | * Try getDeclaredField on a non-existant field. |
| 237 | */ |
| 238 | try { |
| 239 | field = target.getDeclaredField("nonExistant"); |
| 240 | System.out.println("ERROR: Expected NoSuchFieldException"); |
| 241 | } catch (NoSuchFieldException nsfe) { |
| 242 | String msg = nsfe.getMessage(); |
| 243 | if (!msg.contains("Target;")) { |
| 244 | System.out.println(" NoSuchFieldException '" + msg + |
| 245 | "' didn't contain class"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 250 | * Do some stuff with long. |
| 251 | */ |
| 252 | long longVal; |
| 253 | field = target.getField("pubLong"); |
| 254 | longVal = field.getLong(instance); |
| 255 | System.out.println("pubLong initial value is " + |
| 256 | Long.toHexString(longVal)); |
| 257 | field.setLong(instance, 0x9988776655443322L); |
| 258 | longVal = field.getLong(instance); |
| 259 | System.out.println("pubLong new value is " + |
| 260 | Long.toHexString(longVal)); |
| 261 | |
| 262 | |
| 263 | field = target.getField("superInt"); |
| 264 | if (field.getDeclaringClass() == target) |
| 265 | throw new RuntimeException(); |
| 266 | printFieldInfo(field); |
| 267 | int intVal = field.getInt(instance); |
| 268 | System.out.println(" superInt value is " + intVal); |
| 269 | Integer boxedIntVal = (Integer) field.get(instance); |
| 270 | System.out.println(" superInt boxed is " + boxedIntVal); |
| 271 | |
| 272 | field.set(instance, new Integer(20202)); |
| 273 | intVal = field.getInt(instance); |
| 274 | System.out.println(" superInt value is now " + intVal); |
| 275 | field.setShort(instance, (short)30303); |
| 276 | intVal = field.getInt(instance); |
| 277 | System.out.println(" superInt value (from short) is now " +intVal); |
| 278 | field.setInt(instance, 40404); |
| 279 | intVal = field.getInt(instance); |
| 280 | System.out.println(" superInt value is now " + intVal); |
| 281 | try { |
| 282 | field.set(instance, new Long(123)); |
| 283 | System.out.println("FAIL: expected exception not thrown"); |
| 284 | } |
| 285 | catch (IllegalArgumentException iae) { |
| 286 | System.out.println(" got expected long->int failure"); |
| 287 | } |
| 288 | try { |
| 289 | field.setLong(instance, 123); |
| 290 | System.out.println("FAIL: expected exception not thrown"); |
| 291 | } |
| 292 | catch (IllegalArgumentException iae) { |
| 293 | System.out.println(" got expected long->int failure"); |
| 294 | } |
| 295 | try { |
| 296 | field.set(instance, new String("abc")); |
| 297 | System.out.println("FAIL: expected exception not thrown"); |
| 298 | } |
| 299 | catch (IllegalArgumentException iae) { |
| 300 | System.out.println(" got expected string->int failure"); |
| 301 | } |
| 302 | |
| 303 | try { |
| 304 | field.getShort(instance); |
| 305 | System.out.println("FAIL: expected exception not thrown"); |
| 306 | } |
| 307 | catch (IllegalArgumentException iae) { |
| 308 | System.out.println(" got expected int->short failure"); |
| 309 | } |
| 310 | |
| 311 | field = target.getField("superClassInt"); |
| 312 | printFieldInfo(field); |
| 313 | int superClassIntVal = field.getInt(instance); |
| 314 | System.out.println(" superClassInt value is " + superClassIntVal); |
| 315 | |
| 316 | field = target.getField("staticDouble"); |
| 317 | printFieldInfo(field); |
| 318 | double staticDoubleVal = field.getDouble(null); |
| 319 | System.out.println(" staticDoubleVal value is " + staticDoubleVal); |
| 320 | |
| 321 | try { |
| 322 | field.getLong(instance); |
| 323 | System.out.println("FAIL: expected exception not thrown"); |
| 324 | } |
| 325 | catch (IllegalArgumentException iae) { |
| 326 | System.out.println(" got expected double->long failure"); |
| 327 | } |
| 328 | |
| 329 | excep = false; |
| 330 | try { |
| 331 | field = target.getField("aPrivateInt"); |
| 332 | printFieldInfo(field); |
| 333 | } |
| 334 | catch (NoSuchFieldException nsfe) { |
| 335 | System.out.println("as expected: aPrivateInt not found"); |
| 336 | excep = true; |
| 337 | } |
| 338 | if (!excep) |
| 339 | System.out.println("BUG: got aPrivateInt"); |
| 340 | |
| 341 | |
| 342 | field = target.getField("constantString"); |
| 343 | printFieldInfo(field); |
| 344 | String val = (String) field.get(instance); |
| 345 | System.out.println(" Constant test value is " + val); |
| 346 | |
| 347 | |
| 348 | field = target.getField("cantTouchThis"); |
| 349 | printFieldInfo(field); |
| 350 | intVal = field.getInt(instance); |
| 351 | System.out.println(" cantTouchThis is " + intVal); |
| 352 | try { |
| 353 | field.setInt(instance, 99); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 354 | System.out.println("ERROR: set-final did not throw exception"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 355 | } catch (IllegalAccessException iae) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 356 | System.out.println(" as expected: set-final throws exception"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 357 | } |
| 358 | intVal = field.getInt(instance); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 359 | System.out.println(" cantTouchThis is still " + intVal); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 360 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 361 | System.out.println(" " + field + " accessible=" + field.isAccessible()); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 362 | field.setAccessible(true); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 363 | System.out.println(" " + field + " accessible=" + field.isAccessible()); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 364 | field.setInt(instance, 87); // exercise int version |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame] | 365 | intVal = field.getInt(instance); |
| 366 | System.out.println(" cantTouchThis is now " + intVal); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 367 | field.set(instance, 88); // exercise Object version |
| 368 | intVal = field.getInt(instance); |
| 369 | System.out.println(" cantTouchThis is now " + intVal); |
| 370 | |
| 371 | Constructor<Target> cons; |
| 372 | Target targ; |
| 373 | Object[] args; |
| 374 | |
| 375 | cons = target.getConstructor(new Class[] { int.class,float.class }); |
| 376 | args = new Object[] { new Integer(7), new Float(3.3333) }; |
| 377 | System.out.println("cons modifiers=" + cons.getModifiers()); |
| 378 | targ = cons.newInstance(args); |
| 379 | targ.myMethod(17); |
| 380 | |
Jeff Hao | 63f5b9e | 2014-04-23 14:51:46 -0700 | [diff] [blame] | 381 | try { |
| 382 | Thrower thrower = Thrower.class.newInstance(); |
| 383 | System.out.println("ERROR: Class.newInstance did not throw exception"); |
| 384 | } catch (UnsupportedOperationException uoe) { |
| 385 | System.out.println("got expected exception for Class.newInstance"); |
| 386 | } catch (Exception e) { |
| 387 | System.out.println("ERROR: Class.newInstance got unexpected exception: " + |
| 388 | e.getClass().getName()); |
| 389 | } |
| 390 | |
| 391 | try { |
| 392 | Constructor<Thrower> constructor = Thrower.class.getDeclaredConstructor(); |
| 393 | Thrower thrower = constructor.newInstance(); |
| 394 | System.out.println("ERROR: Constructor.newInstance did not throw exception"); |
| 395 | } catch (InvocationTargetException ite) { |
| 396 | System.out.println("got expected exception for Constructor.newInstance"); |
| 397 | } catch (Exception e) { |
| 398 | System.out.println("ERROR: Constructor.newInstance got unexpected exception: " + |
| 399 | e.getClass().getName()); |
| 400 | } |
| 401 | |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 402 | } catch (Exception ex) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 403 | System.out.println("----- unexpected exception -----"); |
| 404 | ex.printStackTrace(); |
| 405 | } |
| 406 | |
| 407 | System.out.println("ReflectTest done!"); |
| 408 | } |
| 409 | |
| 410 | public static void checkType() { |
| 411 | Method m; |
| 412 | |
| 413 | try { |
| 414 | m = Collections.class.getDeclaredMethod("checkType", |
| 415 | Object.class, Class.class); |
| 416 | } catch (NoSuchMethodException nsme) { |
| 417 | nsme.printStackTrace(); |
| 418 | return; |
| 419 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 420 | System.out.println(m + " accessible=" + m.isAccessible()); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 421 | m.setAccessible(true); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 422 | System.out.println(m + " accessible=" + m.isAccessible()); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 423 | try { |
| 424 | m.invoke(null, new Object(), Object.class); |
| 425 | } catch (IllegalAccessException iae) { |
| 426 | iae.printStackTrace(); |
| 427 | return; |
| 428 | } catch (InvocationTargetException ite) { |
| 429 | ite.printStackTrace(); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | try { |
Jeff Hao | 7a54946 | 2013-03-18 19:04:24 -0700 | [diff] [blame] | 434 | String s = "Should be ignored"; |
| 435 | m.invoke(s, new Object(), Object.class); |
| 436 | } catch (IllegalAccessException iae) { |
| 437 | iae.printStackTrace(); |
| 438 | return; |
| 439 | } catch (InvocationTargetException ite) { |
| 440 | ite.printStackTrace(); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | try { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 445 | System.out.println("checkType invoking null"); |
| 446 | m.invoke(null, new Object(), int.class); |
| 447 | System.out.println("ERROR: should throw InvocationTargetException"); |
| 448 | } catch (InvocationTargetException ite) { |
| 449 | System.out.println("checkType got expected exception"); |
| 450 | } catch (IllegalAccessException iae) { |
| 451 | iae.printStackTrace(); |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 456 | public static void checkClinitForFields() throws Exception { |
| 457 | // Loading a class constant shouldn't run <clinit>. |
| 458 | System.out.println("calling const-class FieldNoisyInitUser.class"); |
| 459 | Class niuClass = FieldNoisyInitUser.class; |
| 460 | System.out.println("called const-class FieldNoisyInitUser.class"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 461 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 462 | // Getting the declared fields doesn't run <clinit>. |
| 463 | Field[] fields = niuClass.getDeclaredFields(); |
| 464 | System.out.println("got fields"); |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 465 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 466 | Field field = niuClass.getField("staticField"); |
| 467 | System.out.println("got field"); |
| 468 | field.get(null); |
| 469 | System.out.println("read field value"); |
| 470 | |
| 471 | // FieldNoisyInitUser should now be initialized, but FieldNoisyInit shouldn't be initialized yet. |
| 472 | FieldNoisyInitUser niu = new FieldNoisyInitUser(); |
| 473 | FieldNoisyInit ni = new FieldNoisyInit(); |
| 474 | |
| 475 | System.out.println(""); |
| 476 | } |
| 477 | |
| 478 | public static void checkClinitForMethods() throws Exception { |
| 479 | // Loading a class constant shouldn't run <clinit>. |
| 480 | System.out.println("calling const-class MethodNoisyInitUser.class"); |
| 481 | Class niuClass = MethodNoisyInitUser.class; |
| 482 | System.out.println("called const-class MethodNoisyInitUser.class"); |
| 483 | |
| 484 | // Getting the declared methods doesn't run <clinit>. |
| 485 | Method[] methods = niuClass.getDeclaredMethods(); |
| 486 | System.out.println("got methods"); |
| 487 | |
| 488 | Method method = niuClass.getMethod("staticMethod", (Class[]) null); |
| 489 | System.out.println("got method"); |
| 490 | method.invoke(null); |
| 491 | System.out.println("invoked method"); |
| 492 | |
| 493 | // MethodNoisyInitUser should now be initialized, but MethodNoisyInit shouldn't be initialized yet. |
| 494 | MethodNoisyInitUser niu = new MethodNoisyInitUser(); |
| 495 | MethodNoisyInit ni = new MethodNoisyInit(); |
| 496 | |
| 497 | System.out.println(""); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * Test some generic type stuff. |
| 503 | */ |
| 504 | public List<String> dummy; |
| 505 | public Map<Integer,String> fancyMethod(ArrayList<String> blah) { return null; } |
| 506 | public static void checkGeneric() { |
| 507 | Field field; |
| 508 | try { |
| 509 | field = Main.class.getField("dummy"); |
| 510 | } catch (NoSuchFieldException nsfe) { |
| 511 | throw new RuntimeException(nsfe); |
| 512 | } |
| 513 | Type listType = field.getGenericType(); |
| 514 | System.out.println("generic field: " + listType); |
| 515 | |
| 516 | Method method; |
| 517 | try { |
| 518 | method = Main.class.getMethod("fancyMethod", |
| 519 | new Class[] { ArrayList.class }); |
| 520 | } catch (NoSuchMethodException nsme) { |
| 521 | throw new RuntimeException(nsme); |
| 522 | } |
| 523 | Type[] parmTypes = method.getGenericParameterTypes(); |
| 524 | Type ret = method.getGenericReturnType(); |
| 525 | System.out.println("generic method " + method.getName() + " params='" |
| 526 | + stringifyTypeArray(parmTypes) + "' ret='" + ret + "'"); |
| 527 | |
| 528 | Constructor ctor; |
| 529 | try { |
| 530 | ctor = Main.class.getConstructor(new Class[] { ArrayList.class }); |
| 531 | } catch (NoSuchMethodException nsme) { |
| 532 | throw new RuntimeException(nsme); |
| 533 | } |
| 534 | parmTypes = ctor.getGenericParameterTypes(); |
| 535 | System.out.println("generic ctor " + ctor.getName() + " params='" |
| 536 | + stringifyTypeArray(parmTypes) + "'"); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Convert an array of Type into a string. Start with an array count. |
| 541 | */ |
| 542 | private static String stringifyTypeArray(Type[] types) { |
| 543 | StringBuilder stb = new StringBuilder(); |
| 544 | boolean first = true; |
| 545 | |
| 546 | stb.append("[" + types.length + "]"); |
| 547 | |
| 548 | for (Type t: types) { |
| 549 | if (first) { |
| 550 | stb.append(" "); |
| 551 | first = false; |
| 552 | } else { |
| 553 | stb.append(", "); |
| 554 | } |
| 555 | stb.append(t.toString()); |
| 556 | } |
| 557 | |
| 558 | return stb.toString(); |
| 559 | } |
| 560 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 561 | public static void checkUnique() { |
| 562 | Field field1, field2; |
| 563 | try { |
| 564 | field1 = Main.class.getField("dummy"); |
| 565 | field2 = Main.class.getField("dummy"); |
| 566 | } catch (NoSuchFieldException nsfe) { |
| 567 | throw new RuntimeException(nsfe); |
| 568 | } |
| 569 | if (field1 == field2) { |
| 570 | System.out.println("ERROR: fields shouldn't have reference equality"); |
| 571 | } else { |
| 572 | System.out.println("fields are unique"); |
| 573 | } |
| 574 | if (field1.hashCode() == field2.hashCode() && field1.equals(field2)) { |
| 575 | System.out.println("fields are .equals"); |
| 576 | } else { |
| 577 | System.out.println("ERROR: fields fail equality"); |
| 578 | } |
| 579 | Method method1, method2; |
| 580 | try { |
| 581 | method1 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class }); |
| 582 | method2 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class }); |
| 583 | } catch (NoSuchMethodException nsme) { |
| 584 | throw new RuntimeException(nsme); |
| 585 | } |
| 586 | if (method1 == method2) { |
| 587 | System.out.println("ERROR: methods shouldn't have reference equality"); |
| 588 | } else { |
| 589 | System.out.println("methods are unique"); |
| 590 | } |
| 591 | if (method1.hashCode() == method2.hashCode() && method1.equals(method2)) { |
| 592 | System.out.println("methods are .equals"); |
| 593 | } else { |
| 594 | System.out.println("ERROR: methods fail equality"); |
| 595 | } |
| 596 | } |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 597 | |
Piotr Jastrzebski | 68b539e | 2014-08-06 12:02:48 +0100 | [diff] [blame] | 598 | public static void checkParametrizedTypeEqualsAndHashCode() { |
| 599 | Method method1; |
| 600 | Method method2; |
| 601 | Method method3; |
| 602 | try { |
| 603 | method1 = ParametrizedTypeTest.class.getDeclaredMethod("aMethod", Set.class); |
| 604 | method2 = ParametrizedTypeTest.class.getDeclaredMethod("aMethod", Set.class); |
| 605 | method3 = ParametrizedTypeTest.class.getDeclaredMethod("aMethodIdentical", Set.class); |
| 606 | } catch (NoSuchMethodException nsme) { |
| 607 | throw new RuntimeException(nsme); |
| 608 | } |
| 609 | |
| 610 | List<Type> types1 = Arrays.asList(method1.getGenericParameterTypes()); |
| 611 | List<Type> types2 = Arrays.asList(method2.getGenericParameterTypes()); |
| 612 | List<Type> types3 = Arrays.asList(method3.getGenericParameterTypes()); |
| 613 | |
| 614 | Type type1 = types1.get(0); |
| 615 | Type type2 = types2.get(0); |
| 616 | Type type3 = types3.get(0); |
| 617 | |
| 618 | if (type1 instanceof ParameterizedType) { |
| 619 | System.out.println("type1 is a ParameterizedType"); |
| 620 | } |
| 621 | if (type2 instanceof ParameterizedType) { |
| 622 | System.out.println("type2 is a ParameterizedType"); |
| 623 | } |
| 624 | if (type3 instanceof ParameterizedType) { |
| 625 | System.out.println("type3 is a ParameterizedType"); |
| 626 | } |
| 627 | |
| 628 | if (type1.equals(type2)) { |
| 629 | System.out.println("type1("+type1+") equals type2("+type2+")"); |
| 630 | } else { |
| 631 | System.out.println("type1("+type1+") does not equal type2("+type2+")"); |
| 632 | } |
| 633 | |
| 634 | if (type1.equals(type3)) { |
| 635 | System.out.println("type1("+type1+") equals type3("+type3+")"); |
| 636 | } else { |
| 637 | System.out.println("type1("+type1+") does not equal type3("+type3+")"); |
| 638 | } |
| 639 | if (type1.hashCode() == type2.hashCode()) { |
| 640 | System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode"); |
| 641 | } else { |
| 642 | System.out.println( |
| 643 | "type1("+type1+") hashCode does not equal type2("+type2+") hashCode"); |
| 644 | } |
| 645 | |
| 646 | if (type1.hashCode() == type3.hashCode()) { |
| 647 | System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode"); |
| 648 | } else { |
| 649 | System.out.println( |
| 650 | "type1("+type1+") hashCode does not equal type3("+type3+") hashCode"); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | public static void checkGenericArrayTypeEqualsAndHashCode() { |
| 655 | Method method1; |
| 656 | Method method2; |
| 657 | Method method3; |
| 658 | try { |
| 659 | method1 = GenericArrayTypeTest.class.getDeclaredMethod("aMethod", Object[].class); |
| 660 | method2 = GenericArrayTypeTest.class.getDeclaredMethod("aMethod", Object[].class); |
| 661 | method3 = GenericArrayTypeTest.class.getDeclaredMethod("aMethodIdentical", Object[].class); |
| 662 | } catch (NoSuchMethodException nsme) { |
| 663 | throw new RuntimeException(nsme); |
| 664 | } |
| 665 | |
| 666 | List<Type> types1 = Arrays.asList(method1.getGenericParameterTypes()); |
| 667 | List<Type> types2 = Arrays.asList(method2.getGenericParameterTypes()); |
| 668 | List<Type> types3 = Arrays.asList(method3.getGenericParameterTypes()); |
| 669 | |
| 670 | Type type1 = types1.get(0); |
| 671 | Type type2 = types2.get(0); |
| 672 | Type type3 = types3.get(0); |
| 673 | |
| 674 | if (type1 instanceof GenericArrayType) { |
| 675 | System.out.println("type1 is a GenericArrayType"); |
| 676 | } |
| 677 | if (type2 instanceof GenericArrayType) { |
| 678 | System.out.println("type2 is a GenericArrayType"); |
| 679 | } |
| 680 | if (type3 instanceof GenericArrayType) { |
| 681 | System.out.println("type3 is a GenericArrayType"); |
| 682 | } |
| 683 | |
| 684 | if (type1.equals(type2)) { |
| 685 | System.out.println("type1("+type1+") equals type2("+type2+")"); |
| 686 | } else { |
| 687 | System.out.println("type1("+type1+") does not equal type2("+type2+")"); |
| 688 | } |
| 689 | |
| 690 | if (type1.equals(type3)) { |
| 691 | System.out.println("type1("+type1+") equals type3("+type3+")"); |
| 692 | } else { |
| 693 | System.out.println("type1("+type1+") does not equal type3("+type3+")"); |
| 694 | } |
| 695 | if (type1.hashCode() == type2.hashCode()) { |
| 696 | System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode"); |
| 697 | } else { |
| 698 | System.out.println( |
| 699 | "type1("+type1+") hashCode does not equal type2("+type2+") hashCode"); |
| 700 | } |
| 701 | |
| 702 | if (type1.hashCode() == type3.hashCode()) { |
| 703 | System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode"); |
| 704 | } else { |
| 705 | System.out.println( |
| 706 | "type1("+type1+") hashCode does not equal type3("+type3+") hashCode"); |
| 707 | } |
| 708 | } |
| 709 | |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 710 | private static void checkGetDeclaredConstructor() { |
| 711 | try { |
| 712 | Method.class.getDeclaredConstructor().setAccessible(true); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 713 | System.out.print("Didn't get an exception from Method.class.getDeclaredConstructor().setAccessible"); |
| 714 | } catch (SecurityException e) { |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 715 | } catch (NoSuchMethodException e) { |
| 716 | } catch (Exception e) { |
| 717 | System.out.print(e); |
| 718 | } |
| 719 | try { |
| 720 | Field.class.getDeclaredConstructor().setAccessible(true); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 721 | System.out.print("Didn't get an exception from Field.class.getDeclaredConstructor().setAccessible"); |
| 722 | } catch (SecurityException e) { |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 723 | } catch (NoSuchMethodException e) { |
| 724 | } catch (Exception e) { |
| 725 | System.out.print(e); |
| 726 | } |
| 727 | try { |
| 728 | Class.class.getDeclaredConstructor().setAccessible(true); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 729 | System.out.print("Didn't get an exception from Class.class.getDeclaredConstructor().setAccessible"); |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 730 | } catch (SecurityException e) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 731 | } catch (NoSuchMethodException e) { |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 732 | } catch (Exception e) { |
| 733 | System.out.print(e); |
| 734 | } |
| 735 | } |
| 736 | |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 737 | static void checkPrivateFieldAccess() { |
| 738 | (new OtherClass()).test(); |
| 739 | } |
| 740 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 741 | public static void main(String[] args) throws Exception { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 742 | Main test = new Main(); |
| 743 | test.run(); |
| 744 | |
Mathieu Chartier | 3ed4c54 | 2014-08-08 15:10:11 -0700 | [diff] [blame] | 745 | checkGetDeclaredConstructor(); |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 746 | checkAccess(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 747 | checkType(); |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 748 | checkClinitForFields(); |
| 749 | checkClinitForMethods(); |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 750 | checkGeneric(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 751 | checkUnique(); |
Piotr Jastrzebski | 68b539e | 2014-08-06 12:02:48 +0100 | [diff] [blame] | 752 | checkParametrizedTypeEqualsAndHashCode(); |
| 753 | checkGenericArrayTypeEqualsAndHashCode(); |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 754 | checkPrivateFieldAccess(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | class SuperTarget { |
| 760 | public SuperTarget() { |
| 761 | System.out.println("SuperTarget constructor ()V"); |
| 762 | superInt = 1010101; |
| 763 | superClassInt = 1010102; |
| 764 | } |
| 765 | |
| 766 | public int myMethod(float floatArg) { |
| 767 | System.out.println("myMethod (F)I " + floatArg); |
| 768 | return 6; |
| 769 | } |
| 770 | |
| 771 | public int superInt; |
| 772 | public static int superClassInt; |
| 773 | } |
| 774 | |
| 775 | class Target extends SuperTarget { |
| 776 | public Target() { |
| 777 | System.out.println("Target constructor ()V"); |
| 778 | } |
| 779 | |
| 780 | public Target(int ii, float ff) { |
| 781 | System.out.println("Target constructor (IF)V : ii=" |
| 782 | + ii + " ff=" + ff); |
| 783 | anInt = ii; |
| 784 | } |
| 785 | |
| 786 | public int myMethod(int intarg) throws NullPointerException, IOException { |
| 787 | System.out.println("myMethod (I)I"); |
| 788 | System.out.println(" arg=" + intarg + " anInt=" + anInt); |
| 789 | return 5; |
| 790 | } |
| 791 | |
| 792 | public int myMethod(String[] strarg, float f, char c) { |
| 793 | System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !"); |
| 794 | return 7; |
| 795 | } |
| 796 | |
| 797 | public static void myNoargMethod() { |
| 798 | System.out.println("myNoargMethod ()V"); |
| 799 | } |
| 800 | |
| 801 | public void throwingMethod() { |
| 802 | System.out.println("throwingMethod"); |
| 803 | throw new NullPointerException("gratuitous throw!"); |
| 804 | } |
| 805 | |
| 806 | public void misc() { |
| 807 | System.out.println("misc"); |
| 808 | } |
| 809 | |
| 810 | public int anInt; |
| 811 | public String string1 = "hey"; |
| 812 | public String string2 = "yo"; |
| 813 | public String string3 = "there"; |
| 814 | private String string4 = "naughty"; |
| 815 | public static final String constantString = "a constant string"; |
| 816 | private int aPrivateInt; |
| 817 | |
| 818 | public final int cantTouchThis = 77; |
| 819 | |
| 820 | public long pubLong = 0x1122334455667788L; |
| 821 | |
| 822 | public static double staticDouble = 3.3; |
| 823 | } |
| 824 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 825 | class FieldNoisyInit { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 826 | static { |
| 827 | System.out.println("FieldNoisyInit is initializing"); |
| 828 | //Throwable th = new Throwable(); |
| 829 | //th.printStackTrace(); |
| 830 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 833 | class FieldNoisyInitUser { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 834 | static { |
| 835 | System.out.println("FieldNoisyInitUser is initializing"); |
| 836 | } |
| 837 | public static int staticField; |
| 838 | public static FieldNoisyInit noisy; |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | class MethodNoisyInit { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 842 | static { |
| 843 | System.out.println("MethodNoisyInit is initializing"); |
| 844 | //Throwable th = new Throwable(); |
| 845 | //th.printStackTrace(); |
| 846 | } |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | class MethodNoisyInitUser { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 850 | static { |
| 851 | System.out.println("MethodNoisyInitUser is initializing"); |
| 852 | } |
| 853 | public static void staticMethod() {} |
| 854 | public void createMethodNoisyInit(MethodNoisyInit ni) {} |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 855 | } |
Jeff Hao | 63f5b9e | 2014-04-23 14:51:46 -0700 | [diff] [blame] | 856 | |
| 857 | class Thrower { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 858 | public Thrower() throws UnsupportedOperationException { |
| 859 | throw new UnsupportedOperationException(); |
| 860 | } |
Jeff Hao | 63f5b9e | 2014-04-23 14:51:46 -0700 | [diff] [blame] | 861 | } |
Piotr Jastrzebski | 68b539e | 2014-08-06 12:02:48 +0100 | [diff] [blame] | 862 | |
| 863 | class ParametrizedTypeTest { |
| 864 | public void aMethod(Set<String> names) {} |
| 865 | public void aMethodIdentical(Set<String> names) {} |
| 866 | } |
| 867 | |
| 868 | class GenericArrayTypeTest<T> { |
| 869 | public void aMethod(T[] names) {} |
| 870 | public void aMethodIdentical(T[] names) {} |
| 871 | } |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 872 | |
| 873 | class OtherClass { |
| 874 | private static final long LONG = 1234; |
| 875 | public void test() { |
| 876 | try { |
| 877 | Field field = getClass().getDeclaredField("LONG"); |
| 878 | if (1234 != field.getLong(null)) { |
| 879 | System.out.println("ERROR: values don't match"); |
| 880 | } |
| 881 | } catch (Exception e) { |
| 882 | System.out.println(e); |
| 883 | } |
| 884 | } |
Mathieu Chartier | 4e06778 | 2015-05-13 13:13:24 -0700 | [diff] [blame] | 885 | } |