一、前言
通过spring-aop构建包提供的org.springframework.aop.support.AopUtils可以获取的Spring Bean注入的对象实例转为对于的Class类对象(Class<?> beanClazz=AopUtils.getTargetClass(bean);)、代理类的判定(Proxy.isProxyClass、ClassUtils.isCglibProxyClass及SpringProxy))等面向aop编程常用公共方法。
二、代码
package org.springframework.aop.support;@b@@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Proxy;@b@import java.util.Iterator;@b@import java.util.LinkedHashSet;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Set;@b@import org.springframework.aop.Advisor;@b@import org.springframework.aop.AopInvocationException;@b@import org.springframework.aop.ClassFilter;@b@import org.springframework.aop.IntroductionAdvisor;@b@import org.springframework.aop.IntroductionAwareMethodMatcher;@b@import org.springframework.aop.MethodMatcher;@b@import org.springframework.aop.Pointcut;@b@import org.springframework.aop.PointcutAdvisor;@b@import org.springframework.aop.SpringProxy;@b@import org.springframework.aop.TargetClassAware;@b@import org.springframework.core.BridgeMethodResolver;@b@import org.springframework.util.Assert;@b@import org.springframework.util.ClassUtils;@b@import org.springframework.util.ReflectionUtils;@b@@b@public abstract class AopUtils@b@{@b@ public static boolean isAopProxy(Object object)@b@ {@b@ return ((object instanceof SpringProxy) && ((@b@ (Proxy.isProxyClass(object@b@ .getClass())) || (ClassUtils.isCglibProxyClass(object.getClass())))));@b@ }@b@@b@ public static boolean isJdkDynamicProxy(Object object)@b@ {@b@ return ((object instanceof SpringProxy) && (Proxy.isProxyClass(object.getClass())));@b@ }@b@@b@ public static boolean isCglibProxy(Object object)@b@ {@b@ return ((object instanceof SpringProxy) && (ClassUtils.isCglibProxy(object)));@b@ }@b@@b@ @Deprecated@b@ public static boolean isCglibProxyClass(Class<?> clazz)@b@ {@b@ return ClassUtils.isCglibProxyClass(clazz);@b@ }@b@@b@ @Deprecated@b@ public static boolean isCglibProxyClassName(String className)@b@ {@b@ return ClassUtils.isCglibProxyClassName(className);@b@ }@b@@b@ public static Class<?> getTargetClass(Object candidate)@b@ {@b@ Assert.notNull(candidate, "Candidate object must not be null");@b@ Class result = null;@b@ if (candidate instanceof TargetClassAware)@b@ result = ((TargetClassAware)candidate).getTargetClass();@b@@b@ if (result == null)@b@ result = (isCglibProxy(candidate)) ? candidate.getClass().getSuperclass() : candidate.getClass();@b@@b@ return result;@b@ }@b@@b@ public static boolean isEqualsMethod(Method method)@b@ {@b@ return ReflectionUtils.isEqualsMethod(method);@b@ }@b@@b@ public static boolean isHashCodeMethod(Method method)@b@ {@b@ return ReflectionUtils.isHashCodeMethod(method);@b@ }@b@@b@ public static boolean isToStringMethod(Method method)@b@ {@b@ return ReflectionUtils.isToStringMethod(method);@b@ }@b@@b@ public static boolean isFinalizeMethod(Method method)@b@ {@b@ return ((method != null) && (method.getName().equals("finalize")) && @b@ (method@b@ .getParameterTypes().length == 0));@b@ }@b@@b@ public static Method getMostSpecificMethod(Method method, Class<?> targetClass)@b@ {@b@ Method resolvedMethod = ClassUtils.getMostSpecificMethod(method, targetClass);@b@@b@ return BridgeMethodResolver.findBridgedMethod(resolvedMethod);@b@ }@b@@b@ public static boolean canApply(Pointcut pc, Class<?> targetClass)@b@ {@b@ return canApply(pc, targetClass, false);@b@ }@b@@b@ public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions)@b@ {@b@ Method[] arrayOfMethod1;@b@ int j;@b@ Assert.notNull(pc, "Pointcut must not be null");@b@ if (!(pc.getClassFilter().matches(targetClass))) {@b@ return false;@b@ }@b@@b@ MethodMatcher methodMatcher = pc.getMethodMatcher();@b@ IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;@b@ if (methodMatcher instanceof IntroductionAwareMethodMatcher) {@b@ introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher)methodMatcher;@b@ }@b@@b@ Set classes = new LinkedHashSet(ClassUtils.getAllInterfacesForClassAsSet(targetClass));@b@ classes.add(targetClass);@b@ for (Class clazz : classes) {@b@ Method[] methods = clazz.getMethods();@b@ arrayOfMethod1 = methods; int i = arrayOfMethod1.length; for (j = 0; j < i; ++j) { Method method = arrayOfMethod1[j];@b@ if (((introductionAwareMethodMatcher != null) && @b@ (introductionAwareMethodMatcher@b@ .matches(method, targetClass, hasIntroductions))) || @b@ (methodMatcher@b@ .matches(method, targetClass)))@b@ {@b@ return true;@b@ }@b@ }@b@ }@b@@b@ return false;@b@ }@b@@b@ public static boolean canApply(Advisor advisor, Class<?> targetClass)@b@ {@b@ return canApply(advisor, targetClass, false);@b@ }@b@@b@ public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions)@b@ {@b@ if (advisor instanceof IntroductionAdvisor)@b@ return ((IntroductionAdvisor)advisor).getClassFilter().matches(targetClass);@b@@b@ if (advisor instanceof PointcutAdvisor) {@b@ PointcutAdvisor pca = (PointcutAdvisor)advisor;@b@ return canApply(pca.getPointcut(), targetClass, hasIntroductions);@b@ }@b@@b@ return true;@b@ }@b@@b@ public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz)@b@ {@b@ boolean bool1;@b@ if (candidateAdvisors.isEmpty())@b@ return candidateAdvisors;@b@@b@ List eligibleAdvisors = new LinkedList();@b@ for (Iterator localIterator = candidateAdvisors.iterator(); localIterator.hasNext(); ) { candidate = (Advisor)localIterator.next();@b@ if ((candidate instanceof IntroductionAdvisor) && (canApply(candidate, clazz)))@b@ eligibleAdvisors.add(candidate);@b@ }@b@@b@ boolean bool1 = !(eligibleAdvisors.isEmpty());@b@ Advisor candidate = candidateAdvisors.iterator();@b@ while (true) { Advisor candidate;@b@ while (true) { if (!(candidate.hasNext())) break label153; candidate = (Advisor)candidate.next();@b@ if (!(candidate instanceof IntroductionAdvisor))@b@ break;@b@ }@b@@b@ if (canApply(candidate, clazz, bool1))@b@ eligibleAdvisors.add(candidate);@b@ }@b@@b@ label153: return eligibleAdvisors;@b@ }@b@@b@ public static Object invokeJoinpointUsingReflection(Object target, Method method, Object[] args)@b@ throws Throwable@b@ {@b@ try@b@ {@b@ ReflectionUtils.makeAccessible(method);@b@ return method.invoke(target, args);@b@ }@b@ catch (InvocationTargetException ex)@b@ {@b@ throw ex.getTargetException();@b@ }@b@ catch (IllegalArgumentException ex) {@b@ throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" + method + "] on target [" + target + "]", ex);@b@ }@b@ catch (IllegalAccessException ex)@b@ {@b@ throw new AopInvocationException("Could not access method [" + method + "]", ex);@b@ }@b@ }@b@}