Example usage for org.springframework.util MethodInvoker setTargetObject

List of usage examples for org.springframework.util MethodInvoker setTargetObject

Introduction

In this page you can find the example usage for org.springframework.util MethodInvoker setTargetObject.

Prototype

public void setTargetObject(@Nullable Object targetObject) 

Source Link

Document

Set the target object on which to call the target method.

Usage

From source file:org.springframework.cloud.function.deployer.FunctionExtractingFunctionCatalog.java

private Object invoke(Class<?> type, String method, Object arg) {
    for (String id : deployed) {
        Object catalog = deployer.getBean(id, type);
        if (catalog == null) {
            continue;
        }//from   w  w w .ja va2s .  c om
        try {
            MethodInvoker invoker = new MethodInvoker();
            invoker.setTargetObject(catalog);
            invoker.setTargetMethod(method);
            invoker.setArguments(new Object[] { arg });
            invoker.prepare();
            Object result = invoker.invoke();
            if (result != null) {
                return result;
            }
        } catch (Exception e) {
            throw new IllegalStateException("Cannot extract catalog", e);
        }
    }
    return null;
}

From source file:org.springframework.data.keyvalue.redis.listener.adapter.MessageListenerAdapter.java

/**
 * Invoke the specified listener method.
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @return the result returned from the listener method
 * @see #getListenerMethodName/*from   ww w. j a v a  2 s.com*/
 * @see #buildListenerArguments
 */
protected Object invokeListenerMethod(String methodName, Object[] arguments) {
    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(getDelegate());
        methodInvoker.setTargetMethod(methodName);
        methodInvoker.setArguments(arguments);
        methodInvoker.prepare();
        return methodInvoker.invoke();
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof DataAccessException) {
            throw (DataAccessException) targetEx;
        } else {
            throw new RedisListenerExecutionFailedException(
                    "Listener method '" + methodName + "' threw exception", targetEx);
        }
    } catch (Throwable ex) {
        throw new RedisListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
    }
}

From source file:org.springframework.jms.listener.adapter.MessageListenerAdapter.java

/**
 * Invoke the specified listener method.
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @return the result returned from the listener method
 * @throws JMSException if thrown by JMS API methods
 * @see #getListenerMethodName/*from  w w w .  j  a  v  a 2 s  . co  m*/
 * @see #buildListenerArguments
 */
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws JMSException {
    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(getDelegate());
        methodInvoker.setTargetMethod(methodName);
        methodInvoker.setArguments(arguments);
        methodInvoker.prepare();
        return methodInvoker.invoke();
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof JMSException) {
            throw (JMSException) targetEx;
        } else {
            throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception",
                    targetEx);
        }
    } catch (Throwable ex) {
        throw new ListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
    }
}

From source file:org.springframework.test.util.ReflectionTestUtils.java

/**
 * Invoke the method with the given {@code name} on the supplied target
 * object with the supplied arguments.//from  www .jav a  2s  .  c  om
 * <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");
    }
}