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:com.emergya.utils.DataSourceUtils.java

/**
 * Genera un nuevo data source con las propiedades
 * {@link DataSourceUtils#dataSourceProperties}
 * //from  w w w .  j  ava2  s.c  o  m
 * @return {@link BasicDataSource}
 */
public DataSource getDataSource() {
    DataSource dataSource = new BasicDataSource();

    // Se escriben las propiedades por reflexion
    for (Object key : dataSourceProperties.keySet()) {
        if (key instanceof String) {
            try {
                String property = (String) key;
                String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
                Method setterMethod = ReflectionUtils.findMethod(BasicDataSource.class, setter, String.class);
                ReflectionUtils.makeAccessible(setterMethod);
                ReflectionUtils.invokeMethod(setterMethod, dataSource,
                        dataSourceProperties.getProperty(property));
            } catch (Exception e) {
                // Error al poner la propiedad
            }
        }
    }

    return dataSource;
}

From source file:com.consol.citrus.admin.converter.endpoint.AbstractEndpointConverter.java

@Override
public S convertBack(EndpointModel definition) {
    try {/*  w w  w  . j  ava  2 s.c o m*/
        S instance = getSourceModelClass().newInstance();

        ReflectionUtils.invokeMethod(findSetter(getSourceModelClass(), "id"), instance, definition.getId());

        for (Property property : definition.getProperties()) {
            if (StringUtils.hasText(property.getValue())) {
                Method setter = findSetter(getSourceModelClass(), property.getFieldName());
                ReflectionUtils.invokeMethod(setter, instance, getMethodArgument(setter, property.getValue()));
            }
        }

        return instance;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ApplicationRuntimeException("Failed to instantiate model class", e);
    }
}

From source file:grails.plugin.searchable.internal.compass.config.mapping.CompassAnnotationSearchableGrailsDomainClassMappingConfigurator.java

/**
 * Does this strategy handle the given domain class (and it's respective mapping type)
 *
 * @param grailsDomainClass the Grails domain class
 * @return true if the mapping of the class can be handled by this strategy
 *//*  ww w .j a  v a2  s  . c o  m*/
@Override
public boolean isMappedBy(GrailsDomainClass grailsDomainClass) {
    if (!annotationsAvailable) {
        return false;
    }
    Class clazz = grailsDomainClass.getClazz();
    Method getter = findGetAnnotationMethod();
    if (getter == null) {
        return false;
    }
    return ReflectionUtils.invokeMethod(getter, clazz, getSearchableAnnotationClass()) != null;
}

From source file:org.eclipse.gemini.blueprint.test.internal.holder.ReflectionOsgiHolder.java

public void addTestError(Throwable testProblem) {
    ReflectionUtils.invokeMethod(ADD_TEST_ERROR, instance, new Object[] { testProblem });
}

From source file:org.codehaus.groovy.grails.plugins.searchable.compass.config.mapping.CompassAnnotationSearchableGrailsDomainClassMappingConfigurator.java

/**
 * Does this strategy handle the given domain class (and it's respective mapping type)
 *
 * @param grailsDomainClass the Grails domain class
 * @return true if the mapping of the class can be handled by this strategy
 *///w  w  w  . ja v a 2  s. co  m
public boolean isMappedBy(GrailsDomainClass grailsDomainClass) {
    if (!annotationsAvailable) {
        return false;
    }
    Class clazz = grailsDomainClass.getClazz();
    Method getter = findGetAnnotationMethod();
    if (getter == null) {
        return false;
    }
    return ReflectionUtils.invokeMethod(getter, clazz, new Class[] { getSearchableAnnotationClass() }) != null;
}

From source file:org.eclipse.gemini.blueprint.test.internal.holder.ReflectionOsgiHolder.java

public void addTestFailure(Throwable testProblem) {
    ReflectionUtils.invokeMethod(ADD_TEST_FAILURE, instance, new Object[] { testProblem });
}

From source file:com.dianping.dpsf.invoke.filter.MockInvokeFilter.java

private DPSFResponse createMockResponse(InvocationInvokeContext invocationContext, MockMeta mockMeta)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    String mockClassName = mockMeta.getMockClass();
    Class mockClass = mockClassMap.get(mockClassName);
    if (mockClass == null) {
        mockClass = Class.forName(mockClassName);
        mockClassMap.put(mockClassName, mockClass);
    }//w  w  w.j  a  v  a  2s  . c om
    Method invokeMethod = invocationContext.getMethod();
    if (mockClass != null && mockMeta.getMockMethods() != null
            && mockMeta.getMockMethods().contains(invokeMethod.getName())) {
        Object result = ReflectionUtils.invokeMethod(invokeMethod, mockClass.newInstance(),
                invocationContext.getArguments());
        DPSFResponse dpsfResponse = new DefaultResponse(Constants.MESSAGE_TYPE_SERVICE_MOCK,
                invocationContext.getRequest().getSerializ());
        dpsfResponse.setReturn(result);
        return dpsfResponse;
    } else {
        return new DefaultResponse(invocationContext.getRequest().getSerializ(),
                invocationContext.getRequest().getSequence(), Constants.MESSAGE_TYPE_EXCEPTION,
                new DPSFException(
                        "mock class or method config error.please check 'mockConfig'.mockConfig=" + mockMeta));
    }
}

From source file:com.github.dactiv.fear.service.RemoteApiCaller.java

/**
 * ?/*from   w w  w .  j  a  v a 2s  .  c om*/
 *
 * @param serviceName ???
 * @param methodName  ??
 * @param params      ?
 *
 * @return 
 */
@Override
public ApiResult invoke(String serviceName, String methodName, Object... params) {

    SimpleApiResult simpleApiResult = new SimpleApiResult(new Date());

    try {
        LOGGER.info("? " + serviceName + " ?");
        Object bean = applicationContext.getBean(serviceName);

        Method method = getService(bean.getClass(), methodName, params);

        if (method == null) {
            method = findService(bean.getClass(), methodName, params);
        }

        // ??
        if (method == null) {
            return new ErrorApiResult(simpleApiResult.getInvokeDate(),
                    "?" + serviceName + "?" + methodName + "", ApiResult.NOT_FOUND,
                    new Date());
        }
        LOGGER.info(" " + serviceName + " ? " + method.getName() + " ");
        // 
        Object data = ReflectionUtils.invokeMethod(method, bean, params);
        // ? null, ? json 
        if (data != null) {
            simpleApiResult.setData(data);
        }
        // ?
        simpleApiResult.setCompleteDate(new Date());
        LOGGER.info("? " + bean.getClass().getName() + "." + method.getName() + " ");
        return simpleApiResult;
    } catch (Exception e) {
        LOGGER.error(" api ", e);
        return new ErrorApiResult(simpleApiResult.getInvokeDate(), e, ApiResult.ERROR, new Date());
    }
}

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

@Override
public void callHandler(MotechEvent event) {
    Map<String, Object> params = event.getParameters();
    List<Object> args = new ArrayList<Object>();
    int i = 0;/*  w w w .ja  va2 s.  c o m*/
    for (Class<?> t : method.getParameterTypes()) {
        Object param = params.get(Integer.toString(i));
        if (param != null && t.isAssignableFrom(param.getClass())) {
            args.add(param);
            i++;
        } else if (params.containsKey(Integer.toString(i)) && !t.isPrimitive() && param == null) {
            args.add(param);
            i++;
        } else {
            logger.warn(String.format(
                    "Method: %s parameter: #%d of type: %s is not available in the event: %s. Handler skiped...",
                    method.toGenericString(), i, t.getName(), event));
            return;
        }
    }
    ReflectionUtils.invokeMethod(method, bean, args.toArray());
}