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:io.stallion.reflection.PropertyUtils.java

private static Method getSetter(Object target, String propertyName) {
    if (propertyName == null || "".equals(propertyName))
        throw new PropertyException("encountered invalid null or empty property name");
    String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    Method[] methods = target.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equals(setterName) && method.getParameterTypes().length == 1) {
            return method;
        }//from  w  ww  .j a  v  a2s .  co m
    }
    return null;
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

private static String prepareExceptionMessage(Method method, Object[] args) {
    StringBuffer message = new StringBuffer(
            "Illegal object type for the method '" + method.getName() + "'. \n ");
    message.append("Expected types: \n");
    for (Class<?> type : method.getParameterTypes()) {
        message.append(type.getName());/*from w ww  . j  ava  2  s.  c  o  m*/
    }
    message.append("\n Actual types: \n");
    for (Object param : args) {
        message.append(param.getClass().getName());
    }
    return message.toString();
}

From source file:com.amazon.carbonado.layout.LayoutFactory.java

/**
 * Returns an annotation hash code using a algorithm similar to the
 * default. The difference is in the handling of class and enum values. The
 * name is chosen for the hash code component instead of the instance
 * because it is stable between invocations of the JVM.
 *//*from w  w  w .  ja  v a2s.  c o  m*/
private static int annHashCode(Annotation ann) {
    int hash = 0;

    Method[] methods = ann.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getReturnType() == null || m.getReturnType() == void.class) {
            continue;
        }
        if (m.getParameterTypes().length != 0) {
            continue;
        }

        String name = m.getName();
        if (name.equals("hashCode") || name.equals("toString") || name.equals("annotationType")) {
            continue;
        }

        Object value;
        try {
            value = m.invoke(ann);
        } catch (InvocationTargetException e) {
            continue;
        } catch (IllegalAccessException e) {
            continue;
        }

        hash += (127 * name.hashCode()) ^ annValueHashCode(value);
    }

    return hash;
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

private static <A extends Annotation> A searchOnInterfaces(Method method, Class<A> annotationType,
        Class<?>[] ifcs) {/*from  www.jav  a  2s.co  m*/
    A annotation = null;
    for (Class<?> iface : ifcs) {
        if (isInterfaceWithAnnotatedMethods(iface)) {
            try {
                Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
                annotation = getAnnotation(equivalentMethod, annotationType);
            } catch (NoSuchMethodException ex) {
                // Skip this interface - it doesn't have the method...
            }
            if (annotation != null) {
                break;
            }
        }
    }
    return annotation;
}

From source file:com.startechup.tools.ModelParser.java

/**
 * Invokes the setter method that was link to the json key.
 *
 * @param method The setter method to be invoked.
 * @param classInstance The instance of the container class.
 * @param key The json key serving as the reference of the value in the json object
 * @param jsonObject The API response object.
 *//*  w ww .j a v a2 s.  c o  m*/
private static void initMethodInvocation(Method method, Object classInstance, String key,
        JSONObject jsonObject) {
    Object value = getValueFromJsonObject(jsonObject, key, method.getName());

    // Only invoke the method when the value is not null
    if (value != null) {
        method.setAccessible(true);
        Object castedObject = value;

        if (value instanceof Number) {
            castedObject = castNumberObject(method.getParameterTypes()[0], value);
        } else if (value instanceof JSONArray) {
            if (method.getParameterTypes()[0].isArray()) {
                //TODO find a way to genetically convert json array to array, for now throw our custom exception
                throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]);
            } else {
                Object parameterInstance = getNewInstance(method.getParameterTypes()[0]);
                if (parameterInstance instanceof Collection) {
                    ParameterizedType parameterizedType = (ParameterizedType) method
                            .getGenericParameterTypes()[0];
                    Class<?> classType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
                    castedObject = parse(classType, (JSONArray) value);
                } else {
                    //TODO find a way to genetically convert json array to the other parameter class, for now throw our custom exception
                    throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]);
                }
            }
        } else if (value instanceof JSONObject) {
            castedObject = parse(method.getParameterTypes()[0], ((JSONObject) value));
        }

        // Finally invoke the method after casting the values into the method parameter type
        invoke(method, classInstance, castedObject);
    }
}

From source file:ch.digitalfondue.npjt.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args,
        Collection<ParameterConverter> parameterConverters) {

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }//from w ww .  j  a v a2  s  .  co m

    MapSqlParameterSource ps = new MapSqlParameterSource();
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            Object arg = args[i];
            Class<?> parameterType = parameterTypes[i];

            boolean hasAccepted = false;
            for (ParameterConverter parameterConverter : parameterConverters) {
                if (parameterConverter.accept(parameterType)) {
                    hasAccepted = true;
                    parameterConverter.processParameter(name, arg, parameterType, ps);
                    break;
                }
            }

            if (!hasAccepted) {
                throw new IllegalStateException(
                        "Was not able to find a ParameterConverter able to process object: " + arg
                                + " with class " + parameterType);
            }
        }
    }

    return ps;
}

From source file:org.openflamingo.uploader.util.ReflectionUtils.java

/**
 * ? Signature ./*from  w  w w  . j a  va2s  . c om*/
 *
 * @param method 
 * @return ? Signature
 */
private static String getMethodSignature(Method method) {
    String methodName = method.getName();
    Class<?>[] classes = method.getParameterTypes();

    StringBuilder builder = new StringBuilder();
    builder.append("[ ");
    builder.append(methodName);
    builder.append("(");
    for (int i = 0; i < classes.length; i++) {
        Class<?> aClass = classes[i];
        builder.append(aClass.getName());
        if (i != (classes.length - 1)) {
            builder.append(", ");
        }
    }
    builder.append(")");
    builder.append(" ]");
    return builder.toString();
}

From source file:com.snaplogic.snaps.firstdata.Create.java

static boolean isGetter(Method method) {
    if (Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0) {
        String methodName = method.getName();
        if (methodName.matches(GET_REGEX) && !method.getReturnType().equals(void.class))
            return true;
        if (methodName.matches(IS_REGEX) && method.getReturnType().equals(boolean.class))
            return true;
    }//from  w  ww  .  j av  a2  s . c  om
    return false;
}

From source file:org.openflamingo.uploader.util.ReflectionUtils.java

/**
 * ? Signature ./*from  w ww  . j a  v  a  2  s  .  c o m*/
 *
 * @param method 
 * @return ? Signature
 */
private static String getMethodSignature(Method method, Object[] args) {
    String methodName = method.getName();
    Class<?>[] classes = method.getParameterTypes();

    StringBuilder builder = new StringBuilder();
    builder.append("[ ");
    builder.append(methodName);
    builder.append("(");
    builder.append(getMethodParameter(classes, args));
    builder.append(")");
    builder.append(" ]");
    return builder.toString();
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Determine whether the given method is a "hashCode" method.
 * @see java.lang.Object#hashCode()//from  w ww .j a  va  2  s .c o  m
 */
public static boolean isHashCodeMethod(Method method) {
    return (method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0);
}