Example usage for java.lang.reflect Method getParameterTypes

List of usage examples for java.lang.reflect Method getParameterTypes

Introduction

In this page you can find the example usage for java.lang.reflect Method getParameterTypes.

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:Main.java

public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<String, Object>();
    for (Method method : bean.getClass().getMethods()) {
        String name = method.getName();
        if ((name.length() > 3 && name.startsWith("get") || name.length() > 2 && name.startsWith("is"))
                && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0
                && method.getDeclaringClass() != Object.class) {
            int i = name.startsWith("get") ? 3 : 2;
            String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
            try {
                map.put(key, method.invoke(bean, new Object[0]));
            } catch (Exception e) {
            }/*ww  w .j  a  v a  2s. co  m*/
        }
    }
    return map;
}

From source file:com.taobao.itest.spring.context.SpringContextManager.java

public static void findAnnotatedMethodsAndInjectBeanByName(Class<? extends Annotation> annotationType,
        Object object, ApplicationContext applicationContext) {
    Set<Method> methods = getMethodsAnnotatedWith(object.getClass(), annotationType);
    for (Method method : methods) {
        try {/*from  w ww .  j  a va2  s.c  o m*/
            if (!isSetter(method)) {
                invokeMethod(object, method, getBean(method.getParameterTypes()[0], applicationContext));
            } else {
                invokeMethod(object, method, getBean(getPropertyName(method), applicationContext));
            }

        } catch (InvocationTargetException e) {
            throw new RuntimeException(
                    "Unable to assign the Spring bean value to method annotated with @"
                            + annotationType.getSimpleName() + ". Method " + "has thrown an exception.",
                    e.getCause());
        }
    }
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * This is the (mostly) inverse of {@link #setParametersFromMap(Map, Object)}. Given a parameter object that is bean where the parameters are the
 * attributes with a simple type (i.e. Double, Integer, Float, or Boolean), a map of the attribute names to values (as strings) is
 * created./*from  w w w .  j ava 2s .c om*/
 * 
 * @param parameters
 *          Parameter object to pull keys and values from.
 * @return
 */
@SuppressWarnings("unchecked")
public static void setMapFromParameters(final Object parameters, final Map parameterMap) {
    final List<Class> simpleTypes = java.util.Arrays.asList(new Class[] { Double.class, Integer.class,
            Float.class, Boolean.class, Long.class, String.class, Short.class, double.class, int.class,
            float.class, boolean.class, long.class, short.class });
    for (final Method method : parameters.getClass().getMethods()) {
        String methodName = method.getName();
        if (method.getParameterTypes().length != 0
                || !(methodName.startsWith("get") || methodName.startsWith("is"))
                || !simpleTypes.contains(method.getReturnType())) {
            continue;
        }
        String attributeName;
        if (methodName.startsWith("get")) {
            attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
        } else { // is method
            attributeName = methodName.substring(2, 3).toLowerCase() + methodName.substring(3);
        }
        Object result;
        try {
            result = method.invoke(parameters);
        } catch (final Exception e) {
            logger.info(e);
            throw new IllegalArgumentException(
                    "Failed to invoke get method on bean, should not happen with simple beans.", e);
        }
        if (result != null) {
            parameterMap.put(attributeName, result.toString());
        } // else null value found in setMapFromParameters, not adding it to the map"
    }
}

From source file:net.dmulloy2.autocraft.util.BlockStates.java

/**
 * Applies serialized data from {@link #serialize(BlockState)} back to a
 * BlockState.// w  w w . j  a  va  2  s. c  om
 * 
 * @param state State to apply data to
 * @param values Data to apply
 */
public static void deserialize(BlockState state, Map<String, Object> values) {
    for (Method method : ReflectionUtil.getMethods(state.getClass())) {
        try {
            String name = method.getName();
            if (name.startsWith("set") && method.getParameterTypes().length == 1) {
                name = name.substring(3, name.length());
                if (values.containsKey(name)) {
                    method.invoke(state, values.get(name));
                }
            }
        } catch (Throwable ex) {
        }
    }
}

From source file:ar.com.zauber.commons.spring.web.CommandURLParameterGenerator.java

/**
 * @see #getURLParameter(Class, Set)//from   w  ww  .j  a v  a  2s  .  co m
 */
public static String getURLParameter(final Class<?> cmdClass, final Map<Class<?>, Object> values) {
    final StringBuilder sb = new StringBuilder("?");
    final Pattern getter = Pattern.compile("^get(.*)$");
    boolean first = true;

    // look for getters
    for (final Method method : cmdClass.getMethods()) {
        final Matcher matcher = getter.matcher(method.getName());
        if (matcher.lookingAt() && matcher.groupCount() == 1 && method.getParameterTypes().length == 0
                && values.containsKey(method.getReturnType())) {
            try {
                cmdClass.getMethod("set" + matcher.group(1), new Class[] { method.getReturnType() });
                if (!first) {
                    sb.append("&");
                } else {
                    first = false;
                }

                sb.append(URLEncoder.encode(matcher.group(1).toLowerCase(), CHARSET));
                sb.append("=");
                sb.append(URLEncoder.encode(values.get(method.getReturnType()).toString(), CHARSET));
            } catch (Exception e) {
                // skip
            }
        }
    }

    return sb.toString();
}

From source file:net.dmulloy2.autocraft.util.BlockStates.java

/**
 * Serializes a given BlockState into a map. This method ignores
 * inventories, which should be handled separately.
 *
 * @param state BlockState to serialize/* ww w .  j  a v a  2  s . c  o  m*/
 * @return Serialized block state
 */
public static Map<String, Object> serialize(BlockState state) {
    Map<String, Object> ret = new HashMap<>();

    for (Method method : ReflectionUtil.getMethods(state.getClass())) {
        try {
            String name = method.getName();
            if (name.startsWith("get") && method.getParameterTypes().length == 0) {
                name = name.substring(3, name.length());
                Object value = method.invoke(state);
                if (!(value instanceof Inventory)) {
                    ret.put(name, value);
                }
            }
        } catch (Throwable ex) {
        }
    }

    return ret;
}

From source file:de.otto.jsonhome.generator.MethodHelper.java

private static Method methodFromSuperclass(final Method method) {
    final Class<?> superclass = method.getDeclaringClass().getSuperclass();
    if (superclass != null) {
        return ClassUtils.getMethodIfAvailable(superclass, method.getName(), method.getParameterTypes());
    }/*  ww  w. j  av  a 2 s.  co  m*/
    return null;
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.acl.ProxyUtils.java

/**
 * Finds the method in the unproxied superclass if proxied.
 * @param method  the method/*from w ww . j  ava 2s  .  c o m*/
 * @return  the method in the unproxied class
 */
public static Method unproxy(final Method method) {
    Class<?> clazz = method.getDeclaringClass();

    if (!AopUtils.isCglibProxyClass(clazz)) {
        return method;
    }

    return ReflectionUtils.findMethod(unproxy(clazz), method.getName(), method.getParameterTypes());
}

From source file:grails.plugin.springsecurity.acl.util.ProxyUtils.java

/**
 * Finds the method in the unproxied superclass if proxied.
 * @param method  the method// ww w.ja v a  2s  .  co  m
 * @return  the method in the unproxied class
 */
public static Method unproxy(final Method method) {
    Class<?> clazz = method.getDeclaringClass();

    if (!isProxy(clazz)) {
        return method;
    }

    return ReflectionUtils.findMethod(unproxy(clazz), method.getName(), method.getParameterTypes());
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Returns the property name for a method.
 * This method is independent from property fields.
 *
 * @param method The method to get the property name for.
 * @return the property name for given method; null if non could be resolved.
 *///w w  w  . ja va2s  .com
public static String resolvePropertyName(Method method) {

    Matcher matcher = SETTER_PATTERN.matcher(method.getName());
    if (matcher.matches() && method.getParameterTypes().length == 1) {
        String raw = matcher.group(1);
        return raw.substring(0, 1).toLowerCase() + raw.substring(1);
    }

    matcher = GETTER_PATTERN.matcher(method.getName());
    if (matcher.matches() && method.getParameterTypes().length == 0) {
        String raw = matcher.group(2);
        return raw.substring(0, 1).toLowerCase() + raw.substring(1);
    }

    return null;
}