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

ClassgetGenericClass(final Class class1)
get Generic Class
final Type type = class1.getGenericSuperclass();
return getGenericClass(type);
ClassgetGenericClass(final Class parametrizedClass, int pos)
get Generic Class
final ParameterizedType pType = (ParameterizedType) parametrizedClass.getGenericSuperclass();
final TypeVariable tv = (TypeVariable) pType.getActualTypeArguments()[pos];
if (tv.getBounds()[0] instanceof ParameterizedType) {
    return (Class<T>) ((ParameterizedType) tv.getBounds()[0]).getRawType();
} else {
    return (Class<T>) tv.getBounds()[0];
ClassgetGenericClass(final Class parametrizedClass, int pos)
get Generic Class
return (Class<T>) ((ParameterizedType) parametrizedClass.getGenericSuperclass())
        .getActualTypeArguments()[pos];
ClassgetGenericClass(final Method method)
get Generic Class
final Type returnType = method.getGenericReturnType();
return getActualType(returnType, 0);
ClassgetGenericClass(Object o)
get Generic Class
ParameterizedType parameterizedType = (ParameterizedType) o.getClass().getGenericSuperclass();
return (Class) parameterizedType.getActualTypeArguments()[0];
ClassgetGenericClass(Object object, int index)
get Generic Class
ParameterizedType genericSuperclass = getParameterizedType(object.getClass());
if (genericSuperclass == null) {
    return String.class;
Class<?> rawType = getRawType(genericSuperclass.getActualTypeArguments()[index]);
if (rawType == null) {
    return String.class;
return rawType;
ClassgetGenericClass(Object source, ParameterizedType type)
Try to extract the generic type of the given ParameterizedType used in the given source object.
Type type1 = type.getActualTypeArguments()[0];
if (type1 instanceof ParameterizedType) {
    return (Class) ((ParameterizedType) type1).getRawType();
} else if (type1 instanceof TypeVariable) {
    if (source.getClass().getGenericSuperclass() instanceof ParameterizedType) {
        return (Class) ((ParameterizedType) source.getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    } else {
...
ClassgetGenericClass(Type type)
get Generic Class
return getGenericClass(type, 0);
TypegetGenericClassByIndex(Type genericType, int index)
Get parameterized type
Type clazz = null;
if (genericType instanceof ParameterizedType) {
    ParameterizedType t = (ParameterizedType) genericType;
    Type[] types = t.getActualTypeArguments();
    clazz = types[index];
return clazz;
TypegetGenericClassType(Class clazz, Class filterClass)
Extract the real Type from the passed class.
for (Type type : clazz.getGenericInterfaces()) {
    if (type == filterClass) {
        return type;
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        if (filterClass.isAssignableFrom((Class) pType.getRawType())) {
            return type;
return null;