List of utility methods to do Reflection Generic Type from Field
Class | getGenericArgument(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 { ... |
Class> | getGenericClass(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());
|
Class | getGenericElementType(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);
|
Class> | getGenericFieldClassType(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;
|
Class | getGenericFieldType(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; |
Type | getGenericFieldType(Object target, String fieldName) get Generic Field Type try { return getField(target, fieldName).getGenericType(); } catch (NoSuchFieldException e) { throw new RuntimeException(e); |
Class> | getGenericFieldTypeFromPosition(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; |
int | getGenericlyTypeCount(Field field) get Genericly Type Count if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) field.getGenericType(); return type.getActualTypeArguments().length; return 0; |
Class> | getGenericMultivalueType(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]; ... |