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

ClassgetGenericParameterType(Class clazz, Integer parameterIndex)
note: for getting actual generic parameter, the object must be instantiated from an anonymouse class (in line class overriding)
Type type = clazz.getGenericSuperclass();
if (!(type instanceof ParameterizedType))
    return null;
ParameterizedType parameterizedType = ((ParameterizedType) clazz.getGenericSuperclass());
Type[] types = parameterizedType.getActualTypeArguments();
type = types[0];
while (type instanceof ParameterizedType) {
    types = ((ParameterizedType) type).getActualTypeArguments();
...
TypegetGenericParameterType0(Class cls, Class genericSuper, int paramIndex)
get Generic Parameter Type
if (!genericSuper.isAssignableFrom(cls))
    return null;
if (genericSuper.isInterface()) {
    for (Type type : cls.getGenericInterfaces()) {
        final Class<?> clazz = getClass(type);
        if (genericSuper.isAssignableFrom(clazz)) {
            if (genericSuper.equals(clazz))
                return type instanceof ParameterizedType
...
Class[]getGenerics(Class classType)
get Generics
Type type = (Type) classType.getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) type;
Type[] types = pt.getActualTypeArguments();
if (types[0] instanceof Class) {
    Class[] classz = new Class[types.length];
    for (int i = 0; i < classz.length; i++) {
        classz[i] = (Class) types[i];
    return classz;
} else {
    return null;
ClassgetGenericsClass(Class cls)
get Generics Class
ParameterizedType superClass = (ParameterizedType) cls.getGenericSuperclass();
Class target = (Class) superClass.getActualTypeArguments()[0];
return target;
Class[]getGenericSuperclass(Class clazz)
get Generic Superclass
try {
    Type typeGeneric = clazz.getGenericSuperclass();
    if (typeGeneric != null) {
        if (typeGeneric instanceof ParameterizedType) {
            return getGeneric((ParameterizedType) typeGeneric);
} catch (Exception e) {
...
TypegetGenericSuperclass(Class cls)
get Generic Superclass
return cls.getGenericSuperclass();
voidgetGenericSuperclassActualTypes(Collection types, Class aClass)
get Generic Superclass Actual Types
if (aClass != null && types != null) {
    Type superclass = aClass.getGenericSuperclass();
    if (superclass instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) superclass;
        Type[] interfaces = parameterizedType.getActualTypeArguments();
        types.addAll(Arrays.asList(interfaces));
    } else if (superclass instanceof Class) {
        Class sClass = (Class) superclass;
...
ClassgetGenericSuperType(Class clz, int index)
get Generic Super Type
Class[] clzs = getAllGenericSuperType(clz);
if (index < 0 || index > clzs.length - 1)
    return Object.class;
return clzs[index];
TypegetGenericSuperType(Class subclass, Class classWithParameter)
returns either the generic superclass or a generic interface, but only when it is assignable from the typed classWithParameter argument
if (subclass.getSuperclass() != null && classWithParameter.isAssignableFrom(subclass.getSuperclass())) {
    return subclass.getGenericSuperclass();
} else {
    int i = 0;
    for (Class in : subclass.getInterfaces()) {
        if (classWithParameter.isAssignableFrom(in)) {
            return subclass.getGenericInterfaces()[i];
        i++;
    return null;
TypegetGenericSuperType(Class class1, Class class2)
for example when class1 is EpochDateConverter which implements TypeConverter, and class2 is TypeConverter, the return value is Date.class
if (class1 == null || class2 == null)
    throw new RuntimeException("null value");
List<Type> genericSupers = new ArrayList<Type>();
genericSupers.add(class1.getGenericSuperclass());
genericSupers.addAll(newArrayList(class1.getGenericInterfaces()));
for (Type type : genericSupers) {
    if (type instanceof ParameterizedTypeImpl) {
        ParameterizedTypeImpl a = (ParameterizedTypeImpl) type;
...