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

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

Description

Locates generic declaration by index on a class.

License

Open Source License

Parameter

Parameter Description
clazz clazz The class to introspect
index the Index of the generic ddeclaration,start from 0.

Declaration

public static Class getGenericClass(Class clazz, int index) 

Method Source Code


//package com.java2s;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    /**// w ww. j a v a  2s .c o m
     * Locates  generic declaration by index on a class.
     *
     * @param clazz clazz The class to introspect
     * @param index the Index of the generic ddeclaration,start from 0.
     */
    public static Class getGenericClass(Class clazz, int index) {
        Type genType = clazz.getGenericSuperclass();

        if (genType instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

            if ((params != null) && (params.length >= (index - 1))) {
                return (Class) params[index];
            }
        }
        return null;
    }
}

Related

  1. getGenericArgumentType(Class currentClass, Class genericSuperClass, int argumentIndex)
  2. getGenericClass(Class clazz)
  3. getGenericClass(Class clazz)
  4. getGenericClass(Class clazz)
  5. getGenericClass(Class clazz, int index)
  6. getGenericClass(Class clazz)
  7. getGenericClass(Class clazz, int index)
  8. getGenericClass(Class cls)
  9. getGenericClass(final Class class1)