溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring中如何獲取泛型信息的技巧

發(fā)布時(shí)間:2020-10-27 10:36:23 來(lái)源:億速云 閱讀:205 作者:小新 欄目:編程語(yǔ)言

Spring中如何獲取泛型信息的技巧?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

獲取泛型

自己解析

我們之前的處理方式,代碼來(lái)源 vjtools(江南白衣)。

/**
 * 通過(guò)反射, 獲得Class定義中聲明的父類(lèi)的泛型參數(shù)的類(lèi)型.
 * 
 * 注意泛型必須定義在父類(lèi)處. 這是唯一可以通過(guò)反射從泛型獲得Class實(shí)例的地方.
 * 
 * 如無(wú)法找到, 返回Object.class.
 * 
 * 如public UserDao extends HibernateDao<User,Long>
 * 
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic declaration, start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
public static Class getClassGenericType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

    if ((index >= params.length) || (index < 0)) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

ResolvableType 工具

從 Spring 4.0 開(kāi)始 Spring 中添加了 ResolvableType 工具,這個(gè)類(lèi)可以更加方便的用來(lái)回去泛型信息。
首先我們來(lái)看看官方示例:

private HashMap<Integer, List<String>> myMap;

public void example() {
    ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
    t.getSuperType(); // AbstractMap<Integer, List<String>>
    t.asMap(); // Map<Integer, List<String>>
    t.getGeneric(0).resolve(); // Integer
    t.getGeneric(1).resolve(); // List
    t.getGeneric(1); // List<String>
    t.resolveGeneric(1, 0); // String
}

詳細(xì)說(shuō)明

構(gòu)造獲取 Field 的泛型信息

ResolvableType.forField(Field)

構(gòu)造獲取 Method 的泛型信息

ResolvableType.forMethodParameter(Method, int)

構(gòu)造獲取方法返回參數(shù)的泛型信息

ResolvableType.forMethodReturnType(Method)

構(gòu)造獲取構(gòu)造參數(shù)的泛型信息

ResolvableType.forConstructorParameter(Constructor, int)

構(gòu)造獲取類(lèi)的泛型信息

ResolvableType.forClass(Class)

構(gòu)造獲取類(lèi)型的泛型信息

ResolvableType.forType(Type)

構(gòu)造獲取實(shí)例的泛型信息

ResolvableType.forInstance(Object)

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)Spring中如何獲取泛型信息的技巧大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI