List of utility methods to do Reflection Generic Type from Field
Class> | getGenericMultivalueType(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; |
Class | getGenericParameterClass(Field field) get Generic Parameter Class Type genericType = field.getGenericType();
return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
|
Class | getGenericParameterClass(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; ... |
Class | getGenericReturnType(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); |
Class> | getGenericsTypeFromCollectionField(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; |
Class> | getGenericType(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; ... |
Class | getGenericType(Field field) get Generic Type if (!Collection.class.isAssignableFrom(field.getType())) { return field.getType(); ParameterizedType type = (ParameterizedType) field.getGenericType(); return (Class) type.getActualTypeArguments()[0]; |
Type | getGenericType(Field field) get Generic Type Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (type instanceof WildcardType) { return ((WildcardType) type).getUpperBounds()[0]; } else { return type; |