get Parameterized Type Arguments from Field - Java Reflection

Java examples for Reflection:Generic

Description

get Parameterized Type Arguments from Field

Demo Code


import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embedded;

public class Main{
    public static List<Class<?>> getParameterizedTypeArguments(Field field) {
        try {/*from  w  ww.  j av a  2s.c o m*/
            ParameterizedType t = (ParameterizedType) field
                    .getGenericType();
            Type[] args = t.getActualTypeArguments();
            ArrayList<Class<?>> classes = new ArrayList<>();
            for (Type arg : args)
                classes.add((Class<?>) arg);
            return classes;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials