org.springframework.cglib.reflect.FastClass Java Examples

The following examples show how to use org.springframework.cglib.reflect.FastClass. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RpcServerInvoker.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
@Override
public RpcResponse invoke(RpcInvocation invocation) {
    String className = invocation.getClassName();
    Object serviceBean = handlerMap.get(className);

    Class<?> serviceClass = serviceBean.getClass();
    String methodName = invocation.getMethodName();
    Class<?>[] parameterTypes = invocation.getParameterTypes();
    Object[] parameters = invocation.getParameters();

    FastClass serviceFastClass = FastClass.create(serviceClass);
    FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);
    try {
        Object result= serviceFastMethod.invoke(serviceBean, parameters);
        RpcResponse rpcResponse=new RpcResponse();
        rpcResponse.setResult(result);
        rpcResponse.setRequestId(invocation.getRequestId());
        return rpcResponse;
    } catch (InvocationTargetException e) {
        throw new RpcException(e);
    }
}
 
Example #2
Source File: MethodProxy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static FastClass helper(CreateInfo ci, Class type) {
	FastClass.Generator g = new FastClass.Generator();
	g.setType(type);
	// SPRING PATCH BEGIN
	g.setContextClass(type);
	// SPRING PATCH END
	g.setClassLoader(ci.c2.getClassLoader());
	g.setNamingPolicy(ci.namingPolicy);
	g.setStrategy(ci.strategy);
	g.setAttemptLoad(ci.attemptLoad);
	return g.create();
}
 
Example #3
Source File: NetComServerFactory.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
public static RpcResponse invokeService(RpcRequest request, Object serviceBean) {
	if (serviceBean==null) {
		serviceBean = serviceMap.get(request.getClassName());
	}
	if (serviceBean == null) {
		// TODO
	}

	RpcResponse response = new RpcResponse();

	if (System.currentTimeMillis() - request.getCreateMillisTime() > 180000) {
		response.setResult(new ReturnT<String>(ReturnT.FAIL_CODE, "The timestamp difference between admin and executor exceeds the limit."));
		return response;
	}
	if (accessToken!=null && accessToken.trim().length()>0 && !accessToken.trim().equals(request.getAccessToken())) {
		response.setResult(new ReturnT<String>(ReturnT.FAIL_CODE, "The access token[" + request.getAccessToken() + "] is wrong."));
		return response;
	}

	try {
		Class<?> serviceClass = serviceBean.getClass();
		String methodName = request.getMethodName();
		Class<?>[] parameterTypes = request.getParameterTypes();
		Object[] parameters = request.getParameters();

		FastClass serviceFastClass = FastClass.create(serviceClass);
		FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);

		Object result = serviceFastMethod.invoke(serviceBean, parameters);

		response.setResult(result);
	} catch (Throwable t) {
		t.printStackTrace();
		response.setError(t.getMessage());
	}

	return response;
}
 
Example #4
Source File: RpcServerHandler.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
     * @param rpcRequest
     * @descripiton: 处理请求
     * @author: kinson
     * @date: 2019/8/13 22:45
     * @exception:
     * @modifier:
     * @return:java.lang.Object
     */
    private Object handle(RpcRequest rpcRequest) throws Exception {

        String className = rpcRequest.getClassName();
        Object serviceBean = handleMap.get(className);

        Class<?> serviceClass = serviceBean.getClass();
        String methodName = rpcRequest.getMethodName();
        Object[] parameters = rpcRequest.getParameters();
        Class<?>[] parameterTypes = rpcRequest.getParameterTypes();

        logger.debug("Service name is {}, and methodName is {}", serviceClass.getName(), methodName);
        for (Class<?> c : parameterTypes) {
            logger.debug(c.getName());
        }

        for (Object parameter : parameters) {
            logger.debug(parameter.toString());
        }

//        Method method = serviceClass.getMethod(methodName, parameterTypes);
//        method.setAccessible(Boolean.TRUE);
//        return method.invoke(serviceBean, parameters);

        FastClass serviceFastClass = FastClass.create(serviceClass);
        int methodIndex = serviceFastClass.getIndex(methodName, parameterTypes);
        return serviceFastClass.invoke(methodIndex, serviceBean, parameters);
    }
 
Example #5
Source File: MethodProxy.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static FastClass helper(CreateInfo ci, Class type) {
	FastClass.Generator g = new FastClass.Generator();
	g.setType(type);
	// SPRING PATCH BEGIN
	g.setContextClass(type);
	// SPRING PATCH END
	g.setClassLoader(ci.c2.getClassLoader());
	g.setNamingPolicy(ci.namingPolicy);
	g.setStrategy(ci.strategy);
	g.setAttemptLoad(ci.attemptLoad);
	return g.create();
}
 
Example #6
Source File: NetComServerFactory.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
public static RpcResponse invokeService(RpcRequest request, Object serviceBean) {
	if (serviceBean==null) {
		serviceBean = serviceMap.get(request.getClassName());
	}
	if (serviceBean == null) {
		// TODO
	}

	RpcResponse response = new RpcResponse();

	if (System.currentTimeMillis() - request.getCreateMillisTime() > 180000) {
		response.setResult(new ReturnT<String>(ReturnT.FAIL_CODE, "The timestamp difference between admin and executor exceeds the limit."));
		return response;
	}
	if (accessToken!=null && accessToken.trim().length()>0 && !accessToken.trim().equals(request.getAccessToken())) {
		response.setResult(new ReturnT<String>(ReturnT.FAIL_CODE, "The access token[" + request.getAccessToken() + "] is wrong."));
		return response;
	}

	try {
		Class<?> serviceClass = serviceBean.getClass();
		String methodName = request.getMethodName();
		Class<?>[] parameterTypes = request.getParameterTypes();
		Object[] parameters = request.getParameters();

		FastClass serviceFastClass = FastClass.create(serviceClass);
		FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);

		Object result = serviceFastMethod.invoke(serviceBean, parameters);

		response.setResult(result);
	} catch (Throwable t) {
		t.printStackTrace();
		response.setError(t.getMessage());
	}

	return response;
}
 
Example #7
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private static List<FastMethod> setters(Class<?> klass) {
	List<FastMethod> methods = SETTER_CACHE.get(klass);
	if (methods == null) {
		methods = new ArrayList<>();
		FastClass fastClass = FastClass.create(klass);
		for (Method method : klass.getMethods()) {
			if (isSetter(method)) {
				methods.add(fastClass.getMethod(method));
			}
		}
		SETTER_CACHE.put(klass, methods);
	}
	return methods;
}
 
Example #8
Source File: ReflectUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 获取FastClass,并初始化所有FastMethod。类型会被缓存。
 * @author Frodez
 * @date 2019-04-12
 */
public static FastClass fastClass(Class<?> klass, boolean initialized) {
	Assert.notNull(klass, "klass must not be null");
	Table table = CGLIB_CACHE.get(klass);
	if (table == null) {
		table = new Table(klass);
		if (initialized) {
			table.init();
		}
		CGLIB_CACHE.put(klass, table);
		return table.fastClass;
	}
	return table.fastClass;
}
 
Example #9
Source File: ReflectUtil.java    From koper with Apache License 2.0 5 votes vote down vote up
public static void invoke(Object targetObject, Method method, Object[] objects) {
    final Class<?> clazz = targetObject.getClass();
    final FastClass fastClass = FastClass.create(clazz);
    try {
        fastClass.getMethod(method).invoke(targetObject, objects);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: MethodProxy.java    From spring-analysis-note with MIT License 4 votes vote down vote up
FastClass getFastClass() {
	init();
	return fastClassInfo.f1;
}
 
Example #11
Source File: MethodProxy.java    From spring-analysis-note with MIT License 4 votes vote down vote up
FastClass getSuperFastClass() {
	init();
	return fastClassInfo.f2;
}
 
Example #12
Source File: MethodProxy.java    From java-technology-stack with MIT License 4 votes vote down vote up
FastClass getFastClass() {
	init();
	return fastClassInfo.f1;
}
 
Example #13
Source File: MethodProxy.java    From java-technology-stack with MIT License 4 votes vote down vote up
FastClass getSuperFastClass() {
	init();
	return fastClassInfo.f2;
}
 
Example #14
Source File: ReflectUtil.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
public Table(Class<?> klass) {
	this.fastClass = FastClass.create(klass);
	methods = new FastMethod[fastClass.getMaxIndex() + 1];
}
 
Example #15
Source File: HippoServerInit.java    From hippo with Apache License 2.0 4 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext ctx) {
    //init fixed theadcount
    HippoServerThreadPool.FIXED.setThreadCount(threadCount);

    Map<String, Object> serviceBeanMap = ctx.getBeansWithAnnotation(HippoServiceImpl.class);

    if (MapUtils.isEmpty(serviceBeanMap)) {
        throw new HippoServiceException(
                "该项目依赖了hippo-server,在接口实现类请使用[@HippoServiceImpl]来声明");
    }

    Map<String, Object> implObjectMap = HippoServiceCache.INSTANCE.getImplObjectMap();
    Map<String, FastClass> implClassMap = HippoServiceCache.INSTANCE.getImplClassMap();
    Map<String, Class<?>> interfaceMap = HippoServiceCache.INSTANCE.getInterfaceMap();
    for (Object serviceBean : serviceBeanMap.values()) {
        String simpleName = null;
        Class<?> clazz = AopUtils.isAopProxy(serviceBean) ? AopUtils.getTargetClass(serviceBean) : serviceBean.getClass();
        Class<?>[] interfaces = clazz.getInterfaces();
        int index = 0;
        for (Class<?> class1 : interfaces) {
            //兼容@HippoService方式
            HippoService annotation = class1.getAnnotation(HippoService.class);
            if (annotation == null && CommonUtils.isJavaClass(class1)) {
                continue;
            }
            if (index == 1) {
                throw new HippoServiceException(
                        serviceBean.getClass().getName() + "已经实现了[" + simpleName + "]接口,hippoServiceImpl不允许实现2个接口。");
            }
            simpleName = class1.getSimpleName();
            index++;
            // simpleName 提供apiProcess使用
            // 全限定名提供给rpcProcess使用
            String name = class1.getName();
            //提供给apigate访问的方式是接口名+方法名,所以会导致apigate访问过来找到2个实现类导致异常
            if (implObjectMap.containsKey(simpleName)) {
                throw new HippoServiceException(
                        "接口[" + simpleName + "]已存在。[" + name + "],hippo不支持不同包名但接口名相同,请重命名当前接口名");
            }
            implObjectMap.put(name, serviceBean);
            interfaceMap.put(simpleName, class1);
            implClassMap.put(name, FastClass.create(clazz));
            if (annotation != null) {
                registryNames.add(annotation.serviceName());
            } else {
                metaMap.put(name, serviceName);
            }
        }
    }
}
 
Example #16
Source File: ReflectUtil.java    From BlogManagePlatform with Apache License 2.0 2 votes vote down vote up
/**
 * 获取FastClass,类型会被缓存
 * @author Frodez
 * @date 2019-04-12
 */
public static FastClass fastClass(Class<?> klass) {
	return fastClass(klass, false);
}