下面列出了org.springframework.util.MethodInvoker#setTargetClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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");
}
}
@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();
}
@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();
}