Java Code Examples for org.springframework.util.MethodInvoker#setArguments()

The following examples show how to use org.springframework.util.MethodInvoker#setArguments() . 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: ReflectionTestUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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");
	}
}
 
Example 2
Source File: ReflectionTestUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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");
	}
}
 
Example 3
Source File: ReflectionUtils.java    From embedded-database-spring-test with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example 4
Source File: ReflectionUtils.java    From embedded-database-spring-test with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example 5
Source File: ReflectionTestUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
	}
}
 
Example 6
Source File: MethodInvokingFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}
 
Example 7
Source File: MethodInvokingFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example 8
Source File: CacheSupportImpl.java    From jim-framework with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: MethodInvokingFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 10
Source File: CacheSupportImpl.java    From spring-cache-self-refresh with MIT License 3 votes vote down vote up
/**
 * 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();
}