2
データの逆シリアル化のための汎用メソッドを作成しようとしています。Java汎用メソッドが期待通りに機能しない
マイコード:
public <T> ExportedData<T> getExportData(T classType, String exportUri) {
Response response = _client.get(exportUri);
// System.out.println(response.body.toString());
ExportedData<T> exportedData = GsonSingleton.getGson().fromJson(response.body.toString(), new TypeToken<ExportedData<T>>() {
}.getType());
return exportedData;
}
response.body
:
{"totalResults":2,"limit":50000,"offset":0,"count":2,"hasMore":false,"items":[{"DevicesIDs":"","EmailAddress":"[email protected]"},{"DevicesIDs":"","EmailAddress":"[email protected]"}]}
私はジェネリックメソッドを呼び出す方法:
ExportedData<AccengageOutboundContact> exportedData = generalBulkHelper.getExportData(new AccengageOutboundContact(), uriLimitAndOffset);
AccengageOutboundContact
:
public class AccengageOutboundContact {
public String EmailAddress;
public String DevicesIDs;
}
0を
そしてExportedData
:
public class ExportedData<T> {
public int totalResults;
public int limit;
public int offset;
public int count;
public boolean hasMore;
public List<T> items;
}
私はAccengageOutboundContact
オブジェクトのArrayListのを得ることを期待します。私が得ているのは、ArrayList of StringMap
です。
私は間違って何を考えていますか?
はそれをしようとする、ありがとう、しかし、なぜTがに消去されオブジェクト? –
[これはJavaのジェネリックスの仕組み](http://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens)です。 – Kayaman
Tは常にオブジェクトに消去されると言っていますか?もしそうなら、私にはジェネリックメソッドが失われています。オブジェクトを使用して、必要なものだけをキャストできます。 –