Java Reflection Generic Type from Class getGenericTypeClass(Class clazz, int index)

Here you can find the source of getGenericTypeClass(Class clazz, int index)

Description

get Generic Type Class

License

Apache License

Declaration

public static Class<?> getGenericTypeClass(Class<?> clazz, int index) 

Method Source Code


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

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    public static Class<?> getGenericTypeClass(Class<?> clazz, int index) {
        Class<?> superClass = clazz.getSuperclass();
        Type genericSuperClass = clazz.getGenericSuperclass();
        while (!(genericSuperClass instanceof ParameterizedType)) {
            genericSuperClass = superClass.getGenericSuperclass();
            superClass = superClass.getSuperclass();
        }/*from  w w  w  .  jav  a  2  s .co m*/

        Type[] types = ((ParameterizedType) genericSuperClass).getActualTypeArguments();
        if (index < types.length) {
            return (Class<?>) types[index];
        } else {
            return null;
        }
    }
}

Related

  1. getGenericType(Object o, Class declaringClass, int idx)
  2. getGenericType(Type type, Class rawType, int index)
  3. getGenericTypeArgument(Class clazz, int index)
  4. getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass, Class baseRequested)
  5. getGenericTypeArgumentsOfInheritedType(final Object object, final Class inheritedType)
  6. getGenericTypeClasses(List> genericTypeClasses, Type... genericTypes)
  7. getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType)
  8. getGenericTypeOfInterface(Class clazz, Class specificInterface)
  9. getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)