org.springframework.cglib.reflect.FastMethod Java Examples

The following examples show how to use org.springframework.cglib.reflect.FastMethod. 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: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@SneakyThrows
private static <T> T safeAs(Map<String, Object> map, T bean) {
	List<FastMethod> methods = setters(bean.getClass());
	for (FastMethod fastMethod : methods) {
		String field = StrUtil.lowerFirst(fastMethod.getName().substring(3));
		Object value = map.get(field);
		if (value == null) {
			continue;
		}
		if (PrimitiveUtil.isBaseType(value.getClass())) {
			value = PrimitiveUtil.cast(value, fastMethod.getParameterTypes()[0]);
			fastMethod.invoke(bean, new Object[] { value });
		} else {
			fastMethod.invoke(bean, new Object[] { value });
		}
	}
	return bean;
}
 
Example #2
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 #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: 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 #5
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 #6
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
/**
 * 获取所有非空的setters
 * @author Frodez
 * @date 2019-05-22
 */
@SneakyThrows
public static List<FastMethod> getDefaultNotNullSetters(Class<?> klass) {
	Assert.notNull(klass, "klass must not be null");
	return Collections.unmodifiableList(defaultNotNullSetters(ReflectUtil.instance(klass)));
}
 
Example #7
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 #8
Source File: HippoServerHandler.java    From hippo with Apache License 2.0 4 votes vote down vote up
/**
 * apiProcess 不可能有2个Dto的接口,但是可能有多个基础类型 test(User user,Address add)//不会有这种情况,有也不支持 test(String
 * userName,String pwd)//会有
 *
 * @param paras
 * @return
 * @throws Exception
 */
private Object apiProcess(HippoRequest paras) throws Exception {


    Method _method = HippoServiceCache.INSTANCE.getApiMethodMap().get(paras.getClassName() + "-" + paras.getMethodName());
    Class<?> aClass = HippoServiceCache.INSTANCE.getInterfaceMap().get(paras.getClassName());
    if (_method == null) {
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.getName().equals(paras.getMethodName())) {
                continue;
            }
            _method = method;
            HippoServiceCache.INSTANCE.getApiMethodMap().put(paras.getClassName() + "-" + paras.getMethodName(), method);
            break;
        }
    }
    if (_method == null) {
        throw new NoSuchMethodException(paras.getClassName() + ":" + paras.getMethodName());
    }
    Object[] paramDto;

    Class<?>[] parameterTypes = _method.getParameterTypes();

    FastMethod serviceFastMethod = HippoServiceCache.INSTANCE.getImplClassMap().get(aClass.getName()).getMethod(_method.getName(), parameterTypes);
    String[] parameterNames = new LocalVariableTableParameterNameDiscoverer().getParameterNames(serviceFastMethod.getJavaMethod());
    Object[] objects = paras.getParameters();
    // 无参数
    if (parameterTypes.length == 0 || objects == null) {
        paramDto = null;
    }
    // 一个参数(是否是Dto)
    else if (parameterTypes.length == 1) {
        Class<?> parameterType = parameterTypes[0];
        paramDto = new Object[1];
        // 非自定义dto就是java原生类了
        if (CommonUtils.isJavaClass(parameterType)) {
            paramDto[0] = covert(getMap(objects).get(parameterNames[0]), parameterType);
        } else {
            paramDto[0] = FastJsonConvertUtils.jsonToJavaObject((String) objects[0], parameterType);
        }
    }
    // 多参
    else {
        paramDto = new Object[parameterNames.length];
        int index = 0;
        Map<String, Object> map = getMap(objects);
        for (String parameter : parameterNames) {
            paramDto[index] = covert(map.get(parameter), parameterTypes[index]);
            index++;
        }
    }
    // 拿到返回
    return FastJsonConvertUtils.cleanseToObject(serviceFastMethod.invoke(HippoServiceCache.INSTANCE.getImplObjectMap().get(aClass.getName()), paramDto));
}
 
Example #9
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 3 votes vote down vote up
/**
 * 清空bean的默认值<br>
 * 如果只需要一个全新的无默认值的对象,建议使用BeanUtil.clearInstance方法,<br>
 * 原因是本方法会对所有字段执行其setter方法(无法确保只有存在默认值的字段拥有值),开销更大。<br>
 * @see BeanUtil#clearInstance(Class)
 * @author Frodez
 * @param <T>
 * @throws InvocationTargetException
 * @date 2019-02-08
 */
@SneakyThrows
public static void clear(Object bean) {
	Assert.notNull(bean, "bean must not be null");
	List<FastMethod> methods = setters(bean.getClass());
	int length = methods.size();
	for (int i = 0; i < length; i++) {
		methods.get(i).invoke(bean, ReflectUtil.EMPTY_ARRAY);
	}
}
 
Example #10
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 3 votes vote down vote up
/**
 * 获取无默认值的bean。<br>
 * 推荐使用本方法,比新建一个对象然后使用BeanUtil.clear更快,<br>
 * 原因是本方法只会执行存在默认值的字段的setter方法,方法执行的开销减小。<br>
 * @see BeanUtil#clear(Object)
 * @author Frodez
 * @param <T>
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @date 2019-02-08
 */
@SneakyThrows
public static <T> T clearInstance(Class<T> klass) {
	T bean = ReflectUtil.instance(klass);
	List<FastMethod> methods = defaultNotNullSetters(bean);
	int length = methods.size();
	for (int i = 0; i < length; i++) {
		methods.get(i).invoke(bean, ReflectUtil.EMPTY_ARRAY);
	}
	return bean;
}
 
Example #11
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 2 votes vote down vote up
/**
 * 获取所有setters
 * @author Frodez
 * @date 2019-05-22
 */
public static List<FastMethod> getSetters(Class<?> klass) {
	Assert.notNull(klass, "klass must not be null");
	return Collections.unmodifiableList(setters(klass));
}