Package us.codecraft.tinyioc.aop

Source Code of us.codecraft.tinyioc.aop.JdkDynamicAopProxy

package us.codecraft.tinyioc.aop;

import org.aopalliance.intercept.MethodInterceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* 基于jdk的动态代理
*
* @author yihua.huang@dianping.com
*/
public class JdkDynamicAopProxy extends AbstractAopProxy implements InvocationHandler {

    public JdkDynamicAopProxy(AdvisedSupport advised) {
        super(advised);
    }

  @Override
  public Object getProxy() {
    return Proxy.newProxyInstance(getClass().getClassLoader(), advised.getTargetSource().getInterfaces(), this);
  }

  @Override
  public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    MethodInterceptor methodInterceptor = advised.getMethodInterceptor();
    if (advised.getMethodMatcher() != null
        && advised.getMethodMatcher().matches(method, advised.getTargetSource().getTarget().getClass())) {
      return methodInterceptor.invoke(new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(),
          method, args));
    } else {
      return method.invoke(advised.getTargetSource().getTarget(), args);
    }
  }

}
TOP

Related Classes of us.codecraft.tinyioc.aop.JdkDynamicAopProxy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.