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

TypegetGenericType(Class clazz, int index)
Find the Generic Type of the form Class .
ParameterizedType type = (ParameterizedType) clazz.getGenericSuperclass();
if (type.getActualTypeArguments() == null || type.getActualTypeArguments().length <= index) {
    throw new IllegalArgumentException("No generic type at the specified position");
return type.getActualTypeArguments()[index];
ClassgetGenericType(Class propertyType)
get Generic Type
Class genericType = null;
TypeVariable[] typeParameters = propertyType.getTypeParameters();
if (typeParameters != null && typeParameters.length > 0) {
    Type[] bounds = typeParameters[0].getBounds();
    if (bounds != null && bounds.length > 0 && (bounds[0] instanceof Class)) {
        genericType = (Class) bounds[0];
return genericType;
ClassgetGenericType(Class clazz)
get Generic Type
return getGenericType(clazz, 0);
ClassgetGenericType(Class clazz)
get Generic Type
Type genericType = clazz.getGenericInterfaces()[0];
Class<?>[] classes = getActualClass(genericType);
if (classes.length == 0) {
    return null;
} else {
    return classes[0];
ClassgetGenericType(Class clazz, int index)
get Generic Type
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
    return Object.class;
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];
Type[]getGenericType(Class target)
get Generic Type
if (target == null)
    return new Type[0];
Type[] types = target.getGenericInterfaces();
if (types.length > 0) {
    return types;
Type type = target.getGenericSuperclass();
if (type != null) {
...
TgetGenericType(Class type)
get Generic Type
assert type != null;
ParameterizedType genericSuperclass = (ParameterizedType) type.getGenericSuperclass();
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
return (T) actualTypeArguments[0];
ObjectgetGenericType(Class type, Class clazz)
Obtain the concrete type used by an implementation of an interface that uses a generic type.
Type[] interfaces = clazz.getGenericInterfaces();
for (Type iface : interfaces) {
    if (iface instanceof ParameterizedType) {
        ParameterizedType pi = (ParameterizedType) iface;
        if (pi.getRawType() instanceof Class) {
            if (type.isAssignableFrom((Class<?>) pi.getRawType())) {
                return getTypeParameter(clazz, pi.getActualTypeArguments()[0]);
@SuppressWarnings("unchecked")
Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass();
Object result = getGenericType(type, superClazz);
if (result instanceof Class<?>) {
    return result;
} else if (result instanceof Integer) {
    ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass();
    return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]);
} else {
    return null;
ClassgetGenericType(Object o, Class declaringClass, int idx)
get Generic Type
Class<?> clazz = o.getClass();
LinkedList<Class<?>> clazzStack = new LinkedList<Class<?>>();
while (!clazz.getSuperclass().equals(declaringClass)) {
    clazzStack.push(clazz);
    clazz = clazz.getSuperclass();
while (clazz != null) {
    ParameterizedType ptype = (ParameterizedType) clazz.getGenericSuperclass();
...
ClassgetGenericType(Type type, Class rawType, int index)
get Generic Type
if (type instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) type;
    if (rawType.equals(ptype.getRawType())) {
        Type[] typeArguments = ptype.getActualTypeArguments();
        if (index >= typeArguments.length || index < 0) {
            throw new RuntimeException(
                    "index " + (index < 0 ? " must large then 0" : "out of arguments count"));
        return getRawType(typeArguments[index]);
return null;