一、目标
本章我们简化MethodInvocation的逻辑,将cglib与jdk动态代理的内容,统一封装到ReflectiveMethodInvocation
二、实现
简化cglib实现
public class Cglib2AopProxy implements AopProxy {private final AdvisedSupport advised;public Cglib2AopProxy(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object getProxy() {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(advised.getTargetSource().getTarget().getClass());enhancer.setInterfaces(advised.getTargetSource().getTargetClass());enhancer.setCallback(new DynamicAdvisedInterceptor(advised));return enhancer.create();}private static class DynamicAdvisedInterceptor implements MethodInterceptor {private final AdvisedSupport advised;public DynamicAdvisedInterceptor(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {// 创建方法调用链ReflectiveMethodInvocation methodInvocation = new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, objects, advised.getAdvisors());// 执行拦截器链return methodInvocation.proceed();}}}
简化jdk实现
public class JdkDynamicAopProxy implements AopProxy, InvocationHandler {private final AdvisedSupport advised;public JdkDynamicAopProxy(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object getProxy() {return Proxy.newProxyInstance(advised.getTargetSource().getTarget().getClass().getClassLoader(),advised.getTargetSource().getTargetClass(),this);}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// 创建方法调用链ReflectiveMethodInvocation methodInvocation = new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args, advised.getAdvisors());// 执行拦截器链return methodInvocation.proceed();}}
将proceed代码,统一放置到ReflectiveMethodInvocation
public class ReflectiveMethodInvocation implements MethodInvocation {// 目标对象protected final Object target;// 方法protected final Method method;// 入参protected final Object[] arguments;// 当前执行的通知器索引private int currentInterceptorIndex = -1;// 通知器列表private final List<Advisor> advisors;public ReflectiveMethodInvocation(Object target, Method method, Object[] arguments, List<Advisor> advisors) {this.target = target;this.method = method;this.arguments = arguments;this.advisors = advisors;}@Overridepublic Method getMethod() {return method;}@Overridepublic Object[] getArguments() {return arguments;}@Overridepublic Object proceed() throws Throwable {// 如果所有通知器都执行完毕,调用目标方法if (this.currentInterceptorIndex == this.advisors.size() - 1) {return this.method.invoke(this.target, this.arguments);}// 获取下一个通知器Advisor advisor = this.advisors.get(++this.currentInterceptorIndex);// 判断方法是否匹配切点if (advisor.getPointcut().getMethodMatcher().matches(this.method, this.target.getClass())) {// 将 Advice 包装成 MethodInterceptorMethodInterceptor interceptor = getInterceptor(advisor);// 执行拦截器逻辑return interceptor.invoke(this);}// 如果方法不匹配,跳过当前通知器,继续执行下一个return proceed();}@Overridepublic Object getThis() {return target;}@Overridepublic AccessibleObject getStaticPart() {return method;}// 将 Advice 包装成 MethodInterceptorprotected MethodInterceptor getInterceptor(Advisor advisor) {if (advisor.getAdvice() instanceof MethodInterceptor) {return (MethodInterceptor) advisor.getAdvice();}// 如果是 MethodBeforeAdvice,使用适配器包装if (advisor.getAdvice() instanceof MethodBeforeAdvice) {return new MethodBeforeAdviceInterceptor((MethodBeforeAdvice) advisor.getAdvice());}throw new UnsupportedOperationException("Advice type not supported: " + advisor.getAdvice().getClass());}
}