Example usage for java.lang.reflect Method getGenericParameterTypes

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

Introduction

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

Prototype

@Override
public Type[] getGenericParameterTypes() 

Source Link

Usage

From source file:GenericReflectionTest.java

public static void printMethod(Method m) {
    String name = m.getName();//from w w  w. j a v a 2  s  .co  m
    System.out.print(Modifier.toString(m.getModifiers()));
    System.out.print(" ");
    printTypes(m.getTypeParameters(), "<", ", ", "> ", true);

    printType(m.getGenericReturnType(), false);
    System.out.print(" ");
    System.out.print(name);
    System.out.print("(");
    printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
    System.out.println(")");
}

From source file:org.synyx.hades.util.ClassUtils.java

/**
 * Checks the given method's parameters to match the ones of the given base
 * class method. Matches generic arguments agains the ones bound in the
 * given DAO interface.//from  w  ww.j av  a2s .  c om
 * 
 * @param method
 * @param baseClassMethod
 * @param daoInterface
 * @return
 */
private static boolean parametersMatch(Method method, Method baseClassMethod, Class<?> daoInterface) {

    Type[] genericTypes = baseClassMethod.getGenericParameterTypes();
    Class<?>[] types = baseClassMethod.getParameterTypes();
    Class<?>[] methodParameters = method.getParameterTypes();

    for (int i = 0; i < genericTypes.length; i++) {

        Type type = genericTypes[i];

        if (type instanceof TypeVariable<?>) {

            String name = ((TypeVariable<?>) type).getName();

            if (!matchesGenericType(name, methodParameters[i], daoInterface)) {
                return false;
            }

        } else {

            if (!types[i].equals(methodParameters[i])) {
                return false;
            }
        }
    }

    return true;
}

From source file:org.jiemamy.utils.reflect.ReflectionUtil.java

/**
 * ???????????????//  w  w  w . j a  v  a  2s .  c  o  m
 * 
 * @param method 
 * @param parameterPosition ????????
 * @return ?????????????
 * @throws IllegalArgumentException {@code method}?{@code null}???
 * @throws IndexOutOfBoundsException ???????? parameterPosition?????
 */
public static Class<?> getElementTypeOfCollectionFromParameterType(Method method, int parameterPosition) {
    Validate.notNull(method);
    Type[] parameterTypes = method.getGenericParameterTypes();
    return getElementTypeOfCollection(parameterTypes[parameterPosition]);
}

From source file:org.jiemamy.utils.reflect.ReflectionUtil.java

/**
 * ???????????????/*  w w w.  j a  v a 2  s .  c  o  m*/
 * 
 * @param method 
 * @param parameterPosition ????????
 * @return ?????????????
 * @throws IllegalArgumentException ?{@code null}???
 * @throws IndexOutOfBoundsException ???????? parameterPosition?????
 */
public static Class<?> getElementTypeOfListFromParameterType(Method method, int parameterPosition) {
    Validate.notNull(method);
    Type[] parameterTypes = method.getGenericParameterTypes();
    return getElementTypeOfList(parameterTypes[parameterPosition]);
}

From source file:org.jiemamy.utils.reflect.ReflectionUtil.java

/**
 * ???????????????//  ww w .jav a2s .c o  m
 * 
 * @param method 
 * @param parameterPosition ????????
 * @return ?????????????
 * @throws IllegalArgumentException ?{@code null}???
 */
public static Class<?> getElementTypeOfSetFromParameterType(Method method, int parameterPosition) {
    Validate.notNull(method);
    Type[] parameterTypes = method.getGenericParameterTypes();
    return getElementTypeOfSet(parameterTypes[parameterPosition]);
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

private static Class detectDestCollectionPayload(Method setter) {
    Class destArgClass = null;//from   w w  w .  j a  v  a 2 s . c  om
    Type[] genericParameterTypes = setter.getGenericParameterTypes();
    Type genericParameterType = genericParameterTypes[0];
    if (genericParameterType instanceof ParameterizedType) {
        ParameterizedType aType = (ParameterizedType) genericParameterType;
        Type[] destArgs = aType.getActualTypeArguments();
        if (destArgs.length != 1) {
            System.out.println("3: Cannot determine payload type or multiple types found !!!");
            return null;
        }
        destArgClass = (Class) destArgs[0];
    }
    return destArgClass;
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

private static Map<Integer, Class> detectDestMapPayload(Method setter) {
    Map<Integer, Class> result = new HashMap();
    Type[] genericParameterTypes = setter.getGenericParameterTypes();
    Type genericParameterType = genericParameterTypes[0];
    if (genericParameterType instanceof ParameterizedType) {
        ParameterizedType aType = (ParameterizedType) genericParameterType;
        Type[] destArgs = aType.getActualTypeArguments();
        if (destArgs.length != 2) {
            System.err.println("2: Cannot determine payload type of source Map, operation aborted !!!");
            return null;
        }/*from ww  w.  ja va  2s .  c o m*/
        result.put(0, (Class) destArgs[0]);
        result.put(0, (Class) destArgs[1]);
    }
    return result;
}

From source file:Main.java

public static void dumpMethod(final Method method) {
    final StringBuilder builder = new StringBuilder();
    builder.append("------------------------------\n");
    builder.append("MethodName: ").append(method.getName()).append("\n");
    builder.append("ParameterTypes:{");
    for (Class<?> cls : method.getParameterTypes()) {
        builder.append(cls.getName()).append(", ");
    }//from   w w  w.ja  v  a 2s  .c o  m
    builder.append("}\n");
    builder.append("GenericParameterTypes:{");
    for (Type cls : method.getGenericParameterTypes()) {
        builder.append(cls.getClass()).append(", ");
    }
    builder.append("}\n");
    builder.append("TypeParameters:{");
    for (TypeVariable<Method> cls : method.getTypeParameters()) {
        builder.append(cls.getName()).append(", ");
    }
    builder.append("}\n");
    builder.append("DeclaredAnnotations:{");
    for (Annotation cls : method.getDeclaredAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("Annotations:{");
    for (Annotation cls : method.getAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("ExceptionTypes:{");
    for (Class<?> cls : method.getExceptionTypes()) {
        builder.append(cls.getName()).append(", ");
        ;
    }
    builder.append("}\n");
    builder.append("ReturnType: ").append(method.getReturnType());
    builder.append("\nGenericReturnType: ").append(method.getGenericReturnType());
    builder.append("\nDeclaringClass: ").append(method.getDeclaringClass());
    builder.append("\n");

    System.out.println(builder.toString());
}

From source file:org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.java

public static List<TypeInfo> getParameterTypeInfos(Method m, int size) {
    Type[] methodParameterTypes = m.getGenericParameterTypes();

    Type lastParaElementType = TypeInfoUtils.getArrayElementType(
            methodParameterTypes.length == 0 ? null : methodParameterTypes[methodParameterTypes.length - 1]);
    boolean isVariableLengthArgument = (lastParaElementType != null);

    List<TypeInfo> typeInfos = null;
    if (!isVariableLengthArgument) {
        if (size != methodParameterTypes.length) {
            return null;
        }//from  w w  w .  j  a v a  2 s. c om
        typeInfos = new ArrayList<TypeInfo>(methodParameterTypes.length);
        for (int i = 0; i < methodParameterTypes.length; i++) {
            typeInfos.add(getExtendedTypeInfoFromJavaType(methodParameterTypes[i], m));
        }
    } else {
        if (size < methodParameterTypes.length - 1) {
            return null;
        }
        typeInfos = new ArrayList<TypeInfo>(size);
        for (int i = 0; i < methodParameterTypes.length - 1; i++) {
            typeInfos.add(getExtendedTypeInfoFromJavaType(methodParameterTypes[i], m));
        }
        for (int i = methodParameterTypes.length - 1; i < size; i++) {
            typeInfos.add(getExtendedTypeInfoFromJavaType(lastParaElementType, m));
        }
    }
    return typeInfos;
}

From source file:org.dcm4che3.conf.core.util.ConfigIterators.java

private static List<AnnotatedSetter> processAnnotatedSetters(Class clazz) {
    List<AnnotatedSetter> list;
    list = new ArrayList<AnnotatedSetter>();

    // scan all methods including superclasses, assume each is a config-setter
    for (Method m : clazz.getMethods()) {
        AnnotatedSetter annotatedSetter = new AnnotatedSetter();
        annotatedSetter.setParameters(new ArrayList<AnnotatedConfigurableProperty>());

        Annotation[][] parameterAnnotations = m.getParameterAnnotations();
        Type[] genericParameterTypes = m.getGenericParameterTypes();

        // if method is no-arg, then it is not a setter
        boolean thisMethodIsNotASetter = true;

        for (int i = 0; i < parameterAnnotations.length; i++) {

            thisMethodIsNotASetter = false;

            AnnotatedConfigurableProperty property = new AnnotatedConfigurableProperty();
            property.setAnnotations(annotationsArrayToMap(parameterAnnotations[i]));
            property.setType(genericParameterTypes[i]);

            annotatedSetter.getParameters().add(property);

            // make sure all the parameters of this setter-wannabe are annotated
            if (property.getAnnotation(ConfigurableProperty.class) == null) {
                thisMethodIsNotASetter = true;
                break;
            }/*w ww  .j a  v  a  2 s  .  c om*/
        }

        // filter out non-setters
        if (thisMethodIsNotASetter)
            continue;

        list.add(annotatedSetter);
        annotatedSetter.setAnnotations(annotationsArrayToMap(m.getAnnotations()));
        annotatedSetter.setMethod(m);
    }

    configurableSettersCache.put(clazz, list);
    return list;
}