下面列出了怎么用org.springframework.util.MethodInvoker的API类实例代码及写法,或者点击链接到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");
}
}
private QuartzJobContext buildQuartzJobContext(QuartzJobContext quartzJobContext, Trigger trigger) {
JobDataMap triggerJobDataMap = trigger.getJobDataMap();
JobDetail jobDetail = (JobDetail) triggerJobDataMap.get("jobDetail");
// 要执行的类
MethodInvoker methodInvoker = (MethodInvoker) jobDetail.getJobDataMap().get("methodInvoker");
Map<String, Object> jobDataMap = new HashMap<String, Object>();
jobDataMap.putAll(triggerJobDataMap);
jobDataMap.putAll(jobDetail.getJobDataMap());
jobDataMap.remove("jobDetail");
jobDataMap.remove("methodInvoker");
quartzJobContext.setJobDataMap(jobDataMap);
if (methodInvoker != null) {
quartzJobContext.setJobExecution(new MethodInvokeJobExecution(methodInvoker));
} else {
Class<? extends Job> jobClass = jobDetail.getJobClass();
try {
Job job = jobClass.newInstance();
quartzJobContext.setJobExecution(new JobDetailJobExecution(job));
} catch (Exception e) {
throw new QuartzProxyException("Instance JobClass[" + (jobClass == null ? null : jobClass.getName()) + "] error", e);
}
}
return quartzJobContext;
}
public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// If valid arguments found, determine type difference weight.
// Try type difference weight on both the converted arguments and
// the raw arguments. If the raw weight is better, use it.
// Decrease raw weight by 1024 to prefer it over equal converted weight.
int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
return Math.min(rawTypeDiffWeight, typeDiffWeight);
}
@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();
}
public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// If valid arguments found, determine type difference weight.
// Try type difference weight on both the converted arguments and
// the raw arguments. If the raw weight is better, use it.
// Decrease raw weight by 1024 to prefer it over equal converted weight.
int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight);
}
@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();
}
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();
}
public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// If valid arguments found, determine type difference weight.
// Try type difference weight on both the converted arguments and
// the raw arguments. If the raw weight is better, use it.
// Decrease raw weight by 1024 to prefer it over equal converted weight.
int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight);
}
public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// If valid arguments found, determine type difference weight.
// Try type difference weight on both the converted arguments and
// the raw arguments. If the raw weight is better, use it.
// Decrease raw weight by 1024 to prefer it over equal converted weight.
int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight);
}
public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// If valid arguments found, determine type difference weight.
// Try type difference weight on both the converted arguments and
// the raw arguments. If the raw weight is better, use it.
// Decrease raw weight by 1024 to prefer it over equal converted weight.
int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight);
}
@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();
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
public MethodInvokeJobExecution(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
}
/**
* 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();
}
/**
* Constructor for JobMethodInvocationFailedException.
* @param methodInvoker the MethodInvoker used for reflective invocation
* @param cause the root cause (as thrown from the target method)
*/
public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Throwable cause) {
super("Invocation of method '" + methodInvoker.getTargetMethod() +
"' on target class [" + methodInvoker.getTargetClass() + "] failed", cause);
}
/**
* Constructor for JobMethodInvocationFailedException.
* @param methodInvoker the MethodInvoker used for reflective invocation
* @param cause the root cause (as thrown from the target method)
*/
public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Throwable cause) {
super("Invocation of method '" + methodInvoker.getTargetMethod() +
"' on target class [" + methodInvoker.getTargetClass() + "] failed", cause);
}
/**
* Constructor for JobMethodInvocationFailedException.
* @param methodInvoker the MethodInvoker used for reflective invocation
* @param cause the root cause (as thrown from the target method)
*/
public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Throwable cause) {
super("Invocation of method '" + methodInvoker.getTargetMethod() +
"' on target class [" + methodInvoker.getTargetClass() + "] failed", cause);
}
/**
* Constructor for JobMethodInvocationFailedException.
* @param methodInvoker the MethodInvoker used for reflective invocation
* @param cause the root cause (as thrown from the target method)
*/
public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Throwable cause) {
super("Invocation of method '" + methodInvoker.getTargetMethod() +
"' on target class [" + methodInvoker.getTargetClass() + "] failed", cause);
}