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

TypegetGenericDeclaringType(Class baseClass, Class declaringClass)
get Generic Declaring Type
if (baseClass.equals(declaringClass)) {
    return baseClass;
if (declaringClass.isInterface()) {
    Type[] interfaces = baseClass.getGenericInterfaces();
    for (Type type : interfaces) {
        if (type instanceof ParameterizedType) {
            if (((ParameterizedType) type).getRawType().equals(declaringClass)) {
...
ClassgetGenericFirstClass(Type type)
get Generic First Class
return findGenericClass(type, 0);
TypegetGenericInterface(final Class sourceClass, final Class genericInterface)
get Generic Interface
Type[] types = sourceClass.getGenericInterfaces();
for (Type type : types) {
    if (type instanceof Class) {
        if (genericInterface.isAssignableFrom((Class) type)) {
            return type;
    } else if (type instanceof ParameterizedType) {
        if (genericInterface.isAssignableFrom((Class) ((ParameterizedType) type).getRawType())) {
...
TypegetGenericInterfaceParamType(Class cls, Class rawType)
Returns the Type of parameter of the generic interface of the class.
while (cls != null) {
    Type[] interfaces = cls.getGenericInterfaces();
    for (Type type : interfaces) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            if (pType.getRawType() == rawType) {
                return pType.getActualTypeArguments()[0];
            } else {
...
voidgetGenericInterfacesActualTypes(Collection types, Class aClass)
get Generic Interfaces Actual Types
if (aClass != null && types != null) {
    Type[] interfaces = aClass.getGenericInterfaces();
    for (Type anInterface : interfaces) {
        if (anInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) anInterface;
            Type[] actualTypes = parameterizedType.getActualTypeArguments();
            types.addAll(Arrays.asList(actualTypes));
        } else if (anInterface instanceof Class) {
...
ClassgetGenericParameter(Class clazz, int index)
get Generic Parameter
Type genType = clazz.getGenericSuperclass();
if (genType instanceof ParameterizedType) {
    return getGenericParameter((ParameterizedType) genType, index);
return null;
ClassgetGenericParameter(Type class1, int number)
Extract generic parameter from super class
ParameterizedType type = (ParameterizedType) class1;
Type arg = type.getActualTypeArguments()[number];
if (arg instanceof Class) {
    return (Class) arg;
} else if (arg instanceof ParameterizedType) {
    return (Class) ((ParameterizedType) arg).getRawType();
} else {
    throw new UnsupportedOperationException();
...
ClassgetGenericParameterClass(Class actualClass, int parameterIndex)
get Generic Parameter Class
return (Class) ((ParameterizedType) actualClass.getGenericSuperclass())
        .getActualTypeArguments()[parameterIndex];
StringgetGenericParameterClass(Class clazz)
Gets the first generic between class parents and return's its first parameter type
Type type = clazz.getGenericSuperclass();
while (!(type instanceof ParameterizedType)) {
    clazz = clazz.getSuperclass();
    if (clazz == null) {
        return null;
    type = clazz.getGenericSuperclass();
String genericTypeName = ((ParameterizedType) type).getActualTypeArguments()[0].getTypeName();
return genericTypeName;
Class[]getGenericParameters(Class clazz)
get Generic Parameters
return getGenericParametersInternal(clazz.getGenericSuperclass());