Gson非法类型变量引用
Gson illegal type variable reference
我有一个 class 定义为:
public class Calls<T extends Object>
在此 class 中有一个方法 doRequest
为 Gson 请求创建 TypeToken
(来自 com.google.json.reflect
)。
java.lang.reflect.Type type = new TypeToken<HandlerResponse<T>>(){{}}.getType();
其中 HandlerResponse
是一个简单模型 class,它包含如下属性:
private Map detail;
private transient T data;
private transient int count = 0;
private String status;
我在 Android Studio 上遇到的例外情况是:
FATAL EXCEPTION: main
Process: PID: 32628
java.lang.AssertionError: illegal type variable reference
at libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111)
at libcore.reflect.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:125)
at libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47)
at java.util.Arrays.hashCode(Arrays.java:4153)
at com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl.hashCode($Gson$Types.java:479)
at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:64)
at com.company.server.Calls.<init>(Calls.java:244)
在 TypeToken 实例化时崩溃(我在想可能是因为 Java 的类型擦除而丢失了 T class)。
我将 Calls
的实例创建为:
Calls<com.company.model.beans.Model> calls = new Calls<>(){};
已解决,我修改实现如下:
public class Calls<T> {
public Calls(Type type, Class classTypeResponse) {
this.type = type;
this.classTypeResponse = classTypeResponse;
}
doRequest(...) { ... }
...
}
我有一个 classTypeResponse
,因为我有一个回调系统,returns 请求对象的正确 class 类型。
我是这样称呼的:
Type type = new TypeToken<HandlerResponse<com.company.model.beans.Model>>(){}.getType();
Calls<com.company.model.beans.Model> calls = new Calls<>(type, com.company.model.beans.Model.class);
calls.doRequest(...);
T
在运行时不存在,Java 反射系统无法为 TypeToken
推断出正确的类型。解决方案是创建不带泛型的 TypeToken
并在需要的地方传递对象。
我有一个 class 定义为:
public class Calls<T extends Object>
在此 class 中有一个方法 doRequest
为 Gson 请求创建 TypeToken
(来自 com.google.json.reflect
)。
java.lang.reflect.Type type = new TypeToken<HandlerResponse<T>>(){{}}.getType();
其中 HandlerResponse
是一个简单模型 class,它包含如下属性:
private Map detail;
private transient T data;
private transient int count = 0;
private String status;
我在 Android Studio 上遇到的例外情况是:
FATAL EXCEPTION: main
Process: PID: 32628
java.lang.AssertionError: illegal type variable reference
at libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111)
at libcore.reflect.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:125)
at libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47)
at java.util.Arrays.hashCode(Arrays.java:4153)
at com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl.hashCode($Gson$Types.java:479)
at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:64)
at com.company.server.Calls.<init>(Calls.java:244)
在 TypeToken 实例化时崩溃(我在想可能是因为 Java 的类型擦除而丢失了 T class)。
我将 Calls
的实例创建为:
Calls<com.company.model.beans.Model> calls = new Calls<>(){};
已解决,我修改实现如下:
public class Calls<T> {
public Calls(Type type, Class classTypeResponse) {
this.type = type;
this.classTypeResponse = classTypeResponse;
}
doRequest(...) { ... }
...
}
我有一个 classTypeResponse
,因为我有一个回调系统,returns 请求对象的正确 class 类型。
我是这样称呼的:
Type type = new TypeToken<HandlerResponse<com.company.model.beans.Model>>(){}.getType();
Calls<com.company.model.beans.Model> calls = new Calls<>(type, com.company.model.beans.Model.class);
calls.doRequest(...);
T
在运行时不存在,Java 反射系统无法为 TypeToken
推断出正确的类型。解决方案是创建不带泛型的 TypeToken
并在需要的地方传递对象。