关于 TypeReference 的解释
原文地址:https://www.cnblogs.com/cxygg/p/9473506.html
在使用fastJson时,对于泛型的 反序列化 很多场景下都会使用到TypeReference
使用TypeReference可以明确的指定反序列化的类型,具体实现逻辑参考TypeReference的构造函数
首先 TypeReference 是描述 一个复杂 泛型的工具类。
TypeReference 很多类库都有,用 fastjson 的 举例,大概就这个意思。
例子:
Response response = JSONObject.parseObject(result, new TypeReference<Response>() {});
new TypeReference<Response>() {} 描述的是一个 这个样的 类: Response,同理,可以更多层的嵌套泛型。
当 TypeReference 的泛型参数是 泛型变量的时候。可用使用 参数来修饰泛型变量。
我们看看 TypeReference 构造方法的源码:
protected TypeReference(){
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
Type cachedType = classTypeCache.get(type);
if (cachedType == null) {
classTypeCache.putIfAbsent(type, type);
cachedType = classTypeCache.get(type);
}
this.type = cachedType;
}
/**
* @since 1.2.9
* @param actualTypeArguments
*/
protected TypeReference(Type… actualTypeArguments){
Class<?> thisClass = this.getClass();
Type superClass = thisClass.getGenericSuperclass();
ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0];
Type rawType = argType.getRawType();
Type[] argTypes = argType.getActualTypeArguments();
int actualIndex = 0;
for (int i = 0; i < argTypes.length; ++i) {
if (argTypes[i] instanceof TypeVariable &&
actualIndex < actualTypeArguments.length) {
argTypes[i] = actualTypeArguments[actualIndex++];
}
// fix for openjdk and android env
if (argTypes[i] instanceof GenericArrayType) {
argTypes[i] = TypeUtils.checkPrimitiveArray(
(GenericArrayType) argTypes[i]);
}
}
Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType);
Type cachedType = classTypeCache.get(key);
if (cachedType == null) {
classTypeCache.putIfAbsent(key, key);
cachedType = classTypeCache.get(key);
}
type = cachedType;
}
上面的 无参 构造方法 里面 获取了 参数泛型 (ParameterizedType),后面的 有差 构造方法 用参数 替换了 参数泛型中是 泛型变量 里面的 内容。
例子:Response response = JSONObject.parseObject(result, new TypeReference<Response>( respDatacls ) {});
等价于:Response response = JJSONObject.parseObject(result, new TypeReference<Response>( ) {})
如果要用泛型变量 ,有参数的写法就相当必要了。
备注:上面的 respDatacls = TaskCodeRespData.class
备注:如果 T 是 泛型变量 ,如果 如果 没传后面的修饰参数 T 会被 识别成 T 的 上边界( 根据 T的定义 如果: T 被识别成 RespData ,如果是 T被识别成 Object )
推荐阅读
-
Linux中安装Python的交互式解释器IPython的教程
-
Windows下使Python2.x版本的解释器与3.x共存的方法
-
Sql学习第三天——SQL 关于CTE(公用表达式)的递归查询使用
-
Sql学习第四天——SQL 关于with cube,with rollup和grouping解释及演示
-
关于Android短信验证码的获取的示例
-
详解关于Android Studio中安装和gradle的一些坑
-
Python创建二维数组实例(关于list的一个小坑)
-
关于关键词密度多少最好的阐述
-
关于django时区设置的问题
-
关于vue的npm run dev和npm run build的区别介绍