目录

前言

一、TypeReference是什么?

二、核心方法

1.无参构造器

2.方法介绍 

3、 方法示例

4、使用

4-1

 4-2

总结


  •  

 


前言

最近项目中使用到了TypeReference,发现自己使用了这么长时间的fastjson竟然没有了解过这个类,索性查一些资料总结一下。总结的不一定到位,就当自己学习过的证据吧!!!


提示:以下是本篇文章正文内容,下面案例可供参考

一、TypeReference是什么?

TypeReference是com.alibaba.fastjson下面的一个类(很多人说这个类在其他地方也有,我这只是在fastjson中使用到了,就先这么记录吧)。

字面意思是指Type的Reference,也就是某类型的一个指向或者引用

二、核心方法

1.无参构造器

 protected TypeReference() {
        // 获取当前class的父类的Type
        Type superClass = this.getClass().getGenericSuperclass();
        // 父类class的泛型类型数组,表示的是该类型的实际类型参数
        Type type = ((ParameterizedType)superClass).getActualTypeArguments()[0];
        Type cachedType = (Type)classTypeCache.get(type);
        if (cachedType == null) {
            classTypeCache.putIfAbsent(type, type);
            cachedType = (Type)classTypeCache.get(type);
        }

        this.type = cachedType;
    }

2.方法介绍 

Type[] getActualTypeArguments
核心接口,返回泛型类型数组, 该接口可获取父类实际泛型类型,返回的Type数组对象表示该类型的实际类型参数。 
Type getRawType()
返回原始类型Type
Type getOwnerType()
返回 Type 对象,表示此类型是其成员之一的类型。

3、 方法示例

public static void main(String[] args) {
      
        class IntMap extends HashMap<String,Integer> {

        }
        IntMap intMap = new IntMap();
        System.out.println("superClass:"+intMap.getClass().getSuperclass());

        Type type = intMap.getClass().getGenericSuperclass();
        if(type instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) type;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }

        System.out.println("=====newclass=====");
        Map<String,Integer> newIntMap = new HashMap<>();
        System.out.println(newIntMap.getClass().getSuperclass());

        Type newClassType = newIntMap.getClass().getGenericSuperclass();
        if(newClassType instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) newClassType;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }

        System.out.println("=====subclass=====");
        HashMap<String,Integer> subIntMap = new HashMap<String,Integer>(){};
        System.out.println(subIntMap.getClass().getSuperclass());

        Type subClassType = subIntMap.getClass().getGenericSuperclass();
        if(subClassType instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) subClassType;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }

    }

结果:

superClass:class java.util.HashMap
class java.lang.String
class java.lang.Integer
=====newclass=====
class java.util.AbstractMap
K
V
=====subclass=====
class java.util.HashMap
class java.lang.String
class java.lang.Integer

4、使用

通过TypeReference将对象类型进行转换

4-1

这个用法一般是我们的常用用法,主要是将接收的请求(字符串或者JSON对象等)转化成我们期望的对象来接收。

例如:下面的demo中我们传入的是List<Integer>,但是我们可以转化成List<String>来接收

public static void main(String[] args) {
        List list = new ArrayList();
        list.add(1);
        list.add(2);

        JSONObject o = new JSONObject();
        o.put("k",list);

        // list中加入Integer类型元素,这里获取的List对象就是元素类型Integer
        List<String> types = o.getObject("k",List.class);
        System.out.println(JSON.toJSONString(types));
        
        // 通过TypeReference将List<Integer>中泛型映射成List<String>类型
        List<Integer> types2 = o.getObject("k",new TypeReference<List<String>>(){});
        System.out.println(JSON.toJSONString(types2));
}

结果 

[1,2]
["1","2"]

 4-2

 例如:我们将接收到的json字符串jsonString转化成我们期望的 PageParam<TransactionFlowQueryDto> 类型,然后直接get方法获取期望属性

PageParam<TransactionFlowQueryDto> transferParam = JSONObject.parseObject(jsonString, new TypeReference<PageParam<TransactionFlowQueryDto>>() {});
TransactionFlowQueryDto data = transferParam.getData();
...

 


总结

TypeReferenc 我也是简单的理解,源码涉及到了泛型、继承等,有时间的小伙伴可以自行去研究下,我这就当抛砖引玉了

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐