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 clazz = ReflectionAopPOJO.class; 640 try 641 { 642 Field [] check = new Field []{ 643 clazz.getDeclaredField("i"), 644 clazz.getDeclaredField("j"), 645 clazz.getDeclaredField("k")}; 646 647 Field [] fields = clazz.getDeclaredFields(); 648 649 ArrayList fieldList = new ArrayList (); 650 651 for (int i = 0; i < fields.length; i++) 652 { 653 fieldList.add(fields[i]); 654 } 655 656 657 for (int i = 0; i < check.length; i++) 658 { 659 if (!fieldList.remove(check[i])) 660 { 661 throw new RuntimeException ("\"Cleaned\" Class.getDeclaredFields() did not return all expected fields: " + check[i]); 662 } 663 } 664 665 if (fieldList.size() > 0) 666 { 667 StringBuffer sb = new StringBuffer ("The following methods should not " 668 + "have ben returned by \"Cleaned\" Class.getDeclaredFields():\n"); 669 670 for (Iterator it = fieldList.iterator(); it.hasNext();) 671 { 672 sb.append(it.next()); 673 } 674 675 throw new RuntimeException (sb.toString()); 676 } 677 } 678 catch (NoSuchFieldException e) 679 { 680 throw new RuntimeException (e); 681 } 682 } 683 684 685 public void testCleanGetInterfaces() 686 { 687 688 System.out.println("*** reflection (from method): Class.getInterfaces()"); 689 ReflectionAopPOJO pojo = new ReflectionAopPOJO(); 690 System.out.println("pojo=" + pojo); 691 Class clazz = ReflectionAopPOJO.class; 692 Class [] interfaces = clazz.getInterfaces(); 693 694 if (interfaces.length != 1) 695 { 696 throw new RuntimeException ("Expected 1 interface, got " + interfaces.length); 697 } 698 699 if (!interfaces[0].getName().equals("org.jboss.test.aop.reflection.Introduction")) 700 { 701 throw new RuntimeException ("Did not get expected interface org.jboss.test.aop.reflection.Introduction"); 702 } 703 } 704 705 public void testCleanGetDeclaredMethod() 706 { 707 708 System.out.println("*** reflection (from method): Class.getDeclaredMethod()"); 709 Class pojoClass = ReflectionAopPOJO.class; 710 Class pojoRoot = ReflectionAopRootPOJO.class; 711 712 try 713 { 714 Method m = pojoClass.getDeclaredMethod("method", new Class []{Integer.TYPE, Long.TYPE, Short.TYPE}); 715 throw new RuntimeException ("Should not be here: " + m); 716 } 717 catch (NoSuchMethodException e) 718 { 719 } 721 722 try 723 { 724 Method m = pojoRoot.getDeclaredMethod("method", new Class []{Integer.TYPE, Long.TYPE, Short.TYPE}); 725 726 if (!m.getName().equals("method") 727 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopRootPOJO") 728 || !m.getParameterTypes()[0].equals(Integer.TYPE) 729 || !m.getParameterTypes()[1].equals(Long.TYPE)) 730 { 731 throw new RuntimeException ("Wrong method got"); 732 } 733 } 734 catch (NoSuchMethodException e) 735 { 736 throw new RuntimeException ("Expected method not there"); 737 } 738 739 try 740 { 741 Method m = pojoClass.getDeclaredMethod("method", new Class []{Integer.TYPE, Long.TYPE}); 742 743 if (!m.getName().equals("method") 744 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 745 || !m.getParameterTypes()[0].equals(Integer.TYPE) 746 || !m.getParameterTypes()[1].equals(Long.TYPE)) 747 { 748 throw new RuntimeException ("Wrong method got"); 749 } 750 } 751 catch (NoSuchMethodException e) 752 { 753 throw new RuntimeException ("Expected method not there"); 754 } 755 756 try 757 { 758 Method m = pojoClass.getDeclaredMethod("method", new Class []{Integer.TYPE}); 759 760 if (!m.getName().equals("method") 761 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 762 || !m.getParameterTypes()[0].equals(Integer.TYPE)) 763 { 764 throw new RuntimeException ("Wrong method got"); 765 } 766 } 767 catch (NoSuchMethodException e) 768 { 769 throw new RuntimeException ("Expected method not there"); 770 } 771 772 try 773 { 774 Method m = pojoClass.getDeclaredMethod("method", new Class [0]); 775 776 if (!m.getName().equals("method") 777 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")) 778 { 779 throw new RuntimeException ("Wrong method got"); 780 } 781 } 782 catch (NoSuchMethodException e) 783 { 784 throw new RuntimeException ("Expected method not there"); 785 } 786 787 try 788 { 789 Method m = pojoClass.getDeclaredMethod("privateMethod", new Class [0]); 790 791 if (!m.getName().equals("privateMethod") 792 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")) 793 { 794 throw new RuntimeException ("Wrong method got"); 795 } 796 } 797 catch (NoSuchMethodException e) 798 { 799 throw new RuntimeException ("Expected method not there"); 800 } 801 802 try 803 { 804 Method m = pojoClass.getMethod("notThere", new Class [0]); 805 throw new RuntimeException ("Method should not be there " + m); 806 } 807 catch (NoSuchMethodException e) 808 { 809 } 811 812 try 813 { 814 Method m = pojoClass.getMethod("_getAdvisor", new Class [0]); 815 throw new RuntimeException ("Method should have been cleaned " + m); 816 } 817 catch (NoSuchMethodException e) 818 { 819 } 821 822 try 823 { 824 Method m = pojoRoot.getMethod("_getAdvisor", new Class [0]); 825 throw new RuntimeException ("Method should have been cleaned " + m); 826 } 827 catch (NoSuchMethodException e) 828 { 829 } 831 832 } 833 834 public void testCleanGetMethod() 835 { 836 837 System.out.println("*** reflection (from method): Class.getMethod()"); 838 Class clazz = ReflectionAopPOJO.class; 839 840 try 841 { 842 Method m = clazz.getMethod("method", new Class []{Integer.TYPE, Long.TYPE, Short.TYPE}); 843 844 if (!m.getName().equals("method") 845 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopRootPOJO") 846 || !m.getParameterTypes()[0].equals(Integer.TYPE) 847 || !m.getParameterTypes()[1].equals(Long.TYPE)) 848 { 849 throw new RuntimeException ("Wrong method got"); 850 } 851 } 852 catch (NoSuchMethodException e) 853 { 854 throw new RuntimeException ("Expected method not there"); 855 } 856 857 try 858 { 859 Method m = clazz.getMethod("method", new Class []{Integer.TYPE, Long.TYPE}); 860 861 if (!m.getName().equals("method") 862 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 863 || !m.getParameterTypes()[0].equals(Integer.TYPE) 864 || !m.getParameterTypes()[1].equals(Long.TYPE)) 865 { 866 throw new RuntimeException ("Wrong method got"); 867 } 868 } 869 catch (NoSuchMethodException e) 870 { 871 throw new RuntimeException ("Expected method not there"); 872 } 873 874 try 875 { 876 Method m = clazz.getMethod("method", new Class []{Integer.TYPE}); 877 878 if (!m.getName().equals("method") 879 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 880 || !m.getParameterTypes()[0].equals(Integer.TYPE)) 881 { 882 throw new RuntimeException ("Wrong method got"); 883 } 884 } 885 catch (NoSuchMethodException e) 886 { 887 throw new RuntimeException ("Expected method not there"); 888 } 889 890 try 891 { 892 Method m = clazz.getMethod("method", new Class [0]); 893 894 if (!m.getName().equals("method") 895 || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")) 896 { 897 throw new RuntimeException ("Wrong method got"); 898 } 899 } 900 catch (NoSuchMethodException e) 901 { 902 throw new RuntimeException ("Expected method not there"); 903 } 904 905 try 906 { 907 Method m = clazz.getMethod("notThere", new Class [0]); 908 throw new RuntimeException ("Method should not be there " + m); 909 } 910 catch (NoSuchMethodException e) 911 { 912 } 914 915 try 916 { 917 Method m = clazz.getMethod("_getAdvisor", new Class [0]); 918 throw new RuntimeException ("Method should have been cleaned " + m); 919 } 920 catch (NoSuchMethodException e) 921 { 922 } 924 925 } 926 927 public void testCleanGetDeclaredField() 928 { 929 930 System.out.println("*** reflection (from method): Class.getDeclaredField()"); 931 Class clazz = ReflectionAopPOJO.class; 932 try 933 { 934 Field f = clazz.getDeclaredField("i"); 935 936 if (!f.getName().equals("i") 937 || !f.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 938 || !f.getType().equals(Integer.TYPE)) 939 { 940 throw new RuntimeException ("Wrong field got"); 941 } 942 } 943 catch (NoSuchFieldException e) 944 { 945 throw new RuntimeException ("Expected method not there"); 946 } 947 948 try 949 { 950 Field f = clazz.getDeclaredField("j"); 951 952 if (!f.getName().equals("j") 953 || !f.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO") 954 || !f.getType().equals(Integer.TYPE)) 955 { 956 throw new RuntimeException ("Wrong field got"); 957 } 958 } 959 catch (NoSuchFieldException e) 960 { 961 throw new RuntimeException ("Expected method not there"); 962 } 963 } 964 965 public void testCleanGetClasses() 966 { 967 System.out.println("*** reflection (from method): Class.getClasses()"); 969 HashSet classSet = new HashSet (); 970 classSet.add("org.jboss.test.aop.reflection.ReflectionAopPOJO$AopPOJOInner"); 971 classSet.add("org.jboss.test.aop.reflection.ReflectionAopRootPOJO$AopRootPOJOInner"); 972 Class clazz = ReflectionAopPOJO.class; 973 974 Class [] classes = clazz.getClasses(); 975 976 for (int i = 0; i < classes.length; i++) 977 { 978 String name = classes[i].getName(); 979 if (!classSet.contains(name)) 980 { 981 throw new RuntimeException (name + " was in the list of classes and should have been removed"); 982 } 983 } 984 985 if (classes.length != classSet.size()) 986 { 987 throw new RuntimeException ("Not all the declared classes were returned"); 988 } 989 } 990 991 public void testCleanGetDeclaredClasses() 992 { 993 System.out.println("*** reflection (from method): Class.getDeclaredClasses()"); 994 HashSet declaredClasses = new HashSet (); 995 declaredClasses.add("org.jboss.test.aop.reflection.ReflectionAopPOJO$AopPOJOInner"); 996 997 Class clazz = ReflectionAopPOJO.class; 998 Class [] classes = clazz.getDeclaredClasses(); 999 1000 for (int i = 0; i < classes.length; i++) 1001 { 1002 String name = classes[i].getName(); 1003 if (!declaredClasses.contains(name)) 1004 { 1005 throw new RuntimeException (name + " was in the list of declared classes and should have been removed"); 1006 } 1007 } 1008 1009 if (classes.length != declaredClasses.size()) 1010 { 1011 throw new RuntimeException ("Not all the declared classes were returned"); 1012 } 1013 } 1014} 1015 | Popular Tags |