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:com.example.config.StartupApplicationListener.java

private Set<Class<?>> sources(ApplicationReadyEvent event) {
    Method method = ReflectionUtils.findMethod(SpringApplication.class, "getAllSources");
    if (method == null) {
        method = ReflectionUtils.findMethod(SpringApplication.class, "getSources");
    }/*  w w w . j a  va2 s  .c  o m*/
    ReflectionUtils.makeAccessible(method);
    @SuppressWarnings("unchecked")
    Set<Object> objects = (Set<Object>) ReflectionUtils.invokeMethod(method, event.getSpringApplication());
    Set<Class<?>> result = new LinkedHashSet<>();
    for (Object object : objects) {
        if (object instanceof String) {
            object = ClassUtils.resolveClassName((String) object, null);
        }
        result.add((Class<?>) object);
    }
    return result;
}

From source file:com.ace.erp.common.inject.support.InjectBaseDependencyHelper.java

/**
 * ??//from ww  w  . j a v  a 2  s. c om
 *
 * @param target
 * @param annotation
 */
private static Set<Object> findDependencies(final Object target, final Class<? extends Annotation> annotation) {

    final Set<Object> candidates = Sets.newHashSet();

    ReflectionUtils.doWithFields(target.getClass(), new ReflectionUtils.FieldCallback() {
        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            Object obj = ReflectionUtils.getField(field, target);
            candidates.add(obj);
        }
    }, new ReflectionUtils.FieldFilter() {
        @Override
        public boolean matches(Field field) {
            return field.isAnnotationPresent(annotation);
        }
    });

    ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(method);
            PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
            candidates.add(ReflectionUtils.invokeMethod(descriptor.getReadMethod(), target));
        }
    }, new ReflectionUtils.MethodFilter() {
        @Override
        public boolean matches(Method method) {
            boolean hasAnnotation = false;
            hasAnnotation = method.isAnnotationPresent(annotation);
            if (!hasAnnotation) {
                return false;
            }

            boolean hasReadMethod = false;
            PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
            hasReadMethod = descriptor != null && descriptor.getReadMethod() != null;

            if (!hasReadMethod) {
                return false;
            }

            return true;
        }
    });

    return candidates;
}

From source file:io.neba.core.selftests.SelftestReference.java

public void execute() {
    Object bean = getBean();/*  w  w w.  ja  v  a 2 s.c  om*/
    Method method = ReflectionUtils.findMethod(bean.getClass(), this.methodName);
    ReflectionUtils.invokeMethod(method, bean);
}

From source file:com.baidu.cc.spring.ConfigCenterPropertyExtractor.java

/**
 * To initialize properties import action from extractor to target configuration
 * center server. importer and extractor should not be null.
 * @throws Exception to throw out all exception to spring container.
 *///from w  w  w  . jav  a2 s  .  c o m
public void afterPropertiesSet() throws Exception {
    Assert.notNull(importer, "'importer property is null.'");
    Assert.notNull(extractor, "'extractor property is null.'");

    //get configuration items
    Method m = ReflectionUtils.findMethod(PropertyPlaceholderConfigurer.class, "mergeProperties");
    if (m != null) {
        m.setAccessible(true);
        Properties properties = (Properties) ReflectionUtils.invokeMethod(m, extractor);
        if (properties == null) {
            LOGGER.warn("Target extractor get a null property, import will ignore.");
            return;
        }

        Map copied = new HashMap(properties);
        importer.getConfigLoader().importConfigItems(ccVersion, copied);
    }
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

private String invokeMethod(DataSource dataSource, String methodName) {
    Method method = ReflectionUtils.findMethod(dataSource.getClass(), methodName);
    return (String) ReflectionUtils.invokeMethod(method, dataSource);
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

private DataSource getTargetDataSource(DataSource dataSource) {
    Method method = ReflectionUtils.findMethod(dataSource.getClass(), "getTargetDataSource");
    return (DataSource) ReflectionUtils.invokeMethod(method, dataSource);
}

From source file:com.asual.summer.core.util.ClassUtils.java

public static Object invokeMethod(Object target, final String methodName, final Object[] parameters) {
    if (target != null) {
        final List<Method> matches = new ArrayList<Method>();
        ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                matches.add(method);/*  w  w w. j  ava2  s .  c  o  m*/
            }
        }, new ReflectionUtils.MethodFilter() {
            public boolean matches(Method method) {
                if (method.getName().equals(methodName)) {
                    Class<?>[] types = method.getParameterTypes();
                    if (parameters == null && types.length == 0) {
                        return true;
                    }
                    if (types.length != parameters.length) {
                        return false;
                    }
                    for (int i = 0; i < types.length; i++) {
                        if (!types[i].isInstance(parameters[i])) {
                            return false;
                        }
                    }
                    return true;
                }
                return false;
            }
        });
        if (matches.size() > 0) {
            if (parameters == null) {
                return ReflectionUtils.invokeMethod(matches.get(0), target);
            } else {
                return ReflectionUtils.invokeMethod(matches.get(0), target, parameters);
            }
        }
    }
    return null;
}

From source file:com.ryantenney.metrics.spring.GaugeAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
    final Class<?> targetClass = AopUtils.getTargetClass(bean);

    ReflectionUtils.doWithFields(targetClass, new FieldCallback() {
        @Override//w w  w.j a  v  a  2 s  . c  om
        public void doWith(final Field field) throws IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            final Gauge annotation = field.getAnnotation(Gauge.class);
            final String metricName = Util.forGauge(targetClass, field, annotation);

            metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
                @Override
                public Object getValue() {
                    Object value = ReflectionUtils.getField(field, bean);
                    if (value instanceof com.codahale.metrics.Gauge) {
                        value = ((com.codahale.metrics.Gauge<?>) value).getValue();
                    }
                    return value;
                }
            });

            LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(),
                    field.getName());
        }
    }, FILTER);

    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override
        public void doWith(final Method method) throws IllegalAccessException {
            if (method.getParameterTypes().length > 0) {
                throw new IllegalStateException(
                        "Method " + method.getName() + " is annotated with @Gauge but requires parameters.");
            }

            final Gauge annotation = method.getAnnotation(Gauge.class);
            final String metricName = Util.forGauge(targetClass, method, annotation);

            metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
                @Override
                public Object getValue() {
                    return ReflectionUtils.invokeMethod(method, bean);
                }
            });

            LOG.debug("Created gauge {} for method {}.{}", metricName, targetClass.getCanonicalName(),
                    method.getName());
        }
    }, FILTER);

    return bean;
}

From source file:com.consol.citrus.admin.converter.AbstractObjectConverter.java

/**
 * Adds new endpoint property.//from   w  w  w  .jav a  2s .c  o m
 * @param fieldName
 * @param displayName
 * @param definition
 * @param defaultValue
 * @param required
 */
protected Property property(String fieldName, String displayName, S definition, String defaultValue,
        boolean required) {
    Field field = ReflectionUtils.findField(definition.getClass(), fieldName);

    if (field != null) {
        Method getter = ReflectionUtils.findMethod(definition.getClass(), getMethodName(fieldName));

        String value = defaultValue;
        if (getter != null) {
            Object getterResult = ReflectionUtils.invokeMethod(getter, definition);
            if (getterResult != null) {
                value = getterResult.toString();
            }
        }

        if (value != null) {
            if (field.isAnnotationPresent(XmlAttribute.class)) {
                return new Property(field.getAnnotation(XmlAttribute.class).name(), fieldName, displayName,
                        resolvePropertyExpression(value), required);
            } else {
                return new Property(fieldName, fieldName, displayName, resolvePropertyExpression(value),
                        required);
            }
        } else {
            return new Property(fieldName, fieldName, displayName, null, required);
        }
    } else {
        log.warn(String.format("Unknown field '%s' on source type '%s'", fieldName, definition.getClass()));
        return null;
    }
}

From source file:org.cloudfoundry.identity.uaa.codestore.ExpiringCodeStoreTests.java

@After
public void cleanUp() throws Exception {
    Method m = ReflectionUtils.findMethod(jdbcTemplate.getDataSource().getClass(), "close");
    if (m != null) {
        ReflectionUtils.invokeMethod(m, jdbcTemplate.getDataSource());
    }//from w w w . jav a  2s . c  o m
}