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

ClassgetGenericArgument(Field field, int index)
get Generic Argument
final Class entryType;
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
    Type[] typeArgs = ((ParameterizedType) genericType).getActualTypeArguments();
    if (typeArgs != null && typeArgs.length > index) {
        if (typeArgs[index] instanceof Class) {
            entryType = (Class) typeArgs[index];
        } else {
...
ClassgetGenericClass(Field f, int n)
get Generic Class
Type genericFieldType = f.getGenericType();
if (genericFieldType instanceof ParameterizedType) {
    ParameterizedType aType = (ParameterizedType) genericFieldType;
    Type[] fieldArgTypes = aType.getActualTypeArguments();
    return (Class<?>) fieldArgTypes[n];
return null;
Class[]getGenericClasses(Field field)
get Generic Classes
return getGenericClasses(field.getGenericType());
ClassgetGenericElementType(Field field)
Get the element type for a Collection if possible using the JDK 1.5 generics API or null if not possible.
return getType(field, 0);
ClassgetGenericFieldClassType(Class clz, String propertyName)
get Generic Field Class Type
Field field = clz.getDeclaredField(propertyName);
field.setAccessible(true);
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
Class<?> genericClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
return genericClass;
ClassgetGenericFieldType(Field field, boolean isAllowNull)
Returns the generic type parameter of a return value by a field.
Type t = field.getGenericType();
Class result = getGenericType(t, 0);
if (!isAllowNull && result == null) {
    return Object.class;
return result;
TypegetGenericFieldType(Object target, String fieldName)
get Generic Field Type
try {
    return getField(target, fieldName).getGenericType();
} catch (NoSuchFieldException e) {
    throw new RuntimeException(e);
ClassgetGenericFieldTypeFromPosition(Field field, int position)
get Generic Field Type From Position
try {
    Type genericType = field.getGenericType();
    ParameterizedType type = (ParameterizedType) genericType;
    return (Class<?>) type.getActualTypeArguments()[position];
} catch (Exception e) {
    return null;
intgetGenericlyTypeCount(Field field)
get Genericly Type Count
if (field.getGenericType() instanceof ParameterizedType) {
    ParameterizedType type = (ParameterizedType) field.getGenericType();
    return type.getActualTypeArguments().length;
return 0;
ClassgetGenericMultivalueType(final Field p)
Returns the generic class of multi-value objects.
if (p.getType() instanceof Class<?>) {
    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 (((Class<?>) pt.getRawType()).isAssignableFrom(Map.class)) {
                if (pt.getActualTypeArguments()[1] instanceof Class<?>) {
                    return (Class<?>) pt.getActualTypeArguments()[1];
...