Java Utililty Methods Reflection Generic Type from Field

List of utility methods to do Reflection Generic Type from Field

Description

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

Method

ClassgetGenericMultivalueType(final Field p)
Returns the generic class of multi-value objects.
final Type genericType = p.getGenericType();
if (genericType != null && genericType instanceof ParameterizedType) {
    final ParameterizedType pt = (ParameterizedType) genericType;
    if (pt.getActualTypeArguments() != null && pt.getActualTypeArguments().length > 0) {
        if (pt.getActualTypeArguments()[0] instanceof Class<?>) {
            return (Class<?>) pt.getActualTypeArguments()[0];
return null;
ClassgetGenericParameterClass(Field field)
get Generic Parameter Class
Type genericType = field.getGenericType();
return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
ClassgetGenericParameterClass(Field field)
get Generic Parameter Class
if (!hasGenericParameter(field)) {
    return null;
Type genericType = field.getGenericType();
return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
Class[]getGenericParameters(Field f)
get Generic Parameters
Type type = f.getGenericType();
if (type instanceof ParameterizedType) {
    return Arrays.stream(((ParameterizedType) type).getActualTypeArguments())
            .map(t -> t instanceof Class ? (Class) t : null).toArray(Class[]::new);
} else {
    throw new IllegalArgumentException(f + " is not of a parameterized type");
Class[]getGenericParametersInternal(Type genericFieldType)
get Generic Parameters Internal
if (genericFieldType instanceof ParameterizedType) {
    ParameterizedType aType = (ParameterizedType) genericFieldType;
    Type[] fieldArgTypes = aType.getActualTypeArguments();
    Class<?>[] clazzes = new Class<?>[fieldArgTypes.length];
    for (int i = 0; i < clazzes.length; i++) {
        clazzes[i] = (Class<?>) fieldArgTypes[i];
    return clazzes;
...
ClassgetGenericReturnType(Method method, Field field, boolean isAllowNull)
Returns the generic type parameter of a return value by a field or method.
if (method == null) {
    return getGenericFieldType(field, isAllowNull);
} else {
    return getGenericReturnType(method, isAllowNull);
ClassgetGenericsTypeFromCollectionField(Field field)
Gets generics info despite runtime type erasure.
Class<?> clazz = null;
if (List.class.isAssignableFrom(field.getType()) || Set.class.isAssignableFrom(field.getType())) {
    field.setAccessible(true);
    ParameterizedType ptype = (ParameterizedType) field.getGenericType();
    Type[] types = ptype.getActualTypeArguments();
    if (types != null && types.length > 0) {
        clazz = (Class<?>) types[0];
return clazz;
ClassgetGenericType(Field field)
Gets generic type from field (if field type is ParameterizedType )
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
    Type firstParameter = ((ParameterizedType) type).getActualTypeArguments()[0];
    if (!(firstParameter instanceof WildcardType)) {
        return (Class<?>) firstParameter;
return null;
...
ClassgetGenericType(Field field)
get Generic Type
if (!Collection.class.isAssignableFrom(field.getType())) {
    return field.getType();
ParameterizedType type = (ParameterizedType) field.getGenericType();
return (Class) type.getActualTypeArguments()[0];
TypegetGenericType(Field field)
get Generic Type
Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (type instanceof WildcardType) {
    return ((WildcardType) type).getUpperBounds()[0];
} else {
    return type;