Java Code Examples for net.sf.cglib.reflect.FastMethod#invoke()

The following examples show how to use net.sf.cglib.reflect.FastMethod#invoke() . 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: ProxyInvocation.java    From bitchat with Apache License 2.0 6 votes vote down vote up
/**
 * 执行方法的调用
 *
 * @param controller 控制器
 * @param method     方法
 * @param methodName 方法名
 * @return 渲染结果
 * @throws Exception 异常
 */
Object invoke(Object controller, Method method, String methodName) throws Exception {
    if (method == null) {
        throw new NoSuchMethodException("Can not find specified method: " + methodName);
    }

    Class<?> clazz = controller.getClass();
    Class<?>[] parameterTypes = method.getParameterTypes();
    Object[] parameters = null;
    Object result;
    try {
        parameters = getParameters(method, parameterTypes);
        // 使用 CGLib 执行反射调用
        FastClass fastClass = FastClass.create(clazz);
        FastMethod fastMethod = fastClass.getMethod(methodName, parameterTypes);
        // 调用,并得到调用结果
        result = fastMethod.invoke(controller, parameters);

    } catch (InvocationTargetException e) {
        String msg = "调用出错,请求类[" + controller.getClass().getName() + "],方法名[" + method.getName() + "],参数[" + Arrays.toString(parameters) + "]";
        throw getInvokeException(msg, e);
    }
    return result;
}
 
Example 2
Source File: ProxyInvocation.java    From redant with Apache License 2.0 6 votes vote down vote up
/**
 * 执行方法的调用
 * @param controller 控制器
 * @param method 方法
 * @param methodName 方法名
 * @return 渲染结果
 * @throws Exception 异常
 */
      Object invoke(Object controller, Method method, String methodName) throws Exception {
	if (method == null) {
		throw new NoSuchMethodException("Can not find specified method: " + methodName);
	}

	Class<?> clazz = controller.getClass();
	Class<?>[] parameterTypes = method.getParameterTypes();
	Object[] parameters = null;
	Object result;
	try {
		parameters = getParameters(method,parameterTypes);
		// 使用 CGLib 执行反射调用
		FastClass fastClass = FastClass.create(clazz);
		FastMethod fastMethod = fastClass.getMethod(methodName, parameterTypes);
		// 调用,并得到调用结果
		result = fastMethod.invoke(controller, parameters);

	} catch(InvocationTargetException e){
		String msg = "调用出错,请求类["+controller.getClass().getName()+"],方法名[" + method.getName() + "],参数[" + Arrays.toString(parameters)+"]";
		throw getInvokeException(msg, e);
	}
	return result;
}
 
Example 3
Source File: ReflectPerformanceTest.java    From JavaBase with MIT License 6 votes vote down vote up
@Test
public void testCglib() throws Exception {
  long sum = 0;
  TargetObject targetObject = new TargetObject();

  FastClass testClazz = FastClass.create(TargetObject.class);
  FastMethod method = testClazz.getMethod("setNum", new Class[]{int.class});

  Object[] param = new Object[1];
  time.startCount();
  for (int i = 0; i < LOOP_SIZE; ++i) {
    param[0] = i;
    method.invoke(targetObject, param);
    sum += targetObject.getNum();
  }

  time.endCountOneLine("use cglib ");
  log.info("LOOP_SIZE {} 和是 {} ", LOOP_SIZE, sum);
}
 
Example 4
Source File: RpcHandler.java    From rpc4j with MIT License 6 votes vote down vote up
private Object handle(RpcRequest request) throws Throwable {
	String className = request.getClassName();
	Object serviceBean = handlerMap.get(className);

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

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

	FastClass serviceFastClass = FastClass.create(serviceClass);
	FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);
	return serviceFastMethod.invoke(serviceBean, parameters);
}
 
Example 5
Source File: GenericVisitor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private boolean dispatch(Dispatcher<BaseType> dispatcher, BaseType instance) {
  FastMethod method = getMethod(dispatcher, instance);
  if (method == null) {
    return false;
  }
  try {
    method.invoke(this, new Object[]{ instance });
    return true;
  } catch (InvocationTargetException e) {
    throw Throwables.propagate(e.getCause());
  }
}
 
Example 6
Source File: GenericVisitor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private boolean dispatchBefore(Dispatcher<BaseType> dispatcher, BaseType instance) {
  FastMethod method = getMethod(dispatcher, instance);
  if (method == null) {
    return true; // non-presence of before method means continue execution
  }
  try {
    Object result = method.invoke(this, new Object[]{ instance });
    if (method.getReturnType().equals(Boolean.TYPE)) {
      return Boolean.class.cast(result); // method determines whether to continue
    }
    return true;
  } catch (InvocationTargetException e) {
    throw Throwables.propagate(e.getCause());
  }
}