ProccedingJoinPoint源码解析

ProccedingJoinPoint的继承关系

ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: 
   java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
   java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。 

源码解析    

public interface ProceedingJoinPoint extends JoinPoint {
    void set$AroundClosure(AroundClosure var1);

    Object proceed() throws Throwable;

    Object proceed(Object[] var1) throws Throwable;
}

ProceedingJoinPoint是个接口,定义了两个方法,上面有说明。那么proceed方法是怎么实现的呢?它有两种种实现方式

我们只看第二种,MethodInvocationPorceedingJoinPorint的实现

public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint, StaticPart {
    private final ProxyMethodInvocation methodInvocation;
    public Object proceed() throws Throwable {
        return this.methodInvocation.invocableClone().proceed();
    }
}

从源码可以看出,MethodInvocationPorceedingJoinPorint实现了ProceedingJoinPoint接口,proceed方法使用了ProxyMethodInvocation 类的invocableClone方法来获取反射克隆对象。

//ProxyMethodInvocation 类中的invocableClone方法

public MethodInvocation invocableClone(Object... arguments) {
        if (this.userAttributes == null) {
            this.userAttributes = new HashMap();
        }

        try {
            //获得当前对象的反射克隆对象
            ReflectiveMethodInvocation clone = (ReflectiveMethodInvocation)this.clone();
            clone.arguments = arguments;
            return clone;
        } catch (CloneNotSupportedException var3) {
            throw new IllegalStateException("Should be able to clone object of type [" + this.getClass() + "]: " + var3);
        }
    }

获得对象之后调用ReflectiveMethodInvocation类中的proceed方法。注:

public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable

 

 public Object proceed() throws Throwable {
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return this.invokeJoinpoint();
        } else {
            Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
            if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
                InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
                return dm.methodMatcher.matches(this.method, this.targetClass, this.arguments) ? dm.interceptor.invoke(this) : this.proceed();
            } else {
                return ((MethodInterceptor)interceptorOrInterceptionAdvice).invoke(this);  //反射执行需要执行的方法
            }
        }
    }

通过反射执行你需要执行的方法。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值