本文主要是介绍fastjson TypeReference 泛型类型(详解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
系列文章目录
附属文件一:fastjson 泛型转换问题(详解)
文章目录
- 系列文章目录
- 一、简介
- 二、构造方法
- 1. 无参构造方法
- 2. 有参构造方法
一、简介
com.alibaba.fastjson.TypeReference 即类型引用,用于指定我们使用 com.alibaba.fastjson 进行 json 转换时的对象类型。
官方解释:
表示泛型的类型。Java 还没有提供表示泛型类型的方法,所以这个类提供了。强制客户端创建该类的子类,使其即使在运行时也能检索类型信息。
例如,要为 List 创建类型文本,您可以创建一个空的匿名内部类:
TypeReference<List> list = new TypeReference<List>() {};
此语法不能用于创建具有通配符参数的类型字面值,Class<?> 或 List<? extends CharSequence>。
目的是用于在 json 转换为泛型时,指定泛型的类型,以便能够正确的将 json 数据正确设置到实际的类对象中。
因为当 json 转换的对象是泛型时,我们无法获取到实际的类,无法获取到类的变量,也无法获取到类变量的 get、set 方法,所以我们无法将这个 json 数据转换到某一个实际的类对象中存储,所以 com.alibaba.fastjson 会临时转换为 JSONObject 对象,以 key-value 形式存储。
这样就存在一个问题,当我们将该泛型对象赋值转换为实际的类对象时,就会因类型不匹配而抛出异常。
二、构造方法
官方解释:
构造一个新的类型。从类型参数派生表示的类。
客户端创建一个空的匿名子类。这样做会将类型参数嵌入到匿名类的类型层次结构中,这样我们就可以在运行时重新构建它,而不用进行类型擦除。
1. 无参构造方法
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;
}
首先,在构造方法中通过反射获取到当前对象的 Class 类,即 TypeReference 的 Class 类,然后获取到该 Class 表示的实体的直接超类。然后获取到直接父类的实际类型参数。然后将该实际类型放到 static ConcurrentHashMap 类型的 classTypeCache 类型缓存 map 中,并设置到当前 TypeReference 对象的类型变量中。
2. 有参构造方法
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 envif (argTypes[i] instanceof GenericArrayType) {argTypes[i] = TypeUtils.checkPrimitiveArray((GenericArrayType) argTypes[i]);}// 如果有多层泛型且该泛型已经注明实现的情况下,判断该泛型下一层是否还有泛型if(argTypes[i] instanceof ParameterizedType) {argTypes[i] = handlerParameterizedType((ParameterizedType) argTypes[i], actualTypeArguments, actualIndex);}}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;
}
这篇关于fastjson TypeReference 泛型类型(详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!