Java Reflection Generic Type from Field getGenericParameters(Field f)

Here you can find the source of getGenericParameters(Field f)

Description

get Generic Parameters

License

Open Source License

Parameter

Parameter Description
f field to get generic parameters from

Exception

Parameter Description
IllegalArgumentException if f's type is not parameterized

Return

generic parameters of f 's type as Class es, with non- Class parameters replaced with null

Declaration

public static Class[] getGenericParameters(Field f) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;

public class Main {
    /**//w ww  .j  av a 2 s.  c o m
     * @param f field to get generic parameters from
     * @return generic parameters of {@code f}'s type as {@code Class}es, with non-{@code Class} parameters replaced with {@code null}
     * @throws IllegalArgumentException if {@code f}'s type is not parameterized
     */
    public static Class[] getGenericParameters(Field f) {
        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");
        }
    }
}

Related

  1. getGenericlyTypeCount(Field field)
  2. getGenericMultivalueType(final Field p)
  3. getGenericMultivalueType(final Field p)
  4. getGenericParameterClass(Field field)
  5. getGenericParameterClass(Field field)
  6. getGenericParametersInternal(Type genericFieldType)
  7. getGenericReturnType(Method method, Field field, boolean isAllowNull)
  8. getGenericsTypeFromCollectionField(Field field)
  9. getGenericType(Field field)