Java Reflection Generic Type from Class getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)

Here you can find the source of getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)

Description

get Generic Type Of Parameter

License

Open Source License

Declaration

public static Class<?> getGenericTypeOfParameter(Class<?> clazz, String method, int parameterIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static Class<?> getGenericTypeOfParameter(Class<?> clazz, String method, int parameterIndex) {
        try {/*from   www.  j a  v a 2s.  co m*/
            Method m = clazz.getMethod(method, new Class<?>[] { Set.class, int.class });
            ParameterizedType type = (ParameterizedType) m.getGenericParameterTypes()[parameterIndex];
            return (Class<?>) type.getActualTypeArguments()[0];
        } catch (Exception e) {
            try {
                Method m = clazz.getMethod(method, new Class<?>[] { HashSet.class, int.class });
                ParameterizedType type = (ParameterizedType) m.getGenericParameterTypes()[parameterIndex];
                return (Class<?>) type.getActualTypeArguments()[0];
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return null;
    }
}

Related

  1. getGenericTypeArgumentsOfInheritedType(final Object object, final Class inheritedType)
  2. getGenericTypeClass(Class clazz, int index)
  3. getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)
  4. getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType)
  5. getGenericTypeOfInterface(Class clazz, Class specificInterface)
  6. getGenericTypeParameter(Class aClass, Class genericClass, int index)
  7. getGenericTypes(Class c)
  8. getGenericTypes(Class clazz)
  9. getGenericTypes(final Class iClass)