Example usage for org.springframework.util ReflectionUtils invokeMethod

List of usage examples for org.springframework.util ReflectionUtils invokeMethod

Introduction

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

Prototype

@Nullable
public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) 

Source Link

Document

Invoke the specified Method against the supplied target object with the supplied arguments.

Usage

From source file:org.cleverbus.core.common.contextcall.ReflectionCallUtils.java

/**
 * Invokes target method.//from   w ww  .  j a v a2  s  .c o  m
 *
 * @param params the parameters of the call
 * @param beanFactory the Spring bean factory
 * @return response
 */
public static Object invokeMethod(ContextCallParams params, BeanFactory beanFactory) {
    // find target service
    Object targetService = beanFactory.getBean(params.getTargetType());

    // determine method's argument types
    List<Class> argTypes = new ArrayList<Class>();
    for (Object arg : params.getMethodArgs()) {
        argTypes.add(arg.getClass());
    }

    // exist method?
    Method method = ReflectionUtils.findMethod(params.getTargetType(), params.getMethodName(),
            argTypes.toArray(new Class[] {}));
    if (method == null) {
        throw new IllegalStateException("there is no method '" + params.getMethodName() + "' on target type '"
                + params.getTargetType().getSimpleName() + "'");
    }

    // invoke method
    return ReflectionUtils.invokeMethod(method, targetService, params.getMethodArgs().toArray());
}

From source file:com.create.mybatis.repository.query.MyBatisQuery.java

@Override
public Object execute(final Object[] parameters) {
    final Class<?> domainClass = queryMethod.getEntityInformation().getJavaType();
    final Object mapper = mapperProvider.getMapper(domainClass);
    return ReflectionUtils.invokeMethod(queryMethod.getMethod(), mapper, parameters);
}

From source file:org.grails.datastore.gorm.support.BeforeValidateHelper.java

public void invokeBeforeValidate(final Object target, final List<?> validatedFieldsList) {
    Class<?> domainClass = target.getClass();
    Method method = null;//from   w  w w.ja  v  a  2s .  c o m
    if (validatedFieldsList == null) {
        // prefer the no-arg version of beforeValidate() if validatedFieldsList
        // is null...
        method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE);
        if (method == null) {
            method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE, List.class);
        }
    } else {
        // prefer the list-arg version of beforeValidate() if
        // validatedFieldsList is not null...
        method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE, List.class);
        if (method == null) {
            method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE);
        }
    }
    if (method != null) {
        ReflectionUtils.makeAccessible(method);
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
            ReflectionUtils.invokeMethod(method, target, validatedFieldsList);
        } else {
            ReflectionUtils.invokeMethod(method, target);
        }
    }
}

From source file:nh.examples.springintegration.order.framework.domain.commands.CommandHandler.java

/**
 * Dispatches the given {@link Command} to a specialized <tt>handle</tt>
 * -Method on this class//ww  w . j  ava2s .c om
 * 
 * <p>
 * If no execute-method for the specified command can be found an Exception
 * will be raised.
 * 
 * @param command
 *            the command to be executed
 */
public final void handle(Command command) {
    checkNotNull(command);

    // Get handle method ready for invocation
    Method handleMethod = ReflectionHelper.getAccessibleMethod("handle", getClass(), command);

    // Invoke handle method
    ReflectionUtils.invokeMethod(handleMethod, this, command);
}

From source file:org.shredzone.cilla.core.event.EventInvoker.java

/**
 * Invoke the event handler.//ww  w . j a  va 2  s  . c  o  m
 *
 * @param event
 *            {@link Event} that was fired
 */
public void invoke(Event<?> event) {
    Class<?>[] types = method.getParameterTypes();
    Object[] values = new Object[types.length];

    for (int ix = 0; ix < types.length; ix++) {
        values[ix] = evaluateParameter(types[ix], event);
    }

    ReflectionUtils.invokeMethod(method, bean, values);
}

From source file:com.expedia.seiso.domain.repo.adapter.SimpleItemRepoAdapter.java

@Override
public Item find(@NonNull ItemKey key) {
    val itemClass = key.getItemClass();
    val findByKeyMethod = getFindByKeyMethod(itemClass);
    val repo = (CrudRepository) repositories.getRepositoryFor(itemClass);
    val simpleItemKey = (SimpleItemKey) key;
    return (Item) ReflectionUtils.invokeMethod(findByKeyMethod, repo, simpleItemKey.getValue());
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java

@SuppressWarnings("unchecked")
public static <T> T callMethod(String name, Object target, Object[] args, Class<?>[] argsTypes)
        throws Exception {
    Class<?> clazz = target.getClass();
    Method method = ReflectionUtils.findMethod(clazz, name, argsTypes);

    if (method == null)
        throw new IllegalArgumentException(
                "Cannot find method '" + method + "' in the class hierarchy of " + target.getClass());
    method.setAccessible(true);//from   w w w  .  j  a  va 2 s.c om
    return (T) ReflectionUtils.invokeMethod(method, target, args);
}

From source file:org.pentaho.proxy.creators.userdetailsservice.ProxyUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    try {//from   ww w. j a  va 2  s . c om
        Object result = ReflectionUtils.invokeMethod(loadUserByNameMethod, sourceObject,
                new Object[] { username });
        if (result != null) {
            return new ProxyUserDetails(result);
        } else {
            logger.warn(
                    "Got a null from calling the method loadUserByUsername( String username ) of UserDetailsService: "
                            + sourceObject
                            + ". This is an interface violation beacuse it is specified that loadUserByUsername method should never return null. Throwing a UsernameNotFoundException.");
        }
    } catch (Exception e) {
        if (e.getClass().getName().equals(FULL_NAME_SS4_USERNOTFOUNDEXCEPTION)) {
            throw new UsernameNotFoundException(username + " not found", e);
        } else {
            logger.error(e.getMessage(), e);
        }
    }
    throw new UsernameNotFoundException(username);
}

From source file:org.apache.zeppelin.lens.LensSimpleExecutionStrategy.java

private Object invoke(ParseResult parseResult) {
    try {// ww  w.j  av a 2  s.  com
        return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(),
                parseResult.getArguments());
    } catch (Throwable th) {
        logger.severe("Command failed " + th);
        return handleThrowable(th);
    }
}

From source file:org.motechproject.server.event.annotations.MotechListenerEventProxy.java

@Override
public void callHandler(MotechEvent event) {
    ReflectionUtils.invokeMethod(method, bean, event);
}