| 1 22 package org.jboss.test.aop.reflection; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 31 34 35 public class ReflectionPOJO 36 { 37 38 final static Package LANG_PACKAGE = Package.getPackage("java.lang"); 39 40 public ReflectionPOJO() 41 { 42 43 } 44 45 public ReflectionPOJO(int x) 46 { 47 try 48 { 49 System.out.println("*** reflection (from constructor): Sanity checks"); 51 52 SimplePerVmInterceptor.reset(); 53 ReflectionAopPOJO pojo = new ReflectionAopPOJO(8); 54 if (SimplePerVmInterceptor.constructorIntercepted != 1) 55 { 56 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor"); 57 } 58 if (pojo.j != 8) 59 { 60 throw new RuntimeException ("POJO.j was not 8"); 61 } 62 63 SimplePerVmInterceptor.reset(); 64 pojo.method(10); 65 if (SimplePerVmInterceptor.methodIntercepted != 1) 66 { 67 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method call"); 68 } 69 70 SimplePerVmInterceptor.reset(); 71 pojo.j = 5; 72 if (SimplePerVmInterceptor.fieldWriteIntercepted != 1) 73 { 74 throw new RuntimeException ("SimplePerVmInterceptor did not intercept field write"); 75 } 76 77 SimplePerVmInterceptor.reset(); 78 int i = pojo.j; 79 System.out.println("i=" + i); 80 if (SimplePerVmInterceptor.fieldReadIntercepted != 1) 81 { 82 throw new RuntimeException ("SimplePerVmInterceptor did not intercept field read"); 83 } 84 85 CallerInterceptor.reset(); 86 pojo = new ReflectionAopPOJO(false); 87 if (CallerInterceptor.intercepted != 1) 88 { 89 throw new RuntimeException ("CallerInterceptor did not intercept construction"); 90 } 91 92 CallerInterceptor.reset(); 93 pojo.method(false); 94 if (CallerInterceptor.intercepted != 1) 95 { 96 throw new RuntimeException ("CallerInterceptor did not intercept method(boolean) call"); 97 } 98 99 CallerInterceptor.reset(); 100 SimplePerVmInterceptor.reset(); 101 pojo = new ReflectionAopPOJO(100L); 102 if (CallerInterceptor.intercepted != 1) 103 { 104 throw new RuntimeException ("CallerInterceptor did not intercept constructor(long) call"); 105 } 106 if (SimplePerVmInterceptor.constructorIntercepted != 1) 107 { 108 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor(long) call"); 109 } 110 111 CallerInterceptor.reset(); 112 SimplePerVmInterceptor.reset(); 113 pojo.otherMethod(200L); 114 if (CallerInterceptor.intercepted != 1) 115 { 116 throw new RuntimeException ("CallerInterceptor did not intercept method(long) call"); 117 } 118 if (SimplePerVmInterceptor.methodIntercepted != 1) 119 { 120 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method(long) call"); 121 } 122 123 System.out.println("*** reflection (from constructor): Class.newInstance"); 125 Class clazz = ReflectionAopPOJO.class; 126 SimplePerVmInterceptor.reset(); 127 System.out.println("reflection call"); 128 pojo = (ReflectionAopPOJO)clazz.newInstance(); 129 System.out.println("reflection call - end"); 130 if (ReflectionAspectTester.constructor == null) throw new RuntimeException ("Not intercepted"); 131 if (SimplePerVmInterceptor.constructorIntercepted != 1) 132 { 133 throw new RuntimeException ("SimplePerVmInterceptor not invoked for reflected default constructor"); 134 } 135 136 System.out.println("*** reflection (from constructor): Constructor.newInstance"); 138 Constructor constructor = clazz.getConstructor(new Class []{Integer.TYPE}); 139 SimplePerVmInterceptor.reset(); 140 System.out.println("reflection call"); 141 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{new Integer (4)}); 142 System.out.println("reflection call - end"); 143 if (SimplePerVmInterceptor.constructorIntercepted != 1) 144 { 145 throw new RuntimeException ("SimplePerVmInterceptor not invoked for reflected constructor(int)"); 146 } 147 if (pojo.j != 4) 148 { 149 throw new RuntimeException ("POJO.j was not 8 following reflection"); 150 } 151 152 System.out.println("*** reflection (from constructor): Method.invoke"); 155 Method method = clazz.getMethod("method", new Class []{Integer.TYPE}); 156 SimplePerVmInterceptor.reset(); 157 System.out.println("reflection call"); 158 method.invoke(pojo, new Object []{new Integer (55)}); 159 System.out.println("reflection call - end"); 160 if (SimplePerVmInterceptor.methodIntercepted != 1) 161 { 162 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected method call"); 163 } 164 165 System.out.println("*** reflection (from constructor): Field.setInt"); 167 Field field = clazz.getField("j"); 168 SimplePerVmInterceptor.reset(); 169 System.out.println("reflection call"); 170 field.setInt(pojo, 10); 171 System.out.println("reflection call - end"); 172 if (ReflectionAspectTester.field == null 173 || ((Integer ) ReflectionAspectTester.args[0]).intValue() != 10) 174 { 175 throw new RuntimeException ("Not intercepted"); 176 } 177 if (SimplePerVmInterceptor.fieldWriteIntercepted != 1) 178 { 179 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected field write"); 180 } 181 182 183 System.out.println("*** reflection (from constructor): Field.getInt"); 185 SimplePerVmInterceptor.reset(); 186 System.out.println("reflection call"); 187 int j = field.getInt(pojo); 188 System.out.println("reflection call - end"); 189 if (ReflectionAspectTester.field == null 190 || j != 10) 191 { 192 throw new RuntimeException ("Not intercepted"); 193 } 194 if (SimplePerVmInterceptor.fieldReadIntercepted != 1) 195 { 196 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected field read"); 197 } 198 199 System.out.println("*** reflection (from constructor): Checking not advised constructors, fields and methods"); 201 SimplePerVmInterceptor.reset(); 202 constructor = clazz.getConstructor(new Class []{String .class}); 203 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{"x"}); 204 method = clazz.getMethod("method", new Class [0]); 205 method.invoke(pojo, new Object [0]); 206 field = clazz.getField("k"); 207 field.setInt(pojo, 5); 208 field.getInt(pojo); 209 if (SimplePerVmInterceptor.hasIntercepted()) 210 { 211 throw new RuntimeException ("SimplePerVmInterceptor intercepted something that should have been left alone"); 212 } 213 214 System.out.println("*** reflection (from constructor): Checking not advised constructor"); 215 Class notAdvisedClazz = NotAdvisedPOJO.class; 216 constructor = notAdvisedClazz.getConstructor(new Class [0]); 217 NotAdvisedPOJO naPojo = (NotAdvisedPOJO)constructor.newInstance(new Object [0]); 218 219 System.out.println("*** reflection (from constructor): Checking not advised method"); 220 method = notAdvisedClazz.getMethod("method", new Class [0]); 221 method.invoke(naPojo, new Object [0]); 222 223 System.out.println("*** reflection (from constructor): Checking not advised field set"); 224 field = notAdvisedClazz.getField("i"); 225 field.setInt(naPojo, 1); 226 227 System.out.println("*** reflection (from constructor): Checking not advised field get"); 228 field.getInt(naPojo); 229 230 System.out.println("*** reflection (from constructor): Checking constructor caller"); 231 CallerInterceptor.reset(); 232 constructor = clazz.getConstructor(new Class []{Boolean.TYPE}); 233 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{Boolean.TRUE}); 234 if (CallerInterceptor.intercepted != 1) 235 { 236 throw new RuntimeException ("CallerInterceptor did not intercept reflected construction"); 237 } 238 239 System.out.println("*** reflection (from constructor): Checking method caller"); 240 CallerInterceptor.reset(); 241 method = clazz.getMethod("method", new Class []{Boolean.TYPE}); 242 method.invoke(pojo, new Object []{Boolean.FALSE}); 243 if (CallerInterceptor.intercepted != 1) 244 { 245 throw new RuntimeException ("CallerInterceptor did not intercept reflected method(boolean) call"); 246 } 247 248 System.out.println("*** reflection (from constructor): Checking constructor caller and execution"); 250 CallerInterceptor.reset(); 251 SimplePerVmInterceptor.reset(); 252 constructor = clazz.getConstructor(new Class []{Long.TYPE}); 253 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{new Long (100L)}); 254 if (CallerInterceptor.intercepted != 1) 255 { 256 throw new RuntimeException ("CallerInterceptor did not intercept constructor(long) call"); 257 } 258 if (SimplePerVmInterceptor.constructorIntercepted != 1) 259 { 260 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor(long) call"); 261 } 262 263 System.out.println("*** reflection (from constructor): Checking method caller and execution"); 264 CallerInterceptor.reset(); 265 SimplePerVmInterceptor.reset(); 266 method = clazz.getMethod("otherMethod", new Class []{Long.TYPE}); 267 method.invoke(pojo, new Object []{new Long (100L)}); 268 if (CallerInterceptor.intercepted != 1) 269 { 270 throw new RuntimeException ("CallerInterceptor did not intercept method(long) call"); 271 } 272 if (SimplePerVmInterceptor.methodIntercepted != 1) 273 { 274 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method(long) call"); 275 } 276 } 277 catch (Exception e) 278 { 279 throw new RuntimeException (e); 280 } 281 } 282 283 284 public void testCreationAndFieldAccess() 285 { 286 try 287 { 288 System.out.println("*** reflection (from method): Sanity checks"); 290 291 SimplePerVmInterceptor.reset(); 292 ReflectionAopPOJO pojo = new ReflectionAopPOJO(8); 293 if (SimplePerVmInterceptor.constructorIntercepted != 1) 294 { 295 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor"); 296 } 297 if (pojo.j != 8) 298 { 299 throw new RuntimeException ("POJO.j was not 8"); 300 } 301 302 SimplePerVmInterceptor.reset(); 303 pojo.method(10); 304 if (SimplePerVmInterceptor.methodIntercepted != 1) 305 { 306 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method call"); 307 } 308 309 SimplePerVmInterceptor.reset(); 310 pojo.j = 5; 311 if (SimplePerVmInterceptor.fieldWriteIntercepted != 1) 312 { 313 throw new RuntimeException ("SimplePerVmInterceptor did not intercept field write"); 314 } 315 316 SimplePerVmInterceptor.reset(); 317 int i = pojo.j; 318 System.out.println("i=" + i); 319 if (SimplePerVmInterceptor.fieldReadIntercepted != 1) 320 { 321 throw new RuntimeException ("SimplePerVmInterceptor did not intercept field read"); 322 } 323 324 CallerInterceptor.reset(); 325 pojo = new ReflectionAopPOJO(false); 326 if (CallerInterceptor.intercepted != 1) 327 { 328 throw new RuntimeException ("CallerInterceptor did not intercept construction"); 329 } 330 331 CallerInterceptor.reset(); 332 pojo.method(false); 333 if (CallerInterceptor.intercepted != 1) 334 { 335 throw new RuntimeException ("CallerInterceptor did not intercept method(boolean) call"); 336 } 337 338 CallerInterceptor.reset(); 339 SimplePerVmInterceptor.reset(); 340 pojo = new ReflectionAopPOJO(100L); 341 if (CallerInterceptor.intercepted != 1) 342 { 343 throw new RuntimeException ("CallerInterceptor did not intercept constructor(long) call"); 344 } 345 if (SimplePerVmInterceptor.constructorIntercepted != 1) 346 { 347 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor(long) call"); 348 } 349 350 CallerInterceptor.reset(); 351 SimplePerVmInterceptor.reset(); 352 pojo.otherMethod(200L); 353 if (CallerInterceptor.intercepted != 1) 354 { 355 throw new RuntimeException ("CallerInterceptor did not intercept method(long) call"); 356 } 357 if (SimplePerVmInterceptor.methodIntercepted != 1) 358 { 359 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method(long) call"); 360 } 361 362 System.out.println("*** reflection (from method): Class.newInstance"); 364 Class clazz = ReflectionAopPOJO.class; 365 SimplePerVmInterceptor.reset(); 366 System.out.println("reflection call"); 367 pojo = (ReflectionAopPOJO)clazz.newInstance(); 368 System.out.println("reflection call - end"); 369 if (ReflectionAspectTester.constructor == null) throw new RuntimeException ("Not intercepted"); 370 if (SimplePerVmInterceptor.constructorIntercepted != 1) 371 { 372 throw new RuntimeException ("SimplePerVmInterceptor not invoked for reflected default constructor"); 373 } 374 375 System.out.println("*** reflection (from method): Constructor.newInstance"); 377 Constructor constructor = clazz.getConstructor(new Class []{Integer.TYPE}); 378 SimplePerVmInterceptor.reset(); 379 System.out.println("reflection call"); 380 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{new Integer (4)}); 381 System.out.println("reflection call - end"); 382 if (SimplePerVmInterceptor.constructorIntercepted != 1) 383 { 384 throw new RuntimeException ("SimplePerVmInterceptor not invoked for reflected constructor(int)"); 385 } 386 if (pojo.j != 4) 387 { 388 throw new RuntimeException ("POJO.j was not 8 following reflection"); 389 } 390 391 System.out.println("*** reflection (from method): Method.invoke"); 394 Method method = clazz.getMethod("method", new Class []{Integer.TYPE}); 395 SimplePerVmInterceptor.reset(); 396 System.out.println("reflection call"); 397 method.invoke(pojo, new Object []{new Integer (55)}); 398 System.out.println("reflection call - end"); 399 if (SimplePerVmInterceptor.methodIntercepted != 1) 400 { 401 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected method call"); 402 } 403 404 System.out.println("*** reflection (from method): Field.setInt"); 406 Field field = clazz.getField("j"); 407 SimplePerVmInterceptor.reset(); 408 System.out.println("reflection call"); 409 field.setInt(pojo, 10); 410 System.out.println("reflection call - end"); 411 if (ReflectionAspectTester.field == null 412 || ((Integer ) ReflectionAspectTester.args[0]).intValue() != 10) 413 { 414 throw new RuntimeException ("Not intercepted"); 415 } 416 if (SimplePerVmInterceptor.fieldWriteIntercepted != 1) 417 { 418 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected field write"); 419 } 420 421 422 System.out.println("*** reflection (from method): Field.getInt"); 424 SimplePerVmInterceptor.reset(); 425 System.out.println("reflection call"); 426 int j = field.getInt(pojo); 427 System.out.println("reflection call - end"); 428 if (ReflectionAspectTester.field == null 429 || j != 10) 430 { 431 throw new RuntimeException ("Not intercepted"); 432 } 433 if (SimplePerVmInterceptor.fieldReadIntercepted != 1) 434 { 435 throw new RuntimeException ("SimplePerVmInterceptor did not intercept reflected field read"); 436 } 437 438 SimplePerVmInterceptor.reset(); 440 constructor = clazz.getConstructor(new Class []{String .class}); 441 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{"x"}); 442 method = clazz.getMethod("method", new Class [0]); 443 method.invoke(pojo, new Object [0]); 444 field = clazz.getField("k"); 445 field.setInt(pojo, 5); 446 field.getInt(pojo); 447 448 if (SimplePerVmInterceptor.hasIntercepted()) 449 { 450 throw new RuntimeException ("SimplePerVmInterceptor intercepted something that should have been left alone"); 451 } 452 453 System.out.println("*** reflection (from method): Checking not advised constructor"); 454 Class notAdvisedClazz = NotAdvisedPOJO.class; 455 constructor = notAdvisedClazz.getConstructor(new Class [0]); 456 NotAdvisedPOJO naPojo = (NotAdvisedPOJO)constructor.newInstance(new Object [0]); 457 458 System.out.println("*** reflection (from method): Checking not advised method"); 459 method = notAdvisedClazz.getMethod("method", new Class [0]); 460 method.invoke(naPojo, new Object [0]); 461 462 System.out.println("*** reflection (from method): Checking not advised field set"); 463 field = notAdvisedClazz.getField("i"); 464 field.setInt(naPojo, 1); 465 466 System.out.println("*** reflection (from method): Checking not advised field get"); 467 field.getInt(naPojo); 468 469 System.out.println("*** reflection (from method): Checking constructor caller"); 470 CallerInterceptor.reset(); 471 constructor = clazz.getConstructor(new Class []{Boolean.TYPE}); 472 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{Boolean.TRUE}); 473 if (CallerInterceptor.intercepted != 1) 474 { 475 throw new RuntimeException ("CallerInterceptor did not intercept reflected construction"); 476 } 477 478 System.out.println("*** reflection (from method): Checking method caller"); 479 CallerInterceptor.reset(); 480 method = clazz.getMethod("method", new Class []{Boolean.TYPE}); 481 method.invoke(pojo, new Object []{Boolean.FALSE}); 482 if (CallerInterceptor.intercepted != 1) 483 { 484 throw new RuntimeException ("CallerInterceptor did not intercept reflected method(boolean) call"); 485 } 486 487 System.out.println("*** reflection (from constructor): Checking constructor caller and execution"); 489 CallerInterceptor.reset(); 490 SimplePerVmInterceptor.reset(); 491 constructor = clazz.getConstructor(new Class []{Long.TYPE}); 492 pojo = (ReflectionAopPOJO)constructor.newInstance(new Object []{new Long (100L)}); 493 if (CallerInterceptor.intercepted != 1) 494 { 495 throw new RuntimeException ("CallerInterceptor did not intercept constructor(long) call"); 496 } 497 if (SimplePerVmInterceptor.constructorIntercepted != 1) 498 { 499 throw new RuntimeException ("SimplePerVmInterceptor did not intercept constructor(long) call"); 500 } 501 502 System.out.println("*** reflection (from constructor): Checking method caller and execution"); 503 CallerInterceptor.reset(); 504 SimplePerVmInterceptor.reset(); 505 method = clazz.getMethod("otherMethod", new Class []{Long.TYPE}); 506 method.invoke(pojo, new Object []{new Long (100L)}); 507 if (CallerInterceptor.intercepted != 1) 508 { 509 throw new RuntimeException ("CallerInterceptor did not intercept method(long) call"); 510 } 511 if (SimplePerVmInterceptor.methodIntercepted != 1) 512 { 513 throw new RuntimeException ("SimplePerVmInterceptor did not intercept method(long) call"); 514 } 515 516 } 517 catch (Exception e) 518 { 519 throw new RuntimeException (e); 520 } 521 } 522 523 public void testCleanGetMethods() 524 { 525 System.out.println("*** reflection (from method): Class.getMethods()"); 526 Class pojoClass = ReflectionAopPOJO.class; 527 Class pojoRoot = ReflectionAopRootPOJO.class; 528 try 529 { 530 Method [] check = new Method []{ 532 pojoClass.getMethod("method", new Class [0]), 533 pojoClass.getMethod("method", new Class []{Integer.TYPE}), 534 pojoClass.getMethod("method", new Class []{Boolean.TYPE}), 535 pojoClass.getMethod("method", new Class []{Integer.TYPE, Long.TYPE}), 536 pojoRoot.getMethod("method", new Class []{Integer.TYPE, Long.TYPE, Short.TYPE}), 537 pojoClass.getMethod("otherMethod", new Class []{Long.TYPE}), 538 pojoClass.getMethod("helloWorld", new Class []{String .class})}; 539 540 Method [] methods = pojoClass.getMethods(); 541 542 ArrayList methodList = new ArrayList (); 543 544 for (int i = 0; i < methods.length; i++) 545 { 546 if (!methods[i].getDeclaringClass().getPackage().equals(LANG_PACKAGE)) 547 { 548 methodList.add(methods[i]); 549 } 550 } 551 552 553 for (int i = 0; i < check.length; i++) 554 { 555 if (!methodList.remove(check[i])) 556 { 557 throw new RuntimeException ("\"Cleaned\" Class.getMethods() did not return all expected methods: " + check[i]); 558 } 559 } 560 561 if (methodList.size() > 0) 562 { 563 StringBuffer sb = new StringBuffer ("The following methods should not " 564 + "have ben returned by \"Cleaned\" Class.getMethods():\n"); 565 566 for (Iterator it = methodList.iterator(); it.hasNext();) 567 { 568 sb.append(it.next()); 569 } 570 571 throw new RuntimeException (sb.toString()); 572 } 573 574 } 575 catch (NoSuchMethodException e) 576 { 577 throw new RuntimeException (e); 578 } 579 } 580 581 public void testCleanGetDeclaredMethods() 582 { 583 584 System.out.println("*** reflection (from method): Class.getDeclaredMethods()"); 585 Class clazz = ReflectionAopPOJO.class; 586 try 587 { 588 Method [] check = new Method []{ 590 clazz.getDeclaredMethod("method", new Class [0]), 591 clazz.getDeclaredMethod("method", new Class []{Integer.TYPE}), 592 clazz.getDeclaredMethod("method", new Class []{Boolean.TYPE}), 593 clazz.getMethod("method", new Class []{Integer.TYPE, Long.TYPE}), 594 clazz.getMethod("otherMethod", new Class []{Long.TYPE}), 595 clazz.getDeclaredMethod("privateMethod", new Class [0]), 596 clazz.getMethod("helloWorld", new Class []{String .class})}; 597 598 Method [] methods = clazz.getDeclaredMethods(); 599 600 ArrayList methodList = new ArrayList (); 601 602 for (int i = 0; i < methods.length; i++) 603 { 604 methodList.add(methods[i]); 605 } 606 607 608 for (int i = 0; i < check.length; i++) 609 { 610 if (!methodList.remove(check[i])) 611 { 612 throw new RuntimeException ("\"Cleaned\" Class.getDeclaredMethods() did not return all expected methods: " + check[i]); 613 } 614 } 615 616 if (methodList.size() > 0) 617 { 618 StringBuffer sb = new StringBuffer ("The following methods should not " 619 + "have ben returned by \"Cleaned\" Class.getDeclaredMethods():\n"); 620 621 for (Iterator it = methodList.iterator(); it.hasNext();) 622 { 623 sb.append(it.next()); 624 } 625 626 throw new RuntimeException (sb.toString()); 627 } 628 } 629 catch (NoSuchMethodException e) 630 { 631 throw new RuntimeException (e); 632 } 633 } 634 635 public void testCleanGetDeclaredFields() 636 { 637 638 System.out.println("*** reflection (from method): Class.getDeclaredFields()"); 639 Class
|