Java Reflection Generic Type from Class getGenericTypeParameter(Class aClass, Class genericClass, int index)

Here you can find the source of getGenericTypeParameter(Class aClass, Class genericClass, int index)

Description

get Generic Type Parameter

License

LGPL

Declaration

public static Class<?> getGenericTypeParameter(Class<?> aClass, Class<?> genericClass, int index) 

Method Source Code

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

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Class<?> getGenericTypeParameter(Class<?> aClass, Class<?> genericClass, int index) {
        List<Class<?>> classes = new ArrayList<>();
        Class<?> currentCls = aClass;
        boolean isGenericClassInterface = genericClass.isInterface();

        String parameterName = genericClass.getTypeParameters()[index].getName();
        while (currentCls != null && currentCls != genericClass && genericClass.isAssignableFrom(currentCls)) {
            classes.add(currentCls);/*  www  .  j a  va 2s  .c  o  m*/
            currentCls = currentCls.getSuperclass();
        }

        Class<?> managedClass = null;
        boolean shouldCheckInInterfaces = isGenericClassInterface;
        for (int i = classes.size() - 1; i >= 0 && managedClass == null; i--) {
            Class<?> cls = classes.get(i);
            ParameterizedType parameterizedType = null;

            if (shouldCheckInInterfaces) {
                for (Type superInterface : cls.getGenericInterfaces()) {
                    if (superInterface instanceof ParameterizedType) {
                        ParameterizedType type = (ParameterizedType) superInterface;

                        if (type.getRawType() instanceof Class) {
                            Class rawType = (Class) type.getRawType();
                            if (rawType.isAssignableFrom(genericClass)) {
                                shouldCheckInInterfaces = false;
                                parameterizedType = type;
                            }
                        }
                    }
                }
            } else {
                if (cls.getGenericSuperclass() instanceof ParameterizedType) {
                    parameterizedType = (ParameterizedType) cls.getGenericSuperclass();

                    index = 0;
                    // Updating the index of the type parameter
                    for (TypeVariable typeVariable : cls.getSuperclass().getTypeParameters()) {
                        if (typeVariable.getName().equals(parameterName)) {
                            break;
                        }
                        index++;
                    }
                }
            }

            if (parameterizedType != null && parameterizedType.getActualTypeArguments().length > index) {
                Type typeArgument = parameterizedType.getActualTypeArguments()[index];

                if (typeArgument instanceof Class) {
                    managedClass = (Class<?>) typeArgument;
                } else if (typeArgument instanceof TypeVariable) {
                    TypeVariable typeVar = (TypeVariable) typeArgument;
                    // Updating the new name of the type parameter
                    parameterName = typeVar.getName();
                }
            }
        }

        return managedClass;
    }
}

Related

  1. getGenericTypeClass(Class clazz, int index)
  2. getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)
  3. getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType)
  4. getGenericTypeOfInterface(Class clazz, Class specificInterface)
  5. getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)
  6. getGenericTypes(Class c)
  7. getGenericTypes(Class clazz)
  8. getGenericTypes(final Class iClass)
  9. getGenericTypesImpl(ParameterizedType ptype, List> classes)