2

当然,我对所有这些 Java 东西都很陌生,所以我有一个问题,我正在尝试反序列化在 WCF 服务上获得的响应,一切正常,但是,我正在尝试制作一个通用函数来做到这一点.

基本上我所做的是

public List<msg> GetService(String method){
    List<msg> response = new ArrayList<msg>();

    Type msgType = new TypeToken<List<msg>>(){}.getType();

    //Obtaining result
    response = uJSON.fromJson(serviceResponse, msgType);
    //uJSON is an instance of Gson library, for deserializing it just needs
    //the service response and a Class<T> or Type to reflect the obtained message
}

我想要做的是获得类型“msg”泛型,这意味着......

public <thing> void GetInstanceService(String method){
     List<thing> response = new ArrayList<thing>();

     Type rType2 = new TypeToken<List<thing>>(){}.getType(); //Got java.util.List<thing>

     //And when I'm trying to deserialize I just obtain a List of object 
     //[java.lang.Object@5c7a987e, java.lang.Object@74b1a7a0]

     type2 = uJSON.fromJson(new String(entity), rType2);
}

但我是这样打电话的。

comm.<msgType>GetInstanceService("listTestType");

所以,当我调用“GetInstanceService”时,“thing”是“msgType”类型, List<thing>并且响应不应该List<msgType>List <Object>

此外,当我试图通过“类型”参数显式传递类型时,它只会导致我这样的编译时间错误。

public void GetInstanceService(Type type){
    List<type> type2 = new ArrayList<type>();  //Compilation time error

    //Or
    msgType oType = new msgType();
    Class classType = oType.getClass();
    List<classType> type3;    //Compilation time error
}

那么,如果这些尝试都无效,我该如何设置反序列化的类型呢?

4

2 回答 2

3

Guava 类TypeToken不支持这种使用模式。您正在使用类型变量创建类型标记,并且没有足够的信息可以List<String>List<T>. 您应该创建一个TypeToken拥有所有必需编译时信息的实例。

文档说:

请注意,实际类型参数由子类携带是至关重要的。下面的代码是错误的,因为它只捕获了方法签名的<T> 类型变量;listType()while<String>在擦除中丢失:

class Util {
  static <T> TypeToken<List<T>> listType() {
    return new TypeToken<List<T>>() {};
  }
}

TypeToken<List<String>> stringListType = Util.<String>listType();

但是如上所述,您可以实例化TypeTokenat 调用站点,其中所有类型信息都可用,然后将其作为参数传递。像这样的东西:

public <thing> void GetInstanceService(String method, TypeToken<List<thing>> token){
     List<thing> response = new ArrayList<thing>();

     Type rType2 = token.getType();

     type2 = uJSON.fromJson(new String(entity), rType2);
}

comm.GetInstanceService("listTestType", new TypeToken<List<msgType>>() {});

更新

Paul Bellora 指出,您还可以接受一个参数TypeToken<thing> token,并从中构造一个TypeToken<List<thing>>内部方法token

public <thing> void GetInstanceService(String method, TypeToken<thing> token) {
     List<thing> response = new ArrayList<thing>();

     Type rType2 = new TypeToken<List<thing>>() {}
         .where(new TypeParameter<thing>() {}, token); // where() binds "thing" to token
         .getType();

     type2 = uJSON.fromJson(new String(entity), rType2);
}

comm.GetInstanceService("listTestType", new TypeToken<msgType>() {});
于 2012-12-30T22:00:11.960 回答
2

由于所谓的类型擦除,您需要的类对象在运行时不可用。

但是,有一个标准的解决方法:将类型标记传递给您的方法,如下所示:

public <T> List<T> getService(String method, Class<T> c) {
    // the caller has passed in the class object
    List<T> list = new ArrayList<T>();
    // fill list
    return list;
}
于 2012-12-30T21:29:55.067 回答