1 16 17 package org.springframework.aop.support; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.lang.reflect.Proxy ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import org.springframework.aop.Advisor; 30 import org.springframework.aop.AopInvocationException; 31 import org.springframework.aop.IntroductionAdvisor; 32 import org.springframework.aop.IntroductionAwareMethodMatcher; 33 import org.springframework.aop.MethodMatcher; 34 import org.springframework.aop.Pointcut; 35 import org.springframework.aop.PointcutAdvisor; 36 import org.springframework.aop.SpringProxy; 37 import org.springframework.aop.TargetClassAware; 38 import org.springframework.util.Assert; 39 import org.springframework.util.ClassUtils; 40 41 54 public abstract class AopUtils { 55 56 62 public static boolean isAopProxy(Object object) { 63 return (object instanceof SpringProxy && 64 (Proxy.isProxyClass(object.getClass()) || isCglibProxyClass(object.getClass()))); 65 } 66 67 72 public static boolean isJdkDynamicProxy(Object object) { 73 return (object instanceof SpringProxy && Proxy.isProxyClass(object.getClass())); 74 } 75 76 80 public static boolean isCglibProxy(Object object) { 81 return (object instanceof SpringProxy && isCglibProxyClass(object.getClass())); 82 } 83 84 88 public static boolean isCglibProxyClass(Class clazz) { 89 return (clazz != null && clazz.getName().indexOf("$$") != -1); 90 } 91 92 100 public static Class getTargetClass(Object candidate) { 101 Assert.notNull(candidate, "Candidate object must not be null"); 102 if (candidate instanceof TargetClassAware) { 103 return ((TargetClassAware) candidate).getTargetClass(); 104 } 105 if (isCglibProxyClass(candidate.getClass())) { 106 return candidate.getClass().getSuperclass(); 107 } 108 return candidate.getClass(); 109 } 110 111 115 public static boolean isEqualsMethod(Method method) { 116 return (method != null && method.getName().equals("equals") && 117 method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Object .class); 118 } 119 120 124 public static boolean isHashCodeMethod(Method method) { 125 return (method != null && method.getName().equals("hashCode") && 126 method.getParameterTypes().length == 0); 127 } 128 129 133 public static boolean isToStringMethod(Method method) { 134 return (method != null && method.getName().equals("toString") && 135 method.getParameterTypes().length == 0); 136 } 137 138 150 public static Method getMostSpecificMethod(Method method, Class targetClass) { 151 if (method != null && targetClass != null) { 152 try { 153 method = targetClass.getMethod(method.getName(), method.getParameterTypes()); 154 } 155 catch (NoSuchMethodException ex) { 156 } 159 } 160 return method; 161 } 162 163 164 172 public static boolean canApply(Pointcut pc, Class targetClass) { 173 return canApply(pc, targetClass, false); 174 } 175 176 186 public static boolean canApply(Pointcut pc, Class targetClass, boolean hasIntroductions) { 187 if (!pc.getClassFilter().matches(targetClass)) { 188 return false; 189 } 190 191 MethodMatcher methodMatcher = pc.getMethodMatcher(); 192 IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null; 193 if (methodMatcher instanceof IntroductionAwareMethodMatcher) { 194 introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher; 195 } 196 197 Set classes = new HashSet (ClassUtils.getAllInterfacesForClassAsSet(targetClass)); 198 classes.add(targetClass); 199 for (Iterator it = classes.iterator(); it.hasNext();) { 200 Class clazz = (Class ) it.next(); 201 Method [] methods = clazz.getMethods(); 202 for (int j = 0; j < methods.length; j++) { 203 if ((introductionAwareMethodMatcher != null && 204 introductionAwareMethodMatcher.matches(methods[j], targetClass, hasIntroductions)) || 205 methodMatcher.matches(methods[j], targetClass)) { 206 return true; 207 } 208 } 209 } 210 211 return false; 212 } 213 214 222 public static boolean canApply(Advisor advisor, Class targetClass) { 223 return canApply(advisor, targetClass, false); 224 } 225 226 238 public static boolean canApply(Advisor advisor, Class targetClass, boolean hasIntroductions) { 239 if (advisor instanceof IntroductionAdvisor) { 240 return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass); 241 } 242 else if (advisor instanceof PointcutAdvisor) { 243 PointcutAdvisor pca = (PointcutAdvisor) advisor; 244 return canApply(pca.getPointcut(), targetClass, hasIntroductions); 245 } 246 else { 247 return true; 249 } 250 } 251 252 259 public static List findAdvisorsThatCanApply(List candidateAdvisors, Class clazz) { 260 List eligibleAdvisors = new LinkedList (); 261 for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) { 262 Advisor candidate = (Advisor) it.next(); 263 if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) { 264 eligibleAdvisors.add(candidate); 265 } 266 } 267 boolean hasIntroductions = !eligibleAdvisors.isEmpty(); 268 for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) { 269 Advisor candidate = (Advisor) it.next(); 270 if (candidate instanceof IntroductionAdvisor) { 271 continue; 273 } 274 if (canApply(candidate, clazz, hasIntroductions)) { 275 eligibleAdvisors.add(candidate); 276 } 277 } 278 return eligibleAdvisors; 279 } 280 281 282 290 public static Object invokeJoinpointUsingReflection(Object target, Method method, Object [] args) 291 throws Throwable { 292 293 try { 295 if (!Modifier.isPublic(method.getModifiers()) || 296 !Modifier.isPublic(method.getDeclaringClass().getModifiers())) { 297 method.setAccessible(true); 298 } 299 return method.invoke(target, args); 300 } 301 catch (InvocationTargetException ex) { 302 throw ex.getTargetException(); 305 } 306 catch (IllegalArgumentException ex) { 307 throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" + 308 method + "] on target [" + target + "]", ex); 309 } 310 catch (IllegalAccessException ex) { 311 throw new AopInvocationException("Could not access method [" + method + "]", ex); 312 } 313 } 314 315 } 316 | Popular Tags |