Here you can find the source of getGenericParameters(Field f)
Parameter | Description |
---|---|
f | field to get generic parameters from |
Parameter | Description |
---|---|
IllegalArgumentException | if f's type is not parameterized |
public static Class[] getGenericParameters(Field f)
//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"); } } }