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

Type[]getGenericTypes(Class clazz)
get Generic Types
Class<?> myClass = clazz;
if (!(clazz.getGenericSuperclass() instanceof ParameterizedType)) {
    myClass = clazz.getSuperclass();
Type superClass = myClass.getGenericSuperclass();
ParameterizedType type = (ParameterizedType) superClass;
return type.getActualTypeArguments();
Type[]getGenericTypes(final Class iClass)
Returns the declared generic types of a class.
final Type genericType = iClass.getGenericInterfaces()[0];
if (genericType != null && genericType instanceof ParameterizedType) {
    final ParameterizedType pt = (ParameterizedType) genericType;
    if (pt.getActualTypeArguments() != null && pt.getActualTypeArguments().length > 1)
        return pt.getActualTypeArguments();
return null;
voidgetGenericTypesImpl(ParameterizedType ptype, List> classes)
get Generic Types Impl
for (Type t : ptype.getActualTypeArguments()) {
    if (t instanceof Class) {
        classes.add((Class<?>) t);
    } else if (t instanceof ParameterizedType) {
        ParameterizedType next = (ParameterizedType) t;
        classes.add((Class<?>) next.getRawType());
        getGenericTypesImpl(next, classes);
return;