Here you can find the source of getGenericTypeParameter(Class> aClass, Class> genericClass, int index)
public static Class<?> getGenericTypeParameter(Class<?> aClass, Class<?> genericClass, int index)
//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; } }