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 interceptFieldSet(Invocation invocation, Method reflectionMethod, Object targetObject, Object [] args) throws Throwable 834 { 835 Field field = (Field ) invocation.getTargetObject(); 836 Matcher m = fieldSetPattern.matcher(reflectionMethod.getName()); 837 if (m.matches()) 838 { 839 Object instance = args[0]; 840 return interceptFieldWrite(invocation, field, instance, args[1]); 841 } 842 return invocation.invokeNext(); 843 } 844 845 private Object interceptFieldGet(Invocation invocation, Method reflectionMethod, Object targetObject, Object [] args) throws Throwable 846 { 847 Field field = (Field ) invocation.getTargetObject(); 848 Matcher m = fieldGetPattern.matcher(reflectionMethod.getName()); 849 if (m.matches()) 850 { 851 Object instance = args[0]; 852 return interceptFieldRead(invocation, field, instance); 853 } 854 return invocation.invokeNext(); 855 } 856 857 private Method [] interceptGetDeclaredMethods(Class clazz) 858 { 859 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 860 861 if (advisor == null) 862 { 863 return clazz.getDeclaredMethods(); 864 } 865 else 866 { 867 Object [] advisedMethods = advisor.getAdvisedMethods().getValues(); 868 869 ArrayList methods = new ArrayList (advisedMethods.length); 870 871 for (int i = 0; i < advisedMethods.length; i++) 872 { 873 Method m = (Method ) advisedMethods[i]; 874 if (clazz.equals(m.getDeclaringClass()) && isNotAccessMethod(m) && isNotJavassistWrappedMethod(m)) 875 { 876 methods.add(m); 877 } 878 } 879 880 return (Method []) methods.toArray(new Method [methods.size()]); 881 } 882 } 883 884 private Method interceptGetDeclaredMethod(Class clazz, Object [] args) throws NoSuchMethodException 885 { 886 887 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 888 Method method = clazz.getDeclaredMethod((String ) args[0], (Class []) args[1]); 889 890 if (advisor == null) 891 { 892 return method; 893 } 894 else 895 { 896 Object [] advisedMethods = advisor.getAdvisedMethods().getValues(); 897 898 for (int i = 0; i < advisedMethods.length; i++) 899 { 900 Method m = (Method ) advisedMethods[i]; 901 if (m.equals(method) && isNotAccessMethod(m) && isNotJavassistWrappedMethod(m)) 902 { 903 return method; 904 } 905 } 906 } 907 908 throw new NoSuchMethodException (); 909 } 910 911 912 917 private boolean isNotAccessMethod(Method m) 918 { 919 920 923 if (Modifier.isStatic(m.getModifiers())) 924 { 925 Matcher match = accessMethodPattern.matcher(m.getName()); 926 if (match.matches()) 927 { 928 return false; 929 } 930 } 931 932 return true; 933 } 934 935 private boolean isNotJavassistWrappedMethod(Method m) 936 { 937 if (Modifier.isPrivate(m.getModifiers()) && !Modifier.isStatic(m.getModifiers())) 938 { 939 if (m.getName().startsWith("_added_m$")) 940 { 941 return false; 942 } 943 } 944 945 return true; 946 } 947 948 private Method [] interceptGetMethods(Class clazz) 949 { 950 951 ArrayList methods = new ArrayList (); 952 953 GetMethodsAlreadyFound methodsFound = new GetMethodsAlreadyFound(); 957 958 while (clazz != null) 959 { 960 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 961 Object [] foundMethods; 962 963 if (advisor == null) 964 { 965 foundMethods = clazz.getDeclaredMethods(); 966 } 967 else 968 { 969 foundMethods = advisor.getAdvisedMethods().getValues(); 970 } 971 972 for (int i = 0; i < foundMethods.length; i++) 973 { 974 Method m = (Method ) foundMethods[i]; 975 if (clazz.equals(m.getDeclaringClass()) && Modifier.isPublic(m.getModifiers())) 976 { 977 978 if (!methodsFound.existsMethod(m)) 979 { 980 methods.add(m); 981 methodsFound.addMethod(m); 982 } 983 } 984 } 985 986 clazz = clazz.getSuperclass(); 987 } 988 989 return (Method []) methods.toArray(new Method [methods.size()]); 990 } 991 992 private Method interceptGetMethod(Class clazz, Object [] args) throws NoSuchMethodException 993 { 994 Method method = clazz.getMethod((String ) args[0], (Class []) args[1]); 995 Class declaringClass = method.getDeclaringClass(); 996 997 while (clazz != null) 998 { 999 if (clazz.equals(declaringClass)) 1001 { 1002 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 1003 1004 if (advisor == null) 1005 { 1006 return method; 1007 } 1008 else 1009 { 1010 Object [] methods = advisor.getAdvisedMethods().getValues(); 1011 1012 for (int i = 0; i < methods.length; i++) 1013 { 1014 Method m = (Method ) methods[i]; 1015 if (m.equals(method)) 1016 { 1017 return method; 1018 } 1019 } 1020 } 1021 } 1022 1023 clazz = clazz.getSuperclass(); 1024 } 1025 1026 throw new NoSuchMethodException (); 1027 } 1028 1029 1030 private Field [] interceptGetDeclaredFields(Class clazz) 1031 { 1032 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 1033 1034 if (advisor == null) 1035 { 1036 return clazz.getDeclaredFields(); 1037 } 1038 else 1039 { 1040 Field [] advisedFields = advisor.getAdvisedFields(); 1041 1042 ArrayList fields = new ArrayList (advisedFields.length); 1043 1044 for (int i = 0; i < advisedFields.length; i++) 1045 { 1046 Field f = advisedFields[i]; 1047 if (clazz.equals(f.getDeclaringClass())) 1048 { 1049 fields.add(f); 1050 } 1051 } 1052 1053 return (Field []) fields.toArray(new Field [fields.size()]); 1054 } 1055 1056 } 1057 1058 1062 private Class [] interceptGetClasses(Class clazz) throws Throwable 1063 { 1064 Class [] classes = clazz.getClasses(); 1065 return cleanClasses(classes); 1066 } 1067 1068 private Class [] interceptGetDeclaredClasses(Class clazz) throws Throwable 1069 { 1070 Class [] classes = clazz.getDeclaredClasses(); 1071 return cleanClasses(classes); 1072 } 1073 1074 private Class [] cleanClasses(Class [] classes) 1075 { 1076 ArrayList clazzes = new ArrayList (); 1077 1078 for (int i = 0; i < classes.length; i++) 1079 { 1080 Class innerClass = classes[i]; 1081 1082 Class [] interfaces = classes[i].getInterfaces(); 1084 boolean implUntransformable = false; 1085 1086 for (int j = 0; j < interfaces.length; j++) 1087 { 1088 if (interfaces[j].equals(Untransformable.class)) 1089 { 1090 implUntransformable = true; 1091 break; 1092 } 1093 } 1094 1095 if (!implUntransformable) 1096 { 1097 boolean isInvocationImpl = false; 1099 Class superclass = innerClass.getSuperclass(); 1100 while (superclass != null) 1101 { 1102 1103 interfaces = classes[i].getInterfaces(); 1104 1105 for (int j = 0; j < interfaces.length; j++) 1106 { 1107 if (interfaces[j].equals(Invocation.class)) 1108 { 1109 isInvocationImpl = true; 1110 break; 1111 } 1112 } 1113 superclass = superclass.getSuperclass(); 1114 } 1115 1116 if (!isInvocationImpl) 1117 { 1118 clazzes.add(innerClass); 1119 } 1120 } 1121 } 1122 1123 return (Class []) clazzes.toArray(new Class [clazzes.size()]); 1124 } 1125 1126 private Field interceptGetDeclaredField(Class clazz, Object [] args) throws NoSuchFieldException 1127 { 1128 1129 ClassAdvisor advisor = AspectManager.instance().getAdvisorIfAdvised(clazz); 1130 Field field = clazz.getDeclaredField((String ) args[0]); 1131 1132 if (advisor == null) 1133 { 1134 return field; 1135 } 1136 else 1137 { 1138 Field [] advisedFields = advisor.getAdvisedFields(); 1139 1140 for (int i = 0; i < advisedFields.length; i++) 1141 { 1142 Field f = advisedFields[i]; 1143 if (f.equals(field)) 1144 { 1145 return field; 1146 } 1147 } 1148 } 1149 1150 throw new NoSuchFieldException (); 1151 } 1152 1153 1160 private Object invokeOriginalChainIfExists(Invocation invocation, Constructor constructor, Object [] args) throws Throwable 1161 { 1162 if (invocation instanceof MethodCalledByConstructorInvocation) 1164 { 1165 MethodCalledByConstructorInvocation inv = (MethodCalledByConstructorInvocation) invocation; 1166 Constructor callingCon = inv.getCalling(); 1167 Class callingClass = callingCon.getDeclaringClass(); 1168 if (isAdvised(callingClass)) 1169 { 1170 ClassAdvisor advisor = AspectManager.instance().getAdvisor(callingClass); 1171 if (advisor != null) 1172 { 1173 int index = advisor.getConstructorIndex(callingCon); 1174 if (index >= 0) 1175 { 1176 HashMap calledClassesMap = advisor.getConCalledByConInterceptors()[index]; 1177 if (calledClassesMap != null) 1178 { 1179 TLongObjectHashMap calledCons = (TLongObjectHashMap) calledClassesMap.get(constructor.getDeclaringClass().getName()); 1180 if (calledCons != null) 1181 { 1182 long calledHash = MethodHashing.constructorHash(constructor); 1183 ConByConInfo info = (ConByConInfo) calledCons.get(calledHash); 1184 1185 if (info != null && info.hasAdvices()) 1186 { 1187 return advisor.invokeConCalledByCon(info, args); 1188 } 1189 } 1190 } 1191 } 1192 } 1193 } 1194 } 1195 else if (invocation instanceof MethodCalledByMethodInvocation) 1196 { 1197 MethodCalledByMethodInvocation inv = (MethodCalledByMethodInvocation) invocation; 1198 Method callingMethod = inv.getCallingMethod(); 1199 if (isAdvised(callingMethod.getDeclaringClass())) 1200 { 1201 ClassAdvisor advisor = AspectManager.instance().getAdvisor(callingMethod.getDeclaringClass()); 1202 if (advisor != null) 1203 { 1204 long callingMethodHash = MethodHashing.calculateHash(callingMethod); 1205 long calledHash = MethodHashing.constructorHash(constructor); 1206 1207 HashMap calledClassesMap = (HashMap) advisor.getConCalledByMethodInterceptors().get(callingMethodHash); 1208 if (calledClassesMap != null) 1209 { 1210 TLongObjectHashMap calledCons = (TLongObjectHashMap) calledClassesMap.get(constructor.getDeclaringClass().getName()); 1211 if (calledCons != null) 1212 { 1213 ConByMethodInfo info = (ConByMethodInfo) calledCons.get(calledHash); 1215 1216 if (info != null && info.hasAdvices()) 1217 { 1218 return advisor.invokeConCalledByMethod(info, inv.getCallingObject(), args); 1220 } 1221 } 1222 } 1223 } 1224 } 1225 } 1226 1227 Class calledClass = constructor.getDeclaringClass(); 1229 if (isAdvised(calledClass)) 1230 { 1231 ClassAdvisor advisor = AspectManager.instance().getAdvisor(constructor.getDeclaringClass()); 1232 1233 if (advisor != null && advisor.hasAspects()) 1234 { 1235 int index = advisor.getConstructorIndex(constructor); 1236 if (index >= 0) 1237 { 1238 ConstructorInfo jp = advisor.getConstructorInfos()[index]; 1239 return jp.getWrapper().invoke(null, args); 1240 } 1241 } 1242 } 1243 return invocation.invokeNext(); 1244 } 1245 1246 1247 1253 private Object invokeOriginalChainIfExists(Invocation invocation, Field field, Object targetObject) throws Throwable 1254 { 1255 1257 ClassAdvisor advisor = AspectManager.instance().getAdvisor(field.getDeclaringClass()); 1259 1260 Class calledClass = field.getDeclaringClass(); 1261 if (isAdvised(calledClass)) 1262 { 1263 if (advisor != null && advisor.hasAspects()) 1264 { 1265 int index = advisor.getFieldIndex(field); 1266 if (index >= 0) 1267 { 1268 FieldInfo jp = advisor.getFieldReadInfos()[index]; 1269 return jp.getWrapper().invoke(null, new Object [] {targetObject}); 1270 } 1271 } 1272 } 1273 return invocation.invokeNext(); 1274 } 1275 1276 1282 private Object invokeOriginalChainIfExists(Invocation invocation, Field field, Object targetObject, Object value) throws Throwable 1283 { 1284 1286 ClassAdvisor advisor = AspectManager.instance().getAdvisor(field.getDeclaringClass()); 1288 1289 Class calledClass = field.getDeclaringClass(); 1290 if (isAdvised(calledClass)) 1291 { 1292 if (advisor != null && advisor.hasAspects()) 1293 { 1294 int index = advisor.getFieldIndex(field); 1295 if (index >= 0) 1296 { 1297 FieldInfo jp = advisor.getFieldWriteInfos()[index]; 1298 return jp.getWrapper().invoke(null, new Object [] {targetObject, value}); 1299 } 1300 } 1301 } 1302 return invocation.invokeNext(); 1303 } 1304 1305 1311 private Object invokeOriginalChainIfExists(Invocation invocation, Method method, Object targetObject, Object [] args) throws Throwable 1312 { 1313 if (invocation instanceof MethodCalledByConstructorInvocation) 1315 { 1316 MethodCalledByConstructorInvocation inv = (MethodCalledByConstructorInvocation) invocation; 1317 Constructor callingCon = inv.getCalling(); 1318 Class callingClass = callingCon.getDeclaringClass(); 1319 if (isAdvised(callingClass)) 1320 { 1321 ClassAdvisor advisor = AspectManager.instance().getAdvisor(callingClass); 1322 if (advisor != null) 1323 { 1324 int index = advisor.getConstructorIndex(callingCon); 1325 if (index >= 0) 1326 { 1327 HashMap calledClassesMap = advisor.getMethodCalledByConInterceptors()[index]; 1328 if (calledClassesMap != null) 1329 { 1330 TLongObjectHashMap calledMethods = (TLongObjectHashMap) calledClassesMap.get(method.getDeclaringClass().getName()); 1331 if (calledMethods != null) 1332 { 1333 long calledHash = MethodHashing.calculateHash(method); 1334 MethodByConInfo info = (MethodByConInfo) calledMethods.get(calledHash); 1336 1337 if (info != null && info.hasAdvices()) 1338 { 1339 return advisor.invokeConstructorCaller(info, targetObject, args); 1341 } 1342 } 1343 } 1344 } 1345 } 1346 } 1347 } 1348 else if (invocation instanceof MethodCalledByMethodInvocation) 1349 { 1350 MethodCalledByMethodInvocation inv = (MethodCalledByMethodInvocation) invocation; 1351 Method callingMethod = inv.getCallingMethod(); 1352 if (isAdvised(callingMethod.getDeclaringClass())) 1353 { 1354 ClassAdvisor advisor = AspectManager.instance().getAdvisor(callingMethod.getDeclaringClass()); 1355 if (advisor != null) 1356 { 1357 long callingMethodHash = MethodHashing.calculateHash(callingMethod); 1358 long calledHash = MethodHashing.calculateHash(method); 1359 1360 HashMap calledClassesMap = (HashMap) advisor.getMethodCalledByMethodInterceptors().get(callingMethodHash); 1361 if (calledClassesMap != null) 1362 { 1363 TLongObjectHashMap calledMethods = (TLongObjectHashMap) calledClassesMap.get(method.getDeclaringClass().getName()); 1364 if (calledMethods != null) 1365 { 1366 MethodByMethodInfo info = (MethodByMethodInfo) calledMethods.get(calledHash); 1368 1369 if (info != null && info.hasAdvices()) 1370 { 1371 return advisor.invokeCaller(info, inv.getCallingObject(), targetObject, args); 1373 } 1374 } 1375 } 1376 } 1377 } 1378 } 1379 1380 return invocation.invokeNext(); 1383 } 1384 1385 private boolean isAdvised(Class clazz) 1386 { 1387 Class [] interfaces = clazz.getInterfaces(); 1388 1389 for (int i = 0; i < interfaces.length; i++) 1390 { 1391 if (interfaces[i].equals(Advised.class)) 1392 { 1393 return true; 1394 } 1395 } 1396 1397 Class superClass = clazz.getSuperclass(); 1399 if (superClass != null) 1400 { 1401 return isAdvised(superClass); 1402 } 1403 1404 return false; 1405 } 1406 1408 1410 class GetMethodsAlreadyFound 1411 { 1412 HashMap methodMap = new HashMap(); 1413 1414 public void addMethod(Method m) 1415 { 1416 String methodName = m.getName(); 1417 ArrayList methods = (ArrayList ) methodMap.get(methodName); 1418 1419 if (methods == null) 1420 { 1421 methods = new ArrayList (); 1422 methodMap.put(methodName, methods); 1423 } 1424 1425 methods.add(m); 1426 } 1427 1428 public boolean existsMethod(Method method) 1429 { 1430 ArrayList methods = (ArrayList ) methodMap.get(method.getName()); 1431 1432 if (methods == null) 1433 { 1434 return false; 1435 } 1436 1437 Class [] methodParamTypes = method.getParameterTypes(); 1438 1439 for (Iterator it = methods.iterator(); it.hasNext();) 1440 { 1441 Method found = (Method ) it.next(); 1442 Class [] foundParamTypes = found.getParameterTypes(); 1443 1444 if (methodParamTypes.length == foundParamTypes.length) 1445 { 1446 1447 boolean same = true; 1448 for (int i = 0; i < methodParamTypes.length; i++) 1449 { 1450 if (!methodParamTypes[i].equals(foundParamTypes[i])) 1451 { 1452 same = false; 1453 } 1454 } 1455 1456 if (same) 1457 { 1458 return true; 1459 } 1460 } 1461 } 1462 1463 return false; 1464 } 1465 } 1466} 1467 | Popular Tags |