Java Reflection Generic Type from Class getGenericSuperType(Class clz, int index)

Here you can find the source of getGenericSuperType(Class clz, int index)

Description

get Generic Super Type

License

Open Source License

Declaration

public static Class getGenericSuperType(Class clz, int index) 

Method Source Code

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

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

public class Main {

    public static Class getGenericSuperType(Class clz, int index) {
        Class[] clzs = getAllGenericSuperType(clz);
        if (index < 0 || index > clzs.length - 1)
            return Object.class;
        return clzs[index];
    }/*  w  w w  .  j  a v  a  2  s  . c  o  m*/

    public static Class[] getAllGenericSuperType(Class clz) {
        if (clz == null)
            return new Class[] { Object.class };

        Type genericType = clz.getGenericSuperclass();

        if (!(genericType instanceof ParameterizedType))
            return new Class[] { Object.class };

        Type[] params = ((ParameterizedType) genericType).getActualTypeArguments();

        Class[] clzs = new Class[params.length];
        for (int i = 0; i < params.length; i++)
            if (params[i] instanceof Class)
                clzs[i] = (Class) params[i];
            else
                clzs[i] = Object.class;

        return clzs;
    }
}

Related

  1. getGenerics(Class classType)
  2. getGenericsClass(Class cls)
  3. getGenericSuperclass(Class clazz)
  4. getGenericSuperclass(Class cls)
  5. getGenericSuperclassActualTypes(Collection types, Class aClass)
  6. getGenericSuperType(Class subclass, Class classWithParameter)
  7. getGenericSuperType(Class class1, Class class2)
  8. getGenericType(Class clazz, int index)
  9. getGenericType(Class propertyType)