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) 

Source Link

Document

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

Usage

From source file:org.impalaframework.extension.mvc.annotation.WebAnnotationUtils.java

public static Annotation[] getAnnotations(MethodParameter methodParameter, String methodName) {
    try {/*from  w ww . j a  v  a  2s  . c o  m*/
        return (Annotation[]) ReflectionUtils
                .invokeMethod(methodParameter.getClass().getMethod(methodName, new Class[0]), methodParameter);
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException("No method found: " + methodName, e);
    }
}

From source file:org.jboss.spring.vfs.context.ContextClassUtil.java

public static boolean isJBossAS5orHigher() {
    try {/*from ww  w .  ja v  a  2  s. c  o  m*/
        Class jBossVersionClass = DelegatingContextLoaderListener.class.getClassLoader()
                .loadClass("org.jboss.Version");
        Object versionObject = ReflectionUtils
                .invokeMethod(ReflectionUtils.findMethod(jBossVersionClass, "getInstance"), null);
        Integer majorVersion = (Integer) ReflectionUtils
                .invokeMethod(ReflectionUtils.findMethod(jBossVersionClass, "getMajor"), versionObject);
        // For JBoss AS versions 5 and higher
        if (majorVersion >= 5) {
            return true;
        }
    } catch (ClassNotFoundException e) {
        // do nothing;
    }
    return false;
}

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

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

    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  v a  2s . c  o m*/
    return (T) ReflectionUtils.invokeMethod(method, target);
}

From source file:org.shept.persistence.provider.hibernate.HibernateUtils_old.java

public static Serializable getIdValue(HibernateDaoSupport dao, Object model) {
    String idStr = getIdentifierPropertyName(dao, model);
    // TODO use BeanWrapper instead ?
    Method idMth = ReflectionUtils.findMethod(model.getClass(), "get" + StringUtils.capitalize(idStr));
    Serializable idxObj = (Serializable) ReflectionUtils.invokeMethod(idMth, model);
    return idxObj;
}

From source file:by.creepid.docsreporter.converter.PdfConverterAdapterTest.java

public PdfConverterAdapterTest() {
    converter = new PoiPdfConverterAdapter();

    Method propSetMethod = ReflectionUtils.findMethod(PoiPdfConverterAdapter.class, "afterPropertiesSet");
    ReflectionUtils.invokeMethod(propSetMethod, converter);
}

From source file:am.ik.categolj2.infra.codelist.EnumCodeList.java

@Override
public Map<String, String> asMap() {
    Map<String, String> codeList = new LinkedHashMap<String, String>();
    Method method = ReflectionUtils.findMethod(enumClass, "values");

    Enum<?>[] result = (Enum<?>[]) ReflectionUtils.invokeMethod(method, enumClass);
    for (Enum<?> e : result) {
        CodeListItem item = (CodeListItem) e;
        codeList.put(item.getCodeValue(), item.getCodeLabel());
    }/*  w  w w  . j  a  v  a 2  s.co m*/

    return Collections.unmodifiableMap(codeList);
}

From source file:com.github.springtestdbunit.testutils.CallAfterTestMethodExecutionListener.java

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    Method method = testContext.getTestClass().getMethod("afterTest");
    ReflectionUtils.invokeMethod(method, testContext.getTestInstance());
}

From source file:spring.osgi.utils.OsgiBundleUtils.java

/**
 * Returns the underlying BundleContext for the given Bundle. This uses
 * reflection and highly dependent of the OSGi implementation. Should not be
 * used if OSGi 4.1 is being used.//from w  w w  .ja  va  2  s  . c  om
 *
 * @param bundle OSGi bundle
 * @return the bundle context for this bundle
 */
public static BundleContext getBundleContext(final Bundle bundle) {
    if (bundle == null)
        return null;

    // try Equinox getContext
    Method meth = ReflectionUtils.findMethod(bundle.getClass(), "getContext", new Class[0]);

    // fallback to getBundleContext (OSGi 4.1)
    if (meth == null)
        meth = ReflectionUtils.findMethod(bundle.getClass(), "getBundleContext", new Class[0]);

    final Method m = meth;

    if (meth != null) {
        ReflectionUtils.makeAccessible(meth);
        return (BundleContext) ReflectionUtils.invokeMethod(m, bundle);
    }

    // fallback to field inspection (KF and Prosyst)
    final BundleContext[] ctx = new BundleContext[1];

    ReflectionUtils.doWithFields(bundle.getClass(), new FieldCallback() {

        public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            ctx[0] = (BundleContext) field.get(bundle);
        }
    }, new FieldFilter() {

        public boolean matches(Field field) {
            return BundleContext.class.isAssignableFrom(field.getType());
        }
    });

    return ctx[0];
}

From source file:org.socialsignin.spring.data.dynamodb.repository.support.CompositeIdHashAndRangeKeyExtractor.java

@SuppressWarnings("unchecked")
@Override//w  w  w .  j a  va2 s .  c  o  m
public H getHashKey(ID id) {
    Method method = hashAndRangeKeyMethodExtractor.getHashKeyMethod();
    if (method != null) {
        return (H) ReflectionUtils.invokeMethod(method, id);
    } else {
        return (H) ReflectionUtils.getField(hashAndRangeKeyMethodExtractor.getHashKeyField(), id);
    }
}

From source file:edu.wisc.hrs.dao.HrsUtils.java

@SuppressWarnings("unchecked")
private static <T> T callMethodSafe(Object obj, String method, Object... args) {
    if (obj == null) {
        return null;
    }/*w ww .  j ava 2  s  . com*/

    final Class<? extends Object> cls = obj.getClass();

    if (args == null) {
        final Method valueMethod = ReflectionUtils.findMethod(cls, method);
        return (T) ReflectionUtils.invokeMethod(valueMethod, obj);
    }

    final Class<?>[] types = new Class<?>[args.length];
    for (int i = 0; i < args.length; i++) {
        final Object arg = args[i];
        if (arg == null) {
            types[i] = Object.class;
        } else {
            types[i] = arg.getClass();
        }
    }

    final Method valueMethod = ReflectionUtils.findMethod(cls, method, types);
    return (T) ReflectionUtils.invokeMethod(valueMethod, obj, args);
}