org.springframework.util.MethodInvoker#prepare ( )源码实例Demo

下面列出了org.springframework.util.MethodInvoker#prepare ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Invoke the method with the given {@code name} on the supplied target
 * object with the supplied arguments.
 * <p>This method traverses the class hierarchy in search of the desired
 * method. In addition, an attempt will be made to make non-{@code public}
 * methods <em>accessible</em>, thus allowing one to invoke {@code protected},
 * {@code private}, and <em>package-private</em> methods.
 * @param target the target object on which to invoke the specified method
 * @param name the name of the method to invoke
 * @param args the arguments to provide to the method
 * @return the invocation result, if any
 * @see MethodInvoker
 * @see ReflectionUtils#makeAccessible(Method)
 * @see ReflectionUtils#invokeMethod(Method, Object, Object[])
 * @see ReflectionUtils#handleReflectionException(Exception)
 */
@SuppressWarnings("unchecked")
@Nullable
public static <T> T invokeMethod(Object target, String name, Object... args) {
	Assert.notNull(target, "Target object must not be null");
	Assert.hasText(name, "Method name must not be empty");

	try {
		MethodInvoker methodInvoker = new MethodInvoker();
		methodInvoker.setTargetObject(target);
		methodInvoker.setTargetMethod(name);
		methodInvoker.setArguments(args);
		methodInvoker.prepare();

		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Invoking method '%s' on %s with arguments %s", name, safeToString(target),
					ObjectUtils.nullSafeToString(args)));
		}

		return (T) methodInvoker.invoke();
	}
	catch (Exception ex) {
		ReflectionUtils.handleReflectionException(ex);
		throw new IllegalStateException("Should never get here");
	}
}
 
/**
 * Invoke the method with the given {@code name} on the supplied target
 * object with the supplied arguments.
 * <p>This method traverses the class hierarchy in search of the desired
 * method. In addition, an attempt will be made to make non-{@code public}
 * methods <em>accessible</em>, thus allowing one to invoke {@code protected},
 * {@code private}, and <em>package-private</em> methods.
 * @param target the target object on which to invoke the specified method
 * @param name the name of the method to invoke
 * @param args the arguments to provide to the method
 * @return the invocation result, if any
 * @see MethodInvoker
 * @see ReflectionUtils#makeAccessible(Method)
 * @see ReflectionUtils#invokeMethod(Method, Object, Object[])
 * @see ReflectionUtils#handleReflectionException(Exception)
 */
@SuppressWarnings("unchecked")
@Nullable
public static <T> T invokeMethod(Object target, String name, Object... args) {
	Assert.notNull(target, "Target object must not be null");
	Assert.hasText(name, "Method name must not be empty");

	try {
		MethodInvoker methodInvoker = new MethodInvoker();
		methodInvoker.setTargetObject(target);
		methodInvoker.setTargetMethod(name);
		methodInvoker.setArguments(args);
		methodInvoker.prepare();

		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Invoking method '%s' on %s with arguments %s", name, safeToString(target),
					ObjectUtils.nullSafeToString(args)));
		}

		return (T) methodInvoker.invoke();
	}
	catch (Exception ex) {
		ReflectionUtils.handleReflectionException(ex);
		throw new IllegalStateException("Should never get here");
	}
}
 
@SuppressWarnings("unchecked")
public static <T> T invokeMethod(Object targetObject, String name, Object... args) {
    Assert.notNull(targetObject, "Target object must not be null");
    Assert.hasText(name, "Method name must not be empty");

    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(targetObject);
        methodInvoker.setTargetMethod(name);
        methodInvoker.setArguments(args);
        methodInvoker.prepare();
        return (T) methodInvoker.invoke();
    } catch (Exception ex) {
        org.springframework.util.ReflectionUtils.handleReflectionException(ex);
        throw new IllegalStateException("Should never get here");
    }
}
 
@SuppressWarnings("unchecked")
public static <T> T invokeStaticMethod(Class<?> targetClass, String name, Object... args) {
    Assert.notNull(targetClass, "Target class must not be null");
    Assert.hasText(name, "Method name must not be empty");

    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetClass(targetClass);
        methodInvoker.setTargetMethod(name);
        methodInvoker.setArguments(args);
        methodInvoker.prepare();
        return (T) methodInvoker.invoke();
    } catch (Exception ex) {
        org.springframework.util.ReflectionUtils.handleReflectionException(ex);
        throw new IllegalStateException("Should never get here");
    }
}
 
/**
 * Invoke the method with the given {@code name} on the supplied target
 * object with the supplied arguments.
 * <p>This method traverses the class hierarchy in search of the desired
 * method. In addition, an attempt will be made to make non-{@code public}
 * methods <em>accessible</em>, thus allowing one to invoke {@code protected},
 * {@code private}, and <em>package-private</em> methods.
 * @param target the target object on which to invoke the specified method
 * @param name the name of the method to invoke
 * @param args the arguments to provide to the method
 * @return the invocation result, if any
 * @see MethodInvoker
 * @see ReflectionUtils#makeAccessible(Method)
 * @see ReflectionUtils#invokeMethod(Method, Object, Object[])
 * @see ReflectionUtils#handleReflectionException(Exception)
 */
@SuppressWarnings("unchecked")
public static <T> T invokeMethod(Object target, String name, Object... args) {
	Assert.notNull(target, "Target object must not be null");
	Assert.hasText(name, "Method name must not be empty");

	try {
		MethodInvoker methodInvoker = new MethodInvoker();
		methodInvoker.setTargetObject(target);
		methodInvoker.setTargetMethod(name);
		methodInvoker.setArguments(args);
		methodInvoker.prepare();

		if (logger.isDebugEnabled()) {
			logger.debug("Invoking method '" + name + "' on target [" + target + "] with arguments [" +
					ObjectUtils.nullSafeToString(args) + "]");
		}

		return (T) methodInvoker.invoke();
	}
	catch (Exception ex) {
		ReflectionUtils.handleReflectionException(ex);
		throw new IllegalStateException("Should never get here");
	}
}
 
@Test
public void testInvokeWithNullArgument() throws Exception {
	MethodInvoker methodInvoker = new MethodInvoker();
	methodInvoker.setTargetClass(TestClass1.class);
	methodInvoker.setTargetMethod("nullArgument");
	methodInvoker.setArguments(new Object[] {null});
	methodInvoker.prepare();
	methodInvoker.invoke();
}
 
@Test
public void testInvokeWithNullArgument() throws Exception {
	MethodInvoker methodInvoker = new MethodInvoker();
	methodInvoker.setTargetClass(TestClass1.class);
	methodInvoker.setTargetMethod("nullArgument");
	methodInvoker.setArguments(new Object[] {null});
	methodInvoker.prepare();
	methodInvoker.invoke();
}
 
源代码8 项目: jim-framework   文件: CacheSupportImpl.java
private Object invoke(CachedInvocation invocation)
		throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	final MethodInvoker invoker = new MethodInvoker();
	invoker.setTargetObject(invocation.getTargetBean());
	invoker.setArguments(invocation.getArguments());
	invoker.setTargetMethod(invocation.getTargetMethod().getName());
	invoker.prepare();
	return invoker.invoke();
}
 
@Test
public void testInvokeWithNullArgument() throws Exception {
	MethodInvoker methodInvoker = new MethodInvoker();
	methodInvoker.setTargetClass(TestClass1.class);
	methodInvoker.setTargetMethod("nullArgument");
	methodInvoker.setArguments(new Object[] {null});
	methodInvoker.prepare();
	methodInvoker.invoke();
}
 
/**
 * Creates a MethodInvoker instance from the cached invocation object and
 * invokes it to get the return value
 * 
 * @param invocation
 * @return Return value resulted from the method invocation
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private Object execute(CachedInvocation invocation)
		throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	final MethodInvoker invoker = new MethodInvoker();
	invoker.setTargetObject(invocation.getTargetBean());
	invoker.setArguments(invocation.getArguments());
	invoker.setTargetMethod(invocation.getTargetMethod().getName());
	invoker.prepare();
	return invoker.invoke();
}