Example usage for org.springframework.util MethodInvoker MethodInvoker

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

Introduction

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

Prototype

MethodInvoker

Source Link

Usage

From source file:org.springframework.amqp.rabbit.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 Exception if thrown by Rabbit API methods
 * @see #getListenerMethodName/*from  w  w w  .  j  a v a2 s.  com*/
 * @see #buildListenerArguments
 */
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws Exception {
    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 IOException) {
            throw new AmqpIOException((IOException) targetEx);
        } else {
            throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception",
                    targetEx);
        }
    } catch (Throwable ex) {
        ArrayList<String> arrayClass = new ArrayList<String>();
        if (arguments != null) {
            for (int i = 0; i < arguments.length; i++) {
                arrayClass.add(arguments[i].getClass().toString());
            }
        }
        throw new ListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with argument type = [" + StringUtils.collectionToCommaDelimitedString(arrayClass)
                + "], value = [" + ObjectUtils.nullSafeToString(arguments) + "]", ex);
    }
}

From source file:org.springframework.batch.item.data.RepositoryItemReader.java

private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) {
    MethodInvoker invoker = new MethodInvoker();
    invoker.setTargetObject(targetObject);
    invoker.setTargetMethod(targetMethod);
    return invoker;
}

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;
        }//w w w .ja  va  2s . c  o  m
        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// w  w  w  . j  a  v a2  s . co  m
 * @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/*  w  w w .  j  a  v  a  2 s.  c  o 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./* ww  w  . j a  v a 2 s  .c  o  m*/
 * <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");
    }
}