List of utility methods to do Reflection Generic Type from Field
Class> | getGenericType(Field field) Similar to #getGenericType(Type[]) but only for fields return (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
|
Class> | getGenericType(Field field) Returns the generic type of an field. Type type = field.getGenericType();
return getGenericType(type);
|
Class> | getGenericType(Field field) get Generic Type ParameterizedType type = (ParameterizedType) field.getGenericType();
return (Class<?>) type.getActualTypeArguments()[0];
|
Type | getGenericType(Type type, Type fieldType) get Generic Type if (type instanceof ParameterizedType) { Class<?> klass = getTypeClass(type); if (fieldType instanceof TypeVariable) { TypeVariable<?>[] typeVariables = klass.getTypeParameters(); for (int i = 0; i < typeVariables.length; i++) { TypeVariable<?> typeVariable = typeVariables[i]; if (typeVariable.equals(fieldType)) { fieldType = ((ParameterizedType) type).getActualTypeArguments()[i]; ... |
List | getGenericTypeArguments(final Field field) Attempts to return the actual generic type arguments of a field. final List<Class<?>> genericTypeArguments = new ArrayList<Class<?>>(); if (field.getGenericType() instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); for (final Type type : parameterizedType.getActualTypeArguments()) { if (type instanceof Class) { final Class<?> genericTypeArgument = (Class<?>) type; genericTypeArguments.add(genericTypeArgument); return genericTypeArguments; |
Class> | getGenericTypeOfCollectionField(Field field) get Generic Type Of Collection Field Type genericType = field.getGenericType(); if (!(genericType instanceof ParameterizedType)) { throw new IllegalArgumentException("Field is not a ParameterizedType."); Type[] genericTypes = ((ParameterizedType) genericType).getActualTypeArguments(); if (genericTypes.length != 1) { throw new IllegalArgumentException("Field does not have a single generic type."); if (!(genericTypes[0] instanceof Class)) { throw new IllegalArgumentException("Field's generic type is not a class?"); return (Class<?>) genericTypes[0]; |
Class[] | getGenericTypes(Field field) Returns the list of generic types associated to the provided field (if any). try { ParameterizedType type = (ParameterizedType) field.getGenericType(); Type[] types = type.getActualTypeArguments(); Class<?>[] res = new Class<?>[types.length]; for (int i = 0; i < types.length; i++) { res[i] = (Class<?>) types[i]; return res; ... |
Set | getGenericTypes(Field field) get Generic Types if (field.getGenericType() instanceof ParameterizedType) { return getGenericTypes((ParameterizedType) field.getGenericType()); return new HashSet<>(); |
Type[] | getGenericTypes(Field field) get Generic Types Type type = field.getGenericType(); if (type == null) { return null; if (type instanceof ParameterizedType) { ParameterizedType tc = (ParameterizedType) type; return tc.getActualTypeArguments(); return null; |
Class>[] | getGenericTypes(Field field) Returns the generic type information of an attribute. Type genericFieldType = field.getGenericType(); if (!(genericFieldType instanceof ParameterizedType)) return null; ParameterizedType pType = (ParameterizedType) genericFieldType; Type[] args = pType.getActualTypeArguments(); Class<?>[] types = new Class[args.length]; System.arraycopy(args, 0, types, 0, args.length); return types; ... |