首先 TypeReference  是描述 一个复杂 泛型的工具类。

TypeReference 很多类库都有,用 fastjson 的 举例,大概就这个意思。

 

例子:

Response<FeedInRespData> response = JSONObject.parseObject(result, new TypeReference<Response<FeedInRespData>>() {});
  • 1.

 

 

new TypeReference<Response<FeedInRespData>>() {} 描述的是一个   这个样的 类: Response<FeedInRespData>,同理,可以更多层的嵌套泛型。

 

 

 

 

当 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;

    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

  

上面的 无参 构造方法 里面 获取了 参数泛型 (ParameterizedType),后面的 有差 构造方法 用参数 替换了 参数泛型中是  泛型变量 里面的 内容。

例子:Response<T> response = JSONObject.parseObject(result, new TypeReference<Response<T>>( respDatacls ) {});

等价于:Response<TaskCodeRespData> response = JJSONObject.parseObject(result, new TypeReference<Response<TaskCodeRespData>>( ) {})

如果要用泛型变量 ,有参数的写法就相当必要了。

 

备注:上面的  respDatacls  = TaskCodeRespData.class 

备注:如果 T 是 泛型变量 ,如果 如果 没传后面的修饰参数 T 会被 识别成 T 的 上边界(  根据 T的定义 如果: <T extends RespData> T 被识别成 RespData  ,如果是<T> T被识别成 Object       )