Example usage for org.springframework.util ReflectionUtils findMethod

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

Introduction

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

Prototype

@Nullable
public static Method findMethod(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Method on the supplied class with the supplied name and no parameters.

Usage

From source file:com.px100systems.data.core.CompoundIndexDescriptor.java

public CompoundIndexDescriptor(Class<?> entityClass, String name, String[] fields) {
    if (name.trim().isEmpty())
        throw new RuntimeException("Empty compound index name in " + entityClass.getSimpleName());

    this.name = NAME_PREFIX + entityClass.getSimpleName() + "_" + name + "_idx";

    for (String field : fields) {
        String[] s = field.split(" ");
        if (s.length != 2)
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": couldn't parse field '" + field + "'");

        Method getter = ReflectionUtils.findMethod(entityClass, PropertyAccessor.methodName("get", s[0]));
        if (getter == null)
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": couldn't find getter for '" + field + "'");
        Class<?> returnType = getter.getReturnType();
        if (!returnType.equals(Integer.class) && !returnType.equals(Long.class)
                && !returnType.equals(Double.class) && !returnType.equals(Date.class)
                && !returnType.equals(String.class) && !returnType.equals(Boolean.class))
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": field '" + field
                    + "' - only Integer, Long, Double, Date, String, and Boolean are supported");

        this.fields.add(new Field(s[0], returnType, s[1].equalsIgnoreCase("DESC")));
    }//from w  w  w  .j  a v  a 2 s  .c om
}

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.
 *///www .  ja v  a  2  s .  com
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:io.neba.core.selftests.SelftestReference.java

public void execute() {
    Object bean = getBean();//from ww w.j  a  v  a  2s .  c  o m
    Method method = ReflectionUtils.findMethod(bean.getClass(), this.methodName);
    ReflectionUtils.invokeMethod(method, bean);
}

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.consol.citrus.admin.converter.AbstractObjectConverter.java

/**
 * Adds new endpoint property./* w  w  w.ja  va  2 s . 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:com.iisigroup.cap.model.GenericBean.java

/**
 * ???//from w w w.j a  v  a 2 s  .  c om
 * 
 * @param fieldId
 *            ???
 * @return Object
 */
public Object get(String fieldId) {
    if (CapString.isEmpty(fieldId)) {
        throw new CapException("field [" + fieldId + "] is empty!!", getClass());
    }
    try {
        String field = fieldId;
        int index = fieldId.indexOf(".");
        if (index > 0) {
            field = fieldId.substring(0, index);
            Object keyClazz = get(field);
            if (keyClazz instanceof GenericBean) {
                return ((GenericBean) keyClazz).get(fieldId.substring(index + 1));
            }
        } else {
            String getter = new StringBuffer("get").append(String.valueOf(field.charAt(0)).toUpperCase())
                    .append(field.substring(1)).toString();
            Method method = ReflectionUtils.findMethod(getClass(), getter);
            if (method == null) {
                getter = "is" + getter.substring(3);
                method = ReflectionUtils.findMethod(getClass(), getter);
            }
            if (method != null) {
                return method.invoke(this);
            } else {
                Field f = ReflectionUtils.findField(getClass(), field);
                if (f != null) {
                    f.setAccessible(true);
                    return f.get(this);
                }
            }
        }
        throw new CapException(new StringBuffer("field:").append(field).append(" is not exist!!").toString(),
                getClass());

    } catch (Exception e) {
        throw new CapException(e, getClass());
    }

}

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());
    }//  w  ww .j  a  v a  2  s .  c  om
}

From source file:com.agileapes.couteau.context.spring.event.impl.GenericTranslationScheme.java

@Override
public void fillIn(Event originalEvent, ApplicationEvent translated) throws EventTranslationException {
    if (!(translated instanceof GenericApplicationEvent)) {
        return;//from ww  w.j a va  2 s  .c o m
    }
    GenericApplicationEvent applicationEvent = (GenericApplicationEvent) translated;
    final Enumeration<?> propertyNames = applicationEvent.getPropertyNames();
    while (propertyNames.hasMoreElements()) {
        final String property = (String) propertyNames.nextElement();
        final Object value = applicationEvent.getProperty(property);
        final Method method = ReflectionUtils.findMethod(originalEvent.getClass(),
                "set" + StringUtils.capitalize(property));
        if (method == null || !Modifier.isPublic(method.getModifiers())
                || method.getParameterTypes().length != 1 || !method.getReturnType().equals(void.class)
                || !method.getParameterTypes()[0].isInstance(value)) {
            continue;
        }
        try {
            method.invoke(originalEvent, value);
        } catch (Exception e) {
            throw new EventTranslationException("Failed to call setter on original event", e);
        }
    }
}

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

/**
 * ?/*from w ww  . j  av a2  s .  c  om*/
 *
 * @param targetClass  class
 * @param method ??
 * @param params ?
 *
 * @return 
 */
public Method findService(Class<?> targetClass, String method, Object... params) {

    if (ArrayUtils.isNotEmpty(params)) {

        Class<?>[] paramTypes = convertParamToClass(params);

        return findParamMethod(targetClass, method, paramTypes);

    } else {
        return ReflectionUtils.findMethod(targetClass, method);
    }
}