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:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

/**
 * For abstract (e.g. interface) methods, only Java 8 Parameter names (compiler arg
 * -parameters) can supply parameter names; bytecode-based strategies use local
 * variable declarations, of which there are none for abstract methods.
 * @param m// w  w  w  .  j ava  2s.  c  om
 * @return whether a parameter name was found
 * @throws IllegalArgumentException if method has no parameters
 */
private static boolean hasJava8ParameterNames(Method m) {
    org.springframework.util.Assert.isTrue(m.getParameterTypes().length > 0, "method has no parameters");
    if (EXECUTABLE_TYPE != null) {
        Method getParameters = ReflectionUtils.findMethod(EXECUTABLE_TYPE, "getParameters");
        try {
            Object[] parameters = (Object[]) getParameters.invoke(m);
            Method isNamePresent = ReflectionUtils.findMethod(parameters[0].getClass(), "isNamePresent");
            return Boolean.TRUE.equals(isNamePresent.invoke(parameters[0]));
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        }
    }
    return false;
}

From source file:net.kamhon.ieagle.util.VoUtil.java

/**
 * To trim object String field//from  w w w  .  j  av  a2s .  com
 * 
 * @param all
 *            default is false. true means toTrim all String field, false toTrim the fields have
 *            net.kamhon.ieagle.vo.core.annotation.ToTrim.
 * 
 * @param objects
 */
public static void toTrimProperties(boolean all, Object... objects) {
    for (Object object : objects) {
        if (object == null) {
            continue;
        }

        // getter for String field only
        Map<String, Method> getterMap = new HashMap<String, Method>();
        // setter for String field only
        Map<String, Method> setterMap = new HashMap<String, Method>();

        Class<?> clazz = object.getClass();

        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            if (method.getName().startsWith("get") && method.getParameterTypes().length == 0
                    && method.getReturnType().equals(String.class)) {

                // if method name is getXxx
                String fieldName = method.getName().substring(3);
                fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);

                getterMap.put(fieldName, method);
            } else if (method.getName().startsWith("set") && method.getParameterTypes().length == 1
                    && method.getParameterTypes()[0] == String.class
                    && method.getReturnType().equals(void.class)) {
                // log.debug("setter = " + method.getName());

                // if method name is setXxx
                String fieldName = method.getName().substring(3);
                fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);

                setterMap.put(fieldName, method);
            }
        }

        // if the key exists in both getter & setter
        for (String key : getterMap.keySet()) {
            if (setterMap.containsKey(key)) {
                try {
                    Method getterMethod = getterMap.get(key);
                    Method setterMethod = setterMap.get(key);

                    // if not all, check on Field
                    if (!all) {
                        Field field = null;

                        Class<?> tmpClazz = clazz;
                        // looping up to superclass to get decleared field
                        do {
                            try {
                                field = tmpClazz.getDeclaredField(key);
                            } catch (Exception ex) {
                                // do nothing
                            }

                            if (field != null) {
                                break;
                            }

                            tmpClazz = tmpClazz.getSuperclass();
                        } while (tmpClazz != null);

                        ToTrim toTrim = field.getAnnotation(ToTrim.class);
                        if (toTrim == null || toTrim.trim() == false) {
                            continue;
                        }
                    }

                    String value = (String) getterMethod.invoke(object, new Object[] {});

                    if (StringUtils.isNotBlank(value))
                        setterMethod.invoke(object, value.trim());

                } catch (Exception ex) {
                    // log.error("Getter Setter for " + key + " has error ", ex);
                }
            }
        }
    }
}

From source file:cn.webwheel.ActionSetter.java

public static String isGetter(Method method) {
    String name = method.getName();
    if (!name.startsWith("get"))
        return null;
    if (name.length() < 4)
        return null;
    if (method.getParameterTypes().length != 0)
        return null;
    if (method.getReturnType() == Void.class)
        return null;
    name = name.substring(3);// w  w  w. java 2  s.  co m
    if (name.length() > 1 && name.equals(name.toUpperCase()))
        return name;
    return name.substring(0, 1).toLowerCase() + name.substring(1);
}

From source file:net.sf.ehcache.config.BeanHandler.java

/**
 * Finds a creator method./*w  ww .  jav a  2s. com*/
 */
private static Method findCreateMethod(Class objClass, String name) {
    final String methodName = makeMethodName("create", name);
    final Method[] methods = objClass.getMethods();
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        if (!method.getName().equals(methodName)) {
            continue;
        }
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }
        if (method.getParameterTypes().length != 0) {
            continue;
        }
        if (method.getReturnType().isPrimitive() || method.getReturnType().isArray()) {
            continue;
        }
        return method;
    }

    return null;
}

From source file:net.abhinavsarkar.spelhelper.SpelHelper.java

private static List<Method> filterMethods(final Class<?> clazz) {
    List<Method> allowedMethods = new ArrayList<Method>();
    for (Method method : clazz.getMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
                && !method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length > 0) {
            allowedMethods.add(method);/*  w ww .  j  a  v  a2 s .c  om*/
        }
    }
    return allowedMethods;
}

From source file:alfio.datamapper.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args) {

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }/*from   w ww  .j av  a  2s.  c om*/

    MapSqlParameterSource ps = new MapSqlParameterSource();
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            if (args[i] != null && ZonedDateTime.class.isAssignableFrom(parameterTypes[i])) {
                ZonedDateTime dateTime = ZonedDateTime.class.cast(args[i]);
                final ZonedDateTime utc = dateTime.withZoneSameInstant(ZoneId.of("UTC"));
                Calendar c = Calendar.getInstance();
                c.setTimeZone(TimeZone.getTimeZone("UTC"));
                c.setTimeInMillis(utc.toInstant().toEpochMilli());
                ps.addValue(name, c, Types.TIMESTAMP);
            } else {
                ps.addValue(name, args[i], StatementCreatorUtils.javaTypeToSqlParameterType(parameterTypes[i]));
            }
        }
    }

    return ps;
}

From source file:net.servicefixture.util.ReflectionUtils.java

private static void setProperty(Object parent, String attributeName, Object source) throws Exception {
    Method[] methods = parent.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().startsWith("set") && method.getName().length() > 3
                && StringUtils.uncapitalize(method.getName().substring(3)).equals(attributeName)
                && method.getParameterTypes().length == 1
                && (source == null || method.getParameterTypes()[0].isAssignableFrom(source.getClass()))) {
            method.invoke(parent, source);
            return;
        }/*from w  w  w  .  jav a 2 s  .c om*/
    }
    BeanUtils.setProperty(parent, attributeName, source);
}

From source file:com.amalto.core.metadata.ClassRepository.java

private static boolean isClassMethod(Class clazz, Method declaredMethod) {
    Class superClass = clazz.getSuperclass();
    if (!Object.class.equals(superClass)) {
        try {/*  ww w .  j  av  a2s .  com*/
            return superClass.getMethod(declaredMethod.getName(), declaredMethod.getParameterTypes()) != null;
        } catch (NoSuchMethodException e) {
            return true;
        }
    }
    return true;
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Object invokeMethod(Object object, String methodName, List<?> argList)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method method = findMethod(object, methodName, argList);
    if (method == null) {
        throw new NoSuchMethodException(
                "No method " + methodName + " for arguments " + debugDumpArgList(argList) + " in " + object);
    }//from w  w w .  j  av a  2s. co m
    Object[] args = argList.toArray();
    if (method.isVarArgs()) {
        Class<?> parameterTypeClass = method.getParameterTypes()[0];
        Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length);
        for (int i = 0; i < args.length; i++) {
            varArgs[i] = args[i];
        }
        args = new Object[] { varArgs };
    }
    try {
        return method.invoke(object, args);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                e.getMessage() + " for arguments " + debugDumpArgList(argList) + " in " + object, e);
    }
}

From source file:com.qrmedia.commons.persistence.hibernate.clone.HibernateEntityBeanCloner.java

private static Collection<String> getPropertyNames(Collection<Method> methods) {
    Collection<String> methodNames = new ArrayList<String>();

    /*/*from   www .ja v a  2s  .c o m*/
     * If a method is an instance method, does not return void, takes no parameters 
     * and is named "get..." (it's assumed to be public), add the corresponding field name.
     */
    for (Method method : methods) {
        assert Modifier.isPublic(method.getModifiers()) : method;
        String methodName = method.getName();
        Matcher getterNameMatcher = GETTER_PREFIX.matcher(methodName);

        if (!Modifier.isStatic(method.getModifiers()) && (method.getReturnType() != Void.class)
                && (method.getParameterTypes().length == 0) && getterNameMatcher.matches()) {

            // the first group is the (uppercase) first letter of the field name
            methodNames.add(getterNameMatcher.replaceFirst(getterNameMatcher.group(1).toLowerCase() + "$2"));
        }

    }

    return methodNames;
}