1. 程式人生 > >獲取通過反射獲取泛型物件

獲取通過反射獲取泛型物件

public static <T> T injectT(Object object, int position)
{
    try
    {
        return ((Class<T>) ((ParameterizedType) (object.getClass().getGenericSuperclass())).getActualTypeArguments()
                [position]).newInstance();
    } catch (InstantiationException e)
    {
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        e.printStackTrace();
    } catch (ClassCastException e)
    {
        e.printStackTrace();
    }
    return null;
}