最近在使用Google的Gson包進(jìn)行Json和Java對象之間的轉(zhuǎn)化,對于包含泛型的類的序列化和反序列化Gson也提供了很好的支持,感覺有點(diǎn)意思,就花時間研究了一下。
由于Java泛型的實現(xiàn)機(jī)制,使用了泛型的代碼在運(yùn)行期間相關(guān)的泛型參數(shù)的類型會被擦除,我們無法在運(yùn)行期間獲知泛型參數(shù)的具體類型(所有的泛型類型在運(yùn)行時都是Object類型)。
但是有的時候,我們確實需要獲知泛型參數(shù)的類型,比如將使用了泛型的Java代碼序列化或者反序列化的時候,這個時候問題就變得比較棘手。
1 2 3 4 5 6 7 8 | class Foo<T> {
T value;
}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar
|
對于上面的類Foo<T>,由于在運(yùn)行期間無法得知T的具體類型,對這個類的對象進(jìn)行序列化和反序列化都不能正常進(jìn)行。Gson通過借助TypeToken類來解決這個問題。
1 2 3 4 5 6 7 8 | TestGeneric<String> t = new TestGeneric<String>();
t.setValue( "Alo" );
Type type = new TypeToken<TestGeneric<String>>(){}.getType();
String gStr = GsonUtils.gson.toJson(t,type);
System.out.println(gStr);
TestGeneric t1 = GsonUtils.gson.fromJson(gStr, type);
System.out.println(t1.getValue());
|
TypeToken的使用非常簡單,如上面的代碼,只要將需要獲取類型的泛型類作為TypeToken的泛型參數(shù)構(gòu)造一個匿名的子類,就可以通過getType()方法獲取到我們使用的泛型類的泛型參數(shù)類型。
下面來簡單分析一下原理。
要獲取泛型參數(shù)的類型,一般的做法是在使用了泛型的類的構(gòu)造函數(shù)中顯示地傳入泛型類的Class類型作為這個泛型類的私有屬性,它保存了泛型類的類型信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Foo<T>{
public Class<T> kind;
public Foo(Class<T> clazz){
this .kind = clazz;
}
public T[] getInstance(){
return (T[])Array.newInstance(kind, 5 );
}
public static void main(String[] args){
Foo<String> foo = new Foo(String. class );
String[] strArray = foo.getInstance();
}
}
|
這種方法雖然能解決問題,但是每次都要傳入一個Class類參數(shù),顯得比較麻煩。Gson庫里面對于這個問題采用了了另一種解決辦法。
同樣是為了獲取Class的類型,可以通過另一種方式實現(xiàn):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public abstract class Foo<T>{
Class<T> type;
public Foo(){
this .type = (Class<T>) getClass();
}
public static void main(String[] args) {
Foo<String> foo = new Foo<String>(){};
Class mySuperClass = foo.getClass();
}
}
|
聲明一個抽象的父類Foo,匿名子類將泛型類作為Foo的泛型參數(shù)傳入構(gòu)造一個實例,再調(diào)用getClass方法獲得這個子類的Class類型。
這里雖然通過另一種方式獲得了匿名子類的Class類型,但是并沒有直接將泛型參數(shù)T的Class類型傳進(jìn)來,那又是如何獲得泛型參數(shù)的類型的呢, 這要依賴Java的Class字節(jié)碼中存儲的泛型參數(shù)信息。Java的泛型機(jī)制雖然在運(yùn)行期間泛型類和非泛型類都相同,但是在編譯java源代碼成 class文件中還是保存了泛型相關(guān)的信息,這些信息被保存在class字節(jié)碼常量池中,使用了泛型的代碼處會生成一個signature簽名字段,通過 簽名signature字段指明這個常量池的地址。
關(guān)于class文件中存儲泛型參數(shù)類型的具體的詳細(xì)的知識可以參考這里:http://stackoverflow.com/questions/937933/where-are-generic-types-stored-in-java-class-files
JDK里面提供了方法去讀取這些泛型信息的方法,再借助反射,就可以獲得泛型參數(shù)的具體類型。同樣是對于第一段代碼中的foo對象,通過下面的代碼可以得到foo<T>中的T的類型:
1 2 3 | Type mySuperClass = foo.getClass().getGenericSuperclass();
Type type = ((ParameterizedType)mySuperClass).getActualTypeArguments()[ 0 ];
System.out.println(type);
|
運(yùn)行結(jié)果是class java.lang.String。
分析一下這段代碼,Class類的getGenericSuperClass()方法的注釋是:
Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by thisClass.
If the superclass is a parameterized type, the Type object returned must accurately reflect the actual type parameters used in the source code. The parameterized type representing the superclass is created if it had not been created before. See the declaration of ParameterizedType for the semantics of the creation process for parameterized types. If thisClass represents either theObject class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then theClass object representing theObject class is returned
概括來說就是對于帶有泛型的class,返回一個ParameterizedType對象,對于Object、接口和原始類型返回null,對于數(shù) 組class則是返回Object.class。ParameterizedType是表示帶有泛型參數(shù)的類型的Java類型,JDK1.5引入了泛型之 后,Java中所有的Class都實現(xiàn)了Type接口,ParameterizedType則是繼承了Type接口,所有包含泛型的Class類都會實現(xiàn) 這個接口。
實際運(yùn)用中還要考慮比較多的情況,比如獲得泛型參數(shù)的個數(shù)避免數(shù)組越界等,具體可以參看Gson中的TypeToken類及ParameterizedTypeImpl類的代碼。