Java Reflection Method Parameter getMethodGenericParameterTypes(Method method, int index)

Here you can find the source of getMethodGenericParameterTypes(Method method, int index)

Description

get Method Generic Parameter Types

License

Apache License

Declaration

public static List<Class> getMethodGenericParameterTypes(Method method, int index) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static List<Class> getMethodGenericParameterTypes(Method method, int index) {
        List<Class> results = new ArrayList<Class>();
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        if (index >= genericParameterTypes.length || index < 0) {
            throw new IllegalArgumentException("index " + (index < 0 ? " must > 0 " : " over total arguments"));
        }/*from  w w  w  .ja v  a  2  s  . com*/
        Type genericParameterType = genericParameterTypes[index];
        if (genericParameterType instanceof ParameterizedType) {
            ParameterizedType aType = (ParameterizedType) genericParameterType;
            Type[] parameterArgTypes = aType.getActualTypeArguments();
            for (Type parameterArgType : parameterArgTypes) {
                Class parameterArgClass = (Class) parameterArgType;
                results.add(parameterArgClass);
            }
            return results;
        }
        return results;
    }

    public static List<Class> getMethodGenericParameterTypes(Method method) {
        return getMethodGenericParameterTypes(method, 0);
    }
}

Related

  1. getMethodByName(Class clazz, String name, Class... parameterTypes)
  2. getMethodByParametersWithAnnotation(final Class source, final Class[] params, final Class annotation)
  3. getMethodFromClass(String className, String methodName, Class... parameterTypes)
  4. getMethodFromClassName(String classAndMethodName, Class... parameterTypes)
  5. getMethodFullName(Method m, boolean includeClassPackage, boolean includeParametersType, boolean includeTypesPackage)
  6. getMethodHandle(final Lookup lookup, final Class receiver, final String methodName, final Class... parameterTypes)
  7. getMethodName(Method method, Class[] parameterClasses, String rightCode)
  8. getMethodParameterAnnotations(Method method, int index, Class annotationClass)
  9. getMethodParameterIndexes(final Method m)