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.springframework.instrument.classloading.ReflectiveLoadTimeWeaver.java

@Override
public void addTransformer(ClassFileTransformer transformer) {
    Assert.notNull(transformer, "Transformer must not be null");
    ReflectionUtils.invokeMethod(this.addTransformerMethod, this.classLoader, transformer);
}

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

/**
 * Convert the given result object (from attribute access or operation invocation)
 * to the specified target class for returning from the proxy method.
 * @param result the result object as returned by the {@code MBeanServer}
 * @param parameter the method parameter of the proxy method that's been invoked
 * @return the converted result object, or the passed-in object if no conversion
 * is necessary/*from w w w.  j  a  v a2s .co m*/
 */
@Nullable
protected Object convertResultValueIfNecessary(@Nullable Object result, MethodParameter parameter) {
    Class<?> targetClass = parameter.getParameterType();
    try {
        if (result == null) {
            return null;
        }
        if (ClassUtils.isAssignableValue(targetClass, result)) {
            return result;
        }
        if (result instanceof CompositeData) {
            Method fromMethod = targetClass.getMethod("from", CompositeData.class);
            return ReflectionUtils.invokeMethod(fromMethod, null, result);
        } else if (result instanceof CompositeData[]) {
            CompositeData[] array = (CompositeData[]) result;
            if (targetClass.isArray()) {
                return convertDataArrayToTargetArray(array, targetClass);
            } else if (Collection.class.isAssignableFrom(targetClass)) {
                Class<?> elementType = ResolvableType.forMethodParameter(parameter).asCollection()
                        .resolveGeneric();
                if (elementType != null) {
                    return convertDataArrayToTargetCollection(array, targetClass, elementType);
                }
            }
        } else if (result instanceof TabularData) {
            Method fromMethod = targetClass.getMethod("from", TabularData.class);
            return ReflectionUtils.invokeMethod(fromMethod, null, result);
        } else if (result instanceof TabularData[]) {
            TabularData[] array = (TabularData[]) result;
            if (targetClass.isArray()) {
                return convertDataArrayToTargetArray(array, targetClass);
            } else if (Collection.class.isAssignableFrom(targetClass)) {
                Class<?> elementType = ResolvableType.forMethodParameter(parameter).asCollection()
                        .resolveGeneric();
                if (elementType != null) {
                    return convertDataArrayToTargetCollection(array, targetClass, elementType);
                }
            }
        }
        throw new InvocationFailureException(
                "Incompatible result value [" + result + "] for target type [" + targetClass.getName() + "]");
    } catch (NoSuchMethodException ex) {
        throw new InvocationFailureException(
                "Could not obtain 'from(CompositeData)' / 'from(TabularData)' method on target type ["
                        + targetClass.getName() + "] for conversion of MXBean data structure [" + result + "]");
    }
}

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

private Object convertDataArrayToTargetArray(Object[] array, Class<?> targetClass)
        throws NoSuchMethodException {
    Class<?> targetType = targetClass.getComponentType();
    Method fromMethod = targetType.getMethod("from", array.getClass().getComponentType());
    Object resultArray = Array.newInstance(targetType, array.length);
    for (int i = 0; i < array.length; i++) {
        Array.set(resultArray, i, ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
    }//  w  w  w . j ava  2  s  .  c  om
    return resultArray;
}

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType,
        Class<?> elementType) throws NoSuchMethodException {

    Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
    Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
    for (int i = 0; i < array.length; i++) {
        resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
    }/* w w  w  . j  a va2 s . c  o  m*/
    return resultColl;
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override//www .  j a v a2  s  .  c o  m
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> find(final String queryString, @Nullable final Object... values) throws DataAccessException {
    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = queryObject(
                ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
        prepareQuery(queryObject);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                queryObject.setParameter(i, values[i]);
            }
        }
        return queryObject.list();
    }));
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override//from  ww w .  j  a  v a2 s . c  o  m
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> findByNamedParam(final String queryString, final String[] paramNames, final Object[] values)
        throws DataAccessException {

    if (paramNames.length != values.length) {
        throw new IllegalArgumentException("Length of paramNames array must match length of values array");
    }
    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = queryObject(
                ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
        prepareQuery(queryObject);
        for (int i = 0; i < values.length; i++) {
            applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
        }
        return queryObject.list();
    }));
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override//from w ww .ja  va 2 s  . c o m
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> findByValueBean(final String queryString, final Object valueBean) throws DataAccessException {

    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = queryObject(
                ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
        prepareQuery(queryObject);
        queryObject.setProperties(valueBean);
        return queryObject.list();
    }));
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override/*  ww w  .  ja va 2  s  . c o m*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> findByNamedQuery(final String queryName, @Nullable final Object... values)
        throws DataAccessException {
    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = queryObject(
                ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
        prepareQuery(queryObject);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                queryObject.setParameter(i, values[i]);
            }
        }
        return queryObject.list();
    }));
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override/* ww  w  .jav  a  2 s  .  c o m*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> findByNamedQueryAndNamedParam(final String queryName, @Nullable final String[] paramNames,
        @Nullable final Object[] values) throws DataAccessException {

    if (values != null && (paramNames == null || paramNames.length != values.length)) {
        throw new IllegalArgumentException("Length of paramNames array must match length of values array");
    }
    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = (org.hibernate.Query) nonNull(
                ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
        prepareQuery(queryObject);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
            }
        }
        return queryObject.list();
    }));
}

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

@Deprecated
@Override/*  w w  w  .ja v  a 2 s.co m*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public List<?> findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
        throws DataAccessException {

    return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
        org.hibernate.Query queryObject = queryObject(
                ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
        prepareQuery(queryObject);
        queryObject.setProperties(valueBean);
        return queryObject.list();
    }));
}