Search in sources :

Example 1 with InitializeInvokerException

use of cn.moyada.dubbo.faker.core.exception.InitializeInvokerException in project dubbo-faker by moyada.

the class MethodInvokeHandler method fetchHandleInfo.

@Override
public MethodHandle fetchHandleInfo(String className, String methodName, String returnType, Class<?>[] paramClass) {
    Class<?> classType, returnClassType;
    try {
        classType = ReflectUtil.getClassType(className);
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("接口类型不存在: " + returnType);
    }
    try {
        returnClassType = ReflectUtil.getClassType(returnType);
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("结果类型不存在: " + returnType);
    }
    MethodHandle methodHandle;
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    try {
        // 创建方法信息
        MethodType methodType = MethodType.methodType(returnClassType, paramClass);
        // 查询方法返回方法具柄
        methodHandle = lookup.findVirtual(classType, methodName, methodType);
    } catch (NoSuchMethodException e) {
        throw new InitializeInvokerException("方法不存在: " + methodName);
    } catch (IllegalAccessException e) {
        throw new InitializeInvokerException("方法具柄获取失败");
    }
    return methodHandle;
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) MethodType(java.lang.invoke.MethodType) InitializeInvokerException(cn.moyada.dubbo.faker.core.exception.InitializeInvokerException) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with InitializeInvokerException

use of cn.moyada.dubbo.faker.core.exception.InitializeInvokerException in project dubbo-faker by moyada.

the class MethodHandleProxy method getProxy.

public MethodProxy getProxy(MethodInvokeDO invokeInfo) {
    // , int poolSize) {
    MethodProxy proxy;
    log.info("init method proxy info.");
    // 检测是否已存在
    Integer id = invokeInfo.getId();
    SoftReference<MethodProxy> ref = proxyMap.get(id);
    if (null != ref) {
        proxy = ref.get();
        if (null != proxy) {
            // && proxy.getService().length == poolSize) {
            return proxy;
        }
    }
    // 获取参数类型
    Class<?>[] paramTypes;
    String[] argsType = invokeInfo.getParamType().split(",");
    int length = argsType.length;
    if (0 == length) {
        paramTypes = new Class[0];
    } else {
        paramTypes = new Class[length];
        for (int index = 0; index < length; index++) {
            try {
                paramTypes[index] = ReflectUtil.getClassType(argsType[index]);
            } catch (ClassNotFoundException e) {
                throw new InitializeInvokerException("获取参数类型失败: " + argsType[index]);
            }
        }
    }
    // 获取方法具柄
    MethodHandle methodHandle = handle.fetchHandleInfo(invokeInfo.getClassName(), invokeInfo.getMethodName(), invokeInfo.getReturnType(), paramTypes);
    // 获取接口
    Class classType;
    try {
        classType = ReflectUtil.getClassType(invokeInfo.getClassName());
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("获取结果失败: " + invokeInfo.getClassName());
    }
    // 获取接口实例
    Object serviceAssembly;
    // Object[] serviceAssembly = new Object[poolSize];
    try {
        serviceAssembly = beanHolder.getBean(classType);
    // for (int index = 0; index < poolSize; index++) {
    // serviceAssembly[index] = beanHelper.getBean(classType);
    // }
    } catch (BeansException e) {
        throw new RpcException("获取接口实例失败: " + invokeInfo.getClassName() + ".", e);
    }
    // for (Object service : serviceAssembly) {
    try {
        methodHandle.invoke(serviceAssembly, null);
    } catch (Throwable throwable) {
    }
    // }
    proxy = new MethodProxy();
    proxy.setParamTypes(paramTypes);
    proxy.setMethodHandle(methodHandle);
    proxy.setService(serviceAssembly);
    // 缓存调用代理
    ref = new SoftReference<>(proxy);
    proxyMap.put(id, ref);
    return proxy;
}
Also used : InitializeInvokerException(cn.moyada.dubbo.faker.core.exception.InitializeInvokerException) RpcException(com.alibaba.dubbo.rpc.RpcException) MethodProxy(cn.moyada.dubbo.faker.core.model.MethodProxy) MethodHandle(java.lang.invoke.MethodHandle) BeansException(org.springframework.beans.BeansException)

Aggregations

InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)2 MethodHandle (java.lang.invoke.MethodHandle)2 MethodProxy (cn.moyada.dubbo.faker.core.model.MethodProxy)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 MethodHandles (java.lang.invoke.MethodHandles)1 MethodType (java.lang.invoke.MethodType)1 BeansException (org.springframework.beans.BeansException)1