Java Utililty Methods Reflection Generic Type from Class

List of utility methods to do Reflection Generic Type from Class

Description

The list of methods to do Reflection Generic Type from Class are organized into topic(s).

Method

ClassgetGenericArgumentType(Class clazz)
Gets the first generic argument of the given class.
return getGenericArgumentType(clazz, 0);
ClassgetGenericArgumentType(Class currentClass, Class genericSuperClass, int argumentIndex)
get Generic Argument Type
Type superType = currentClass.getGenericSuperclass();
if (superType == null) {
    throw new IllegalArgumentException(
            "Class '" + currentClass + "' doesn't have correct generic super class");
if (!(superType instanceof ParameterizedType)
        || genericSuperClass != null && ((ParameterizedType) superType).getRawType() != genericSuperClass) {
    return getGenericArgumentType(currentClass.getSuperclass(), genericSuperClass, argumentIndex);
...
ClassgetGenericClass(Class clazz)
get Generic Class
return getGenericClass(clazz, 0);
ClassgetGenericClass(Class clazz)
get the first generic declaration on a class.
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
    return Object.class;
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
return (Class) params[0];
ClassgetGenericClass(Class clazz)
get Generic Class
return getGenericClass(clazz, 0);
ClassgetGenericClass(Class clazz, int index)
get Generic Class
Type type = getGenericType(clazz, index);
if (type instanceof Class<?>) {
    return (Class<?>) type;
throw new IllegalArgumentException("Generic parameter is not of type Class");
ClassgetGenericClass(Class clazz, int index)
Locates generic declaration by index on a class.
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;
...
ClassgetGenericClass(Class clazz)
get Generic Class
Type t = clazz.getGenericSuperclass();
if (t instanceof ParameterizedType) {
    Type[] p = ((ParameterizedType) t).getActualTypeArguments();
    return ((Class<?>) p[0]);
return null;
ClassgetGenericClass(Class clazz, int index)
get Generic Class
Type[] actualTypeArguments = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();
if (actualTypeArguments.length < index)
    throw new IndexOutOfBoundsException();
return (Class<T>) actualTypeArguments[index];
ClassgetGenericClass(Class cls)
get Generic Class
return getGenericClass(cls, 0);