org.springframework.util.MethodInvoker Java Examples

The following examples show how to use org.springframework.util.MethodInvoker. 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: QuartzSchedulerBeanTargetEditor.java    From light-task-scheduler with Apache License 2.0 7 votes vote down vote up
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;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: ConstructorResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #8
Source File: ConstructorResolver.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: ConstructorResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #10
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 #11
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 #12
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 #13
Source File: ConstructorResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
}
 
Example #14
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 #15
Source File: ConstructorResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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);
}
 
Example #16
Source File: MethodInvokingJobDetailFactoryBean.java    From uncode-schedule with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
	this.methodInvoker = methodInvoker;
}
 
Example #17
Source File: MethodInvokingJobDetailFactoryBean.java    From uncode-schedule with Apache License 2.0 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
  this.methodInvoker = methodInvoker;
}
 
Example #18
Source File: MethodInvokeJobExecution.java    From light-task-scheduler with Apache License 2.0 4 votes vote down vote up
public MethodInvokeJobExecution(MethodInvoker methodInvoker) {
    this.methodInvoker = methodInvoker;
}
 
Example #19
Source File: MethodInvokingJobDetailFactoryBean.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
	this.methodInvoker = methodInvoker;
}
 
Example #20
Source File: MethodInvokingJobDetailFactoryBean.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
	this.methodInvoker = methodInvoker;
}
 
Example #21
Source File: MethodInvokingJobDetailFactoryBean.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
	this.methodInvoker = methodInvoker;
}
 
Example #22
Source File: MethodInvokingJobDetailFactoryBean.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Set the MethodInvoker to use.
 */
public void setMethodInvoker(MethodInvoker methodInvoker) {
	this.methodInvoker = methodInvoker;
}
 
Example #23
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();
}
 
Example #24
Source File: JobMethodInvocationFailedException.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #25
Source File: JobMethodInvocationFailedException.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #26
Source File: JobMethodInvocationFailedException.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example #27
Source File: JobMethodInvocationFailedException.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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);
}