编程语言
首页 > 编程语言> > dubbo源码阅读-ProxyFactory(十一)之Javassist

dubbo源码阅读-ProxyFactory(十一)之Javassist

作者:互联网

说明

详见《dubbo源码阅读-ProxyFactory(十一)之JdkProxyFactory》

接口定义

详见《dubbo源码阅读-ProxyFactory(十一)之JdkProxyFactory》

类图

JavassistProxyFactory

/**
 * JavaassistRpcProxyFactory
 */
public class JavassistProxyFactory extends AbstractProxyFactory {

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
        //<1>javasist动态生成Proxy再调用newinstance
        return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
    }

    @Override
    public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
        //<2>使用javasisit动态生成Wrapper 感兴趣可以看生成规则
        final Wrapper wrapper = Wrapper.getWrapper(proxy.getClass().getName().indexOf('$') < 0 ? proxy.getClass() : type);
        return new AbstractProxyInvoker<T>(proxy, type, url) {
            @Override
            protected Object doInvoke(T proxy, String methodName,
                                      Class<?>[] parameterTypes,
                                      Object[] arguments) throws Throwable {
                return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
            }
        };
    }

}

<1>动态生成Proxy

Prxy只是一个工厂

/**
 * @author liqiang
 * @date 2020/3/17 11:27
 * @Description: (what)
 * (why)
 * (how)
 */
public class Proxy0 extends Proxy {
    /**
     * get instance with special handler.
     *
     * @param handler
     * @return instance.
     */
    @Override
    public Object newInstance(java.lang.reflect.InvocationHandler h) {
//<3> return new proxy0(h); } }

<3>proxy0

public class proxy0 implements com.alibaba.dubbo.rpc.service.EchoService, com.alibaba.dubbo.demo.DemoService {
    public static java.lang.reflect.Method[] methods;
    private java.lang.reflect.InvocationHandler handler;

    public proxy0(java.lang.reflect.InvocationHandler arg0) {
        handler = arg0;
    }

//我服务接口暴露的方法 @Override public java.lang.String sayHello(java.lang.String arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (String) arg0; Object ret = handler.invoke(this, methods[0], args); return (java.lang.String) ret; } //我服务接口暴露的方法 @Override public void save(vo.TestVo arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (vo.TestVo) arg0; Object ret = handler.invoke(this, methods[1], args); } /** * echo test. * * @param message message. * @return message. */ @Override public java.lang.Object $echo(java.lang.Object arg0) throws Throwable { Object[] args = new Object[1]; args[0] = (java.lang.Object) arg0; Object ret = handler.invoke(this, methods[2], args); return (java.lang.Object) ret; } }

<2>动态生成的Wrapper

/**
 * @author liqiang
 * @date 2020/3/17 10:56
 * @Description: (what)
 * (why)
 * (how)
 */
class Wrapper1 extends com.alibaba.dubbo.common.bytecode.Wrapper {
    public static String[] pns;
    public static java.util.Map pts;
    public static String[] mns;
    public static String[] dmns;
    public static Class[] mts0;
    public static Class[] mts1;


    @Override
    public String[] getPropertyNames() {
        return pns;
    }


    @Override
    public boolean hasProperty(String n) {
        return pts.containsKey(n);
    }

    @Override
    public Class getPropertyType(String n) {
        return (Class) pts.get(n);
    }


    @Override
    public String[] getMethodNames() {
        return mns;
    }


    @Override
    public String[] getDeclaredMethodNames() {
        return dmns;
    }

//    /**
//     * get method name array.
//     *
//     * @return method name array.
//     */
//    @Override
//    public String[] getMethodNames() {
//        return new String[0];
//    }
//
//    /**
//     * get method name array.
//     *
//     * @return method name array.
//     */
//    @Override
//    public String[] getDeclaredMethodNames() {
//        return new String[0];
//    }


    @Override
    public void setPropertyValue(Object o, String n, Object v) {
        provider.DemoServiceImpl w;
        try {
            w = ((provider.DemoServiceImpl) o);
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
        throw new com.alibaba.dubbo.common.bytecode.NoSuchPropertyException("Not found property \"" + n + "\" filed or setter method in class provider.DemoServiceImpl.");
    }

    @Override
    public Object getPropertyValue(Object o, String n) {
        provider.DemoServiceImpl w;
        try {
            w = ((provider.DemoServiceImpl) o);
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
        throw new com.alibaba.dubbo.common.bytecode.NoSuchPropertyException("Not found property \"" + n + "\" filed or setter method in class provider.DemoServiceImpl.");
    }

    @Override
    public Object invokeMethod(Object o, String n, Class[] p, Object[] v) throws java.lang.reflect.InvocationTargetException {
        
        //这个是暴露接口的实现类
        provider.DemoServiceImpl w;
        try {
            //复制
            w = ((provider.DemoServiceImpl) o);
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
        try {
            //以下是我暴露的2个方法
            if ("save".equals(n) && p.length == 1) {
                w.save((vo.TestVo) v[0]);
                return null;
            }
            if ("sayHello".equals(n) && p.length == 1) {
                return (String) w.sayHello((java.lang.String) v[0]);
            }
        } catch (Throwable e) {
            throw new java.lang.reflect.InvocationTargetException(e);
        }
        throw new com.alibaba.dubbo.common.bytecode.NoSuchMethodException("Not found method \"" + n + "\" in class provider.DemoServiceImpl.");
    }
}

 

标签:dubbo,return,String,Object,源码,ProxyFactory,Override,new,public
来源: https://www.cnblogs.com/LQBlog/p/12509644.html