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

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

Description

get Generic Type

License

Apache License

Declaration

public static Class<?> getGenericType(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<?> getGenericType(Class<?> clazz, int index) {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }//www .j a  v  a  2 s  . co m
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if (index >= params.length || index < 0) {
            throw new RuntimeException("Index outof bounds");
        }
        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return (Class<?>) params[index];
    }
}

Related

  1. getGenericSuperType(Class class1, Class class2)
  2. getGenericType(Class clazz, int index)
  3. getGenericType(Class propertyType)
  4. getGenericType(Class clazz)
  5. getGenericType(Class clazz)
  6. getGenericType(Class target)
  7. getGenericType(Class type)
  8. getGenericType(Class type, Class clazz)
  9. getGenericType(Object o, Class declaringClass, int idx)