【发布时间】:2021-07-28 11:22:23
【问题描述】:
来自 gson-2.8.6.jar
package com.google.gson.reflect;
...
public class TypeToken<T> {
final Class<? super T> rawType;
final Type type;
final int hashCode;
/**
* Constructs a new type literal. Derives represented class from type
* parameter.
*
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
* parameter in the anonymous class's type hierarchy so we can reconstitute it
* at runtime despite erasure.
*/
@SuppressWarnings("unchecked")
protected TypeToken() {
this.type = getSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) $Gson$Types.getRawType(type);
this.hashCode = type.hashCode();
}
/**
* Unsafe. Constructs a type literal manually.
*/
@SuppressWarnings("unchecked")
TypeToken(Type type) {
this.type = $Gson$Types.canonicalize($Gson$Preconditions.checkNotNull(type));
this.rawType = (Class<? super T>) $Gson$Types.getRawType(this.type);
this.hashCode = this.type.hashCode();
}
...
}
在包com.google.gson.reflect之外,为什么protected TypeToken()构造函数可以用来新建一个实例?{}出现在new TypeToken<String>()之后的语法是什么?
package com.dataservice.controller;
...
Type localVarReturnType = (new TypeToken<String>() {}).getType();
...
【问题讨论】:
-
this 回答你的问题了吗?