溫馨提示×

溫馨提示×

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

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

Java如何使用反射獲取list泛型

發(fā)布時間:2023-02-28 10:07:39 來源:億速云 閱讀:162 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java如何使用反射獲取list泛型”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“Java如何使用反射獲取list泛型”文章能幫助大家解決問題。

通過屬性來獲取泛型的類型

Field[] fields = bean.getClass().getDeclaredFields();
for(Field f : fields){
	f.setAccessible(true);
	if(f.getType() == java.util.List.class){
		// 如果是List類型,得到其Generic的類型  
		Type genericType = f.getGenericType(); 
		if(genericType == null) continue;  
		// 如果是泛型參數(shù)的類型   
		if(genericType instanceof ParameterizedType){   
			ParameterizedType pt = (ParameterizedType) genericType;
			//得到泛型里的class類型對象  
			Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0]; 
		}   
	}

通過class對象來獲取泛型類型

Type type = getClass().getGenericSuperclass();
		System.err.println("generic super class type:" + type);
		Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
		//trueType就是泛型的真實類型

一、getSuperclass 返回直接繼承的父類(由于編譯擦除,沒有顯示泛型參數(shù))

二、getGenericSuperclass 返回直接繼承的父類(包含泛型參數(shù))

返回表示此 Class 所表示的實體(類、接口、基本類型或 void)的直接超類的 Type。

如果超類是參數(shù)化類型,則返回的 Type 對象必須準(zhǔn)確反映源代碼中所使用的實際類型參數(shù)。如果以前未曾創(chuàng)建表示超類的參數(shù)化類型,則創(chuàng)建這個類型。有關(guān)參數(shù)化類型創(chuàng)建過程的語義,請參閱 ParameterizedType 聲明。

如果此 Class 表示 Object 類、接口、基本類型或 void,則返回 null。

如果此對象表示一個數(shù)組類,則返回表示 Object 類的 Class 對象。

列如

package cn.test;
public class Test {
    public static void main(String[] args) {
        System.out.println("Student.class.getSuperclass()\t"                             + Student.class.getSuperclass());
        System.out.println("Student.class.getGenericSuperclass()\t"                            + Student.class.getGenericSuperclass());
        System.out.println("Test.class.getSuperclass()\t"                             + Test.class.getSuperclass());
        System.out.println("Test.class.getGenericSuperclass()\t"                            + Test.class.getGenericSuperclass());
        System.out.println("Object.class.getGenericSuperclass()\t"                             + Object.class.getGenericSuperclass());
        System.out.println("Object.class.getSuperclass()\t"                             + Object.class.getSuperclass());
        System.out.println("void.class.getSuperclass()\t"                             + void.class.getSuperclass());
        System.out.println("void.class.getGenericSuperclass()\t"                             + void.class.getGenericSuperclass());
        System.out.println("int[].class.getSuperclass()\t"                             + int[].class.getSuperclass());
        System.out.println("int[].class.getGenericSuperclass()\t"                             + int[].class.getGenericSuperclass());
    }
}
class Person<T> {
}
class Student extends Person<Test> {
}

輸出結(jié)果:

Student.class.getSuperclass() class cn.test.Person
Student.class.getGenericSuperclass() cn.test.Person<cn.test.Test>
Test.class.getSuperclass() class java.lang.Object
Test.class.getGenericSuperclass() class java.lang.Object
Object.class.getGenericSuperclass() null
Object.class.getSuperclass() null
void.class.getSuperclass() null
void.class.getGenericSuperclass() null
int[].class.getSuperclass() class java.lang.Object
int[].class.getGenericSuperclass() class java.lang.Object

關(guān)于“Java如何使用反射獲取list泛型”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。

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

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

AI