Java Utililty Methods Reflection Generic Parameter

List of utility methods to do Reflection Generic Parameter

Description

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

Method

Class[]getGeneric(ParameterizedType type)
get Generic
try {
    if (type != null) {
        Type[] typeArgs = type.getActualTypeArguments();
        if (typeArgs != null && typeArgs.length > 0) {
            Class[] args = new Class[typeArgs.length];
            for (int i = 0; i < typeArgs.length; i++) {
                Type arg = typeArgs[i];
                args[i] = (Class) arg;
...
Type[]getGenericParameter(final Type type)
get Generic Parameter
if (ParameterizedType.class.isInstance(type)) {
    return ParameterizedType.class.cast(type).getActualTypeArguments();
if (GenericArrayType.class.isInstance(type)) {
    return getGenericParameter(GenericArrayType.class.cast(type).getGenericComponentType());
return null;
TypegetGenericParameter(final Type type, final int index)
get Generic Parameter
if (!(type instanceof ParameterizedType)) {
    return null;
final Type[] genericParameter = getGenericParameters(type);
if (genericParameter == null) {
    return null;
return genericParameter[index];
...
ListgetGenericParameterList(Type type)
get Generic Parameter List
if (ParameterizedType.class.isInstance(type)) {
    final ParameterizedType paramType = ParameterizedType.class.cast(type);
    return Arrays.asList(paramType.getActualTypeArguments());
if (GenericArrayType.class.isInstance(type)) {
    final GenericArrayType arrayType = GenericArrayType.class.cast(type);
    return getGenericParameterList(arrayType.getGenericComponentType());
@SuppressWarnings("unchecked")
List<Type> emptyList = Collections.EMPTY_LIST;
return emptyList;
Type[]getGenericParameters(final Type type)
get Generic Parameters
if (type instanceof ParameterizedType) {
    return ParameterizedType.class.cast(type).getActualTypeArguments();
if (type instanceof GenericArrayType) {
    return getGenericParameters(GenericArrayType.class.cast(type).getGenericComponentType());
return null;
TypegetGenericParameterType(Method method, int paramIdx)
get Generic Parameter Type
return method.getGenericParameterTypes()[paramIdx];
TypegetGenericParameterType(Type type, int index)
get Generic Parameter Type
if (!ParameterizedType.class.isInstance(type)) {
    return null;
final Type[] genericParameterTypeList = getGenericParameterTypes(type);
if (genericParameterTypeList.length == 0 || genericParameterTypeList.length < index) {
    return null;
return genericParameterTypeList[index];
...
Type[]getGenericParameterTypes(final Type type)
get Generic Parameter Types
if (ParameterizedType.class.isInstance(type)) {
    return ParameterizedType.class.cast(type).getActualTypeArguments();
if (GenericArrayType.class.isInstance(type)) {
    return getGenericParameterTypes(GenericArrayType.class.cast(type).getGenericComponentType());
return EMPTY_TYPES;
Type[]getGenericParameterTypes(Method m)
get Generic Parameter Types
return m.getGenericParameterTypes();
Type[]getGenericParameterTypes(Type t)
get Generic Parameter Types
if (ParameterizedType.class.isInstance(t)) {
    ParameterizedType pt = ParameterizedType.class.cast(t);
    return pt.getActualTypeArguments();
} else
    return null;