Java Utililty Methods Reflection Generic Type

List of utility methods to do Reflection Generic Type

Description

The list of methods to do Reflection Generic Type are organized into topic(s).

Method

Type[]getGenericArguments(Type type)
Returns the generic type argument.
if (type instanceof GenericArrayType) {
    return getGenericArguments(((GenericArrayType) type).getGenericComponentType());
} else if (type instanceof ParameterizedType) {
    ParameterizedType parameters = (ParameterizedType) type;
    return parameters.getActualTypeArguments();
} else {
    return new Class<?>[0];
Type[]getGenericElementType(Type type)
Retrieves type parameter from the parameterized type (either Map or Collection) specification.
if (isGenericType(type)) {
    return ((ParameterizedType) type).getActualTypeArguments();
return null;
ClassgetGenericFirst(Object obj)
get Generic First
Type type = (Type) obj.getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) type;
Class classz = ((Class) pt.getActualTypeArguments()[0]);
return classz;
ListgetGenerics(Type genericType)
Returns the generic types represented in the given type.
List<Type> classes = new ArrayList<Type>();
if (genericType instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) genericType;
    Type[] targs = ptype.getActualTypeArguments();
    for (Type targ : targs) {
        classes.add(targ);
return classes;
Class[]getGenerics(Type t)
get Generics
Class[] generics = null;
if (t instanceof ParameterizedType) {
    ParameterizedType type = (ParameterizedType) t;
    Type[] typeArguments = type.getActualTypeArguments();
    generics = new Class[typeArguments.length];
    for (int i = 0; i < typeArguments.length; ++i) {
        generics[i] = (Class) typeArguments[i];
return generics;
StringgetGenericString(AccessibleObject ao)
Gets the generic string of the passed AccessibleObject
if (ao == null)
    return null;
if (ao instanceof Method) {
    return ((Method) ao).toGenericString();
} else if (ao instanceof Constructor) {
    return ((Constructor<?>) ao).toGenericString();
} else if (ao instanceof Field) {
    Field f = (Field) ao;
...
TypegetGenericSuperType(Type t)
get Generic Super Type
if (t instanceof Class) {
    return ((Class) t).getGenericSuperclass();
} else if (t instanceof ParameterizedType) {
    return getGenericSuperType(((ParameterizedType) t).getRawType());
return null;
ClassgetGenericType(@Nullable Type genericType)
Retrieves the generic subtype of the given type.
if (genericType != null && genericType instanceof ParameterizedType) {
    Type[] types = ((ParameterizedType) genericType).getActualTypeArguments();
    if (types.length > 0 && types[0] instanceof Class<?>) {
        return (Class<?>) types[0];
return null;
ClassgetGenericType(Member member, int index)
Returns the actual generic type of a class's type parameter of the member.
Type type;
if (member instanceof Field) {
    final Field field = (Field) member;
    type = field.getGenericType();
} else {
    final Method method = (Method) member;
    type = method.getGenericReturnType();
if (!(type instanceof ParameterizedType)) {
    return null;
final ParameterizedType parameterizedType = (ParameterizedType) type;
final Type[] types = parameterizedType.getActualTypeArguments();
return (Class<X>) ((types != null) && (index < types.length) ? types[index] : null);
ClassgetGenericType(Method setter)
get Generic Type
if (setter != null) {
    Type[] genericParameterTypes = setter.getGenericParameterTypes();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        if (genericParameterTypes[i] instanceof ParameterizedType) {
            Type[] parameters = ((ParameterizedType) genericParameterTypes[i]).getActualTypeArguments();
            if (parameters != null && parameters.length == 1) {
                return (Class<?>) parameters[0];
return null;