| 1 22 package org.jboss.aop.reflection; 23 24 import gnu.trove.TLongObjectHashMap; 25 import org.jboss.aop.Advised; 26 import org.jboss.aop.AspectManager; 27 import org.jboss.aop.ClassAdvisor; 28 import org.jboss.aop.ConByConInfo; 29 import org.jboss.aop.ConByMethodInfo; 30 import org.jboss.aop.ConstructorInfo; 31 import org.jboss.aop.FieldInfo; 32 import org.jboss.aop.MethodByConInfo; 33 import org.jboss.aop.MethodByMethodInfo; 34 import org.jboss.aop.advice.Interceptor; 35 import org.jboss.aop.instrument.Untransformable; 36 import org.jboss.aop.joinpoint.Invocation; 37 import org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation; 38 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation; 39 import org.jboss.aop.util.MethodHashing; 40 41 import java.lang.reflect.Constructor ; 42 import java.lang.reflect.Field ; 43 import java.lang.reflect.Method ; 44 import java.lang.reflect.Modifier ; 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.regex.Matcher ; 49 import java.util.regex.Pattern ; 50 51 67 public class ReflectionAspect 68 { 69 71 73 75 private static Pattern fieldGetPattern = 76 Pattern.compile("get(|Boolean|Byte|Char|Double|Float|Int|Long|Short)?"); 77 78 private static Pattern fieldSetPattern = 79 Pattern.compile("set(|Boolean|Byte|Char|Double|Float|Int|Long|Short)?"); 80 81 private static Pattern accessMethodPattern = Pattern.compile("access[$](\\d)+"); 82 83 85 public ReflectionAspect() 86 { 87 } 88 89 91 100 public Object interceptNewInstance(MethodCalledByConstructorInvocation invocation) throws Throwable  101 { 102 103 Method reflectionMethod = invocation.getCalledMethod(); 104 Object targetObject = invocation.getTargetObject(); 105 Object [] args = invocation.getArguments(); 106 107 return interceptNewInstance(invocation, reflectionMethod, targetObject, args); 108 } 109 110 119 public Object interceptNewInstance(MethodCalledByMethodInvocation invocation) throws Throwable  120 { 121 122 Method reflectionMethod = invocation.getCalledMethod(); 123 Object targetObject = invocation.getTargetObject(); 124 Object [] args = invocation.getArguments(); 125 126 return interceptNewInstance(invocation, reflectionMethod, targetObject, args); 127 } 128 129 130 139 public Object interceptMethodInvoke(MethodCalledByConstructorInvocation invocation) throws Throwable  140 { 141 142 Method reflectionMethod = invocation.getCalledMethod(); 143 Object targetObject = invocation.getTargetObject(); 144 Object [] args = invocation.getArguments(); 145 146 return interceptMethodInvoke(invocation, reflectionMethod, targetObject, args); 147 } 148 149 158 public Object interceptMethodInvoke(MethodCalledByMethodInvocation invocation) throws Throwable  159 { 160 161 Method reflectionMethod = invocation.getCalledMethod(); 162 Object targetObject = invocation.getTargetObject(); 163 Object [] args = invocation.getArguments(); 164 165 return interceptMethodInvoke(invocation, reflectionMethod, targetObject, args); 166 } 167 168 169 178 public Object interceptFieldSet(MethodCalledByConstructorInvocation invocation) throws Throwable  179 { 180 181 Method reflectionMethod = invocation.getCalledMethod(); 182 Object targetObject = invocation.getTargetObject(); 183 Object [] args = invocation.getArguments(); 184 185 return interceptFieldSet(invocation, reflectionMethod, targetObject, args); 186 } 187 188 197 public Object interceptFieldSet(MethodCalledByMethodInvocation invocation) throws Throwable  198 { 199 200 Method reflectionMethod = invocation.getCalledMethod(); 201 Object targetObject = invocation.getTargetObject(); 202 Object [] args = invocation.getArguments(); 203 204 return interceptFieldSet(invocation, reflectionMethod, targetObject, args); 205 } 206 207 216 public Object interceptFieldGet(MethodCalledByConstructorInvocation invocation) throws Throwable  217 { 218 219 Method reflectionMethod = invocation.getCalledMethod(); 220 Object targetObject = invocation.getTargetObject(); 221 Object [] args = invocation.getArguments(); 222 223 return interceptFieldGet(invocation, reflectionMethod, targetObject, args); 224 } 225 226 227 236 public Object interceptFieldGet(MethodCalledByMethodInvocation invocation) throws Throwable  237 { 238 239 240 Method reflectionMethod = invocation.getCalledMethod(); 241 Object targetObject = invocation.getTargetObject(); 242 Object [] args = invocation.getArguments(); 243 244 return interceptFieldGet(invocation, reflectionMethod, targetObject, args); 245 } 246 247 248 258 public final Object interceptGetDeclaredMethods(MethodCalledByConstructorInvocation invocation) throws Throwable  259 { 260 261 Object targetObject = invocation.getTargetObject(); 262 return interceptGetDeclaredMethods((Class ) targetObject); 263 } 264 265 275 public final Object interceptGetDeclaredMethods(MethodCalledByMethodInvocation invocation) throws Throwable  276 { 277 278 Object targetObject = invocation.getTargetObject(); 279 return interceptGetDeclaredMethods((Class ) targetObject); 280 } 281 282 292 public final Object interceptGetDeclaredMethod(MethodCalledByConstructorInvocation invocation) throws Throwable  293 { 294 295 Object targetObject = invocation.getTargetObject(); 296 Object [] args = invocation.getArguments(); 297 return interceptGetDeclaredMethod((Class ) targetObject, args); 298 } 299 300 310 public final Object interceptGetDeclaredMethod(MethodCalledByMethodInvocation invocation) throws Throwable  311 { 312 313 Object targetObject = invocation.getTargetObject(); 314 Object [] args = invocation.getArguments(); 315 return interceptGetDeclaredMethod((Class ) targetObject, args); 316 } 317 318 328 public final Object interceptGetMethods(MethodCalledByConstructorInvocation invocation) throws Throwable  329 { 330 331 Object targetObject = invocation.getTargetObject(); 332 return interceptGetMethods((Class ) targetObject); 333 } 334 335 345 public final Object interceptGetMethods(MethodCalledByMethodInvocation invocation) throws Throwable  346 { 347 348 Object targetObject = invocation.getTargetObject(); 349 return interceptGetMethods((Class ) targetObject); 350 } 351 352 362 public final Object interceptGetMethod(MethodCalledByConstructorInvocation invocation) throws Throwable  363 { 364 365 Object targetObject = invocation.getTargetObject(); 366 Object [] args = invocation.getArguments(); 367 return interceptGetMethod((Class ) targetObject, args); 368 } 369 370 380 public final Object interceptGetMethod(MethodCalledByMethodInvocation invocation) throws Throwable  381 { 382 383 Object targetObject = invocation.getTargetObject(); 384 Object [] args = invocation.getArguments(); 385 return interceptGetMethod((Class ) targetObject, args); 386 } 387 388 398 public final Object interceptGetInterfaces(MethodCalledByConstructorInvocation invocation) throws Throwable  399 { 400 401 Object targetObject = invocation.getTargetObject(); 402 return interceptGetInterfaces((Class ) targetObject); 403 } 404 405 415 public final Object interceptGetInterfaces(MethodCalledByMethodInvocation invocation) throws Throwable  416 { 417 418 Object targetObject = invocation.getTargetObject(); 419 return interceptGetInterfaces((Class ) targetObject); 420 } 421 422 431 public final Object interceptGetDeclaredClasses(MethodCalledByConstructorInvocation invocation) throws Throwable  432 { 433 434 Object targetObject = invocation.getTargetObject(); 435 return interceptGetDeclaredClasses((Class ) targetObject); 436 } 437 438 447 public final Object interceptGetDeclaredClasses(MethodCalledByMethodInvocation invocation) throws Throwable  448 { 449 450 Object targetObject = invocation.getTargetObject(); 451 return interceptGetDeclaredClasses((Class ) targetObject); 452 } 453 454 464 public final Object interceptGetClasses(MethodCalledByConstructorInvocation invocation) throws Throwable  465 { 466 467 Object targetObject = invocation.getTargetObject(); 468 return interceptGetClasses((Class ) targetObject); 469 } 470 471 481 public final Object interceptGetClasses(MethodCalledByMethodInvocation invocation) throws Throwable  482 { 483 Object targetObject = invocation.getTargetObject(); 484 return interceptGetClasses((Class ) targetObject); 485 } 486 487 496 public final Object interceptGetDeclaredFields(MethodCalledByConstructorInvocation invocation) throws Throwable  497 { 498 499 Object targetObject = invocation.getTargetObject(); 500 return interceptGetDeclaredFields((Class ) targetObject); 501 } 502 503 512 public final Object interceptGetDeclaredFields(MethodCalledByMethodInvocation invocation) throws Throwable  513 { 514 515 Object targetObject = invocation.getTargetObject(); 516 return interceptGetDeclaredFields((Class ) targetObject); 517 } 518 519 528 public final Object interceptGetDeclaredField(MethodCalledByConstructorInvocation invocation) throws Throwable  529 { 530 531 Object targetObject = invocation.getTargetObject(); 532 Object [] args = invocation.getArguments(); 533 return interceptGetDeclaredField((Class ) targetObject, args); 534 } 535 536 545 public final Object interceptGetDeclaredField(MethodCalledByMethodInvocation invocation) throws Throwable  546 { 547 Object targetObject = invocation.getTargetObject(); 548 Object [] args = invocation.getArguments(); 549 return interceptGetDeclaredField((Class ) targetObject, args); 550 } 551 552 561 public final Object interceptGetFields(MethodCalledByConstructorInvocation invocation) throws Throwable  562 { 563 564 return invocation.invokeNext(); 566 } 567 568 577 public final Object interceptGetFields(MethodCalledByMethodInvocation invocation) throws Throwable  578 { 579 580 return invocation.invokeNext(); 582 } 583 584 593 public final Object interceptGetDeclaredConstructors(MethodCalledByConstructorInvocation invocation) throws Throwable  594 { 595 596 return invocation.invokeNext(); 598 } 599 600 609 public final Object interceptGetDeclaredConstructors(MethodCalledByMethodInvocation invocation) throws Throwable  610 { 611 612 return invocation.invokeNext(); 614 } 615 616 625 public final Object interceptGetDeclaredConstructor(MethodCalledByConstructorInvocation invocation) throws Throwable  626 { 627 628 return invocation.invokeNext(); 630 } 631 632 641 public final Object interceptGetDeclaredConstructor(MethodCalledByMethodInvocation invocation) throws Throwable  642 { 643 644 return invocation.invokeNext(); 646 } 647 648 657 public final Object interceptGetConstructors(MethodCalledByConstructorInvocation invocation) throws Throwable  658 { 659 660 return invocation.invokeNext(); 662 } 663 664 673 public final Object interceptGetConstructors(MethodCalledByMethodInvocation invocation) throws Throwable  674 { 675 676 return invocation.invokeNext(); 678 } 679 680 681 683 685 687 689 690 702 protected Object interceptConstructor(Invocation invocation, Constructor constructor, Object [] args) throws Throwable  703 { 704 return invokeOriginalChainIfExists(invocation, constructor, args); 705 } 706 707 718 protected Object interceptFieldRead(Invocation invocation, Field field, Object instance) throws Throwable  719 { 720 return invokeOriginalChainIfExists(invocation, field, instance); 721 } 722 723 735 protected Object interceptFieldWrite(Invocation invocation, Field field, Object instance, Object arg) throws Throwable  736 { 737 return invokeOriginalChainIfExists(invocation, field, instance, arg); 738 } 739 740 753 protected Object interceptMethod(Invocation invocation, Method method, Object instance, Object [] args) throws Throwable  754 { 755 return invokeOriginalChainIfExists(invocation, method, instance, args); 756 } 757 758 759 761 private Object interceptNewInstance(Invocation invocation, 762 Method reflectionMethod, 763 Object targetObject, 764 Object [] args) throws Throwable  765 { 766 Class reflectionClass = targetObject.getClass(); 767 768 if (reflectionClass.equals(Class .class)) 769 { 770 771 Constructor constructor = ((Class ) targetObject).getConstructor(new Class [0]); 773 return interceptConstructor(invocation, constructor, args); 774 } 775 else if (reflectionClass.equals(Constructor .class)) 776 { 777 778 if (reflectionMethod.getName().equals("newInstance")) 779 { 780 782 Object [] constructorArgs; 783 784 int length = args.length; 785 786 if (length < 1) 787 { 788 constructorArgs = new Object [0]; 789 } 790 else 791 { 792 constructorArgs = (Object []) args[0]; 793 } 794 795 Constructor constructor = (Constructor ) targetObject; 796 return interceptConstructor(invocation, constructor, constructorArgs); 797 } 798 } 799 800 return invocation.invokeNext(); 801 } 802 803 private Object interceptMethodInvoke(Invocation invocation, 804 Method reflectionMethod, 805 Object targetObject, 806 Object [] args) throws Throwable  807 { 808 Method method = (Method ) invocation.getTargetObject(); 809 if (reflectionMethod.getName().equals("invoke")) 810 { 811 Object instance = args[0]; 812 return interceptMethod(invocation, method, instance, (Object []) args[1]); 813 } 814 return invocation.invokeNext(); 815 } 816 817 private Class [] interceptGetInterfaces(Class clazz) 818 { 819 Class [] interfaces = clazz.getInterfaces(); 820 ArrayList cleanedInterfaces = new ArrayList (interfaces.length); 821 822 for (int i = 0; i < interfaces.length; i++) 823 { 824 if (!interfaces[i].equals(Advised.class)) 825 { 826 cleanedInterfaces.add(interfaces[i]); 827 } 828 } 829 830 return (Class []) cleanedInterfaces.toArray(new Class [cleanedInterfaces.size()]); 831 } 832 833 private Object  |