Class TypeReference<T>
java.lang.Object
cloud.opencode.base.serialization.TypeReference<T>
- Type Parameters:
T- the referenced type - 引用的类型
TypeReference - Generic Type Reference for Deserialization
类型引用 - 用于反序列化的泛型类型引用
This class captures generic type information at runtime by using subclassing with an anonymous class. It solves Java's type erasure problem for deserialization.
此类通过使用匿名类子类化在运行时捕获泛型类型信息。 它解决了反序列化中 Java 类型擦除的问题。
Features | 主要功能:
- Preserve generic type information - 保留泛型类型信息
- Factory methods for common types - 常用类型的工厂方法
- Support for nested generics - 支持嵌套泛型
Usage Examples | 使用示例:
// Anonymous subclass pattern
TypeReference<List<User>> listType = new TypeReference<List<User>>() {};
// Factory methods
TypeReference<List<String>> stringList = TypeReference.listOf(String.class);
TypeReference<Map<String, Integer>> map = TypeReference.mapOf(String.class, Integer.class);
// Usage with deserialize
List<User> users = OpenSerializer.deserialize(data, new TypeReference<List<User>>() {});
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是 (不可变)
- Since:
- JDK 25, opencode-base-serialization V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TypeReference<Collection<T>> collectionOf(Class<T> elementType) Creates a TypeReference for Collection type.booleanClass<?> Returns the raw class of the referenced type.getType()Returns the referenced type.Type[]Returns the type arguments if this is a parameterized type.inthashCode()booleanReturns whether this is a parameterized type.static <T> TypeReference<List<T>> Creates a TypeReference for List type.static <K,V> TypeReference <Map<K, V>> Creates a TypeReference for Map type.static <T> TypeReference<T> Creates a TypeReference from a Class.static <T> TypeReference<T> Creates a TypeReference from a Type.static <T> TypeReference<Optional<T>> optionalOf(Class<T> elementType) Creates a TypeReference for Optional type.static <T> TypeReference<Set<T>> Creates a TypeReference for Set type.toString()
-
Constructor Details
-
TypeReference
protected TypeReference()Constructs a new type reference. 构造新的类型引用。This constructor must be called from a subclass (typically an anonymous class) to capture the generic type parameter.
此构造函数必须从子类(通常是匿名类)调用以捕获泛型类型参数。
-
-
Method Details
-
of
Creates a TypeReference from a Class. 从 Class 创建 TypeReference。- Type Parameters:
T- the type - 类型- Parameters:
clazz- the class - 类- Returns:
- the type reference - 类型引用
-
of
Creates a TypeReference from a Type. 从 Type 创建 TypeReference。- Type Parameters:
T- the type - 类型- Parameters:
type- the type - 类型- Returns:
- the type reference - 类型引用
-
listOf
Creates a TypeReference for List type. 为 List 类型创建 TypeReference。- Type Parameters:
T- the element type - 元素类型- Parameters:
elementType- the element type - 元素类型- Returns:
- the type reference - 类型引用
-
setOf
Creates a TypeReference for Set type. 为 Set 类型创建 TypeReference。- Type Parameters:
T- the element type - 元素类型- Parameters:
elementType- the element type - 元素类型- Returns:
- the type reference - 类型引用
-
mapOf
Creates a TypeReference for Map type. 为 Map 类型创建 TypeReference。- Type Parameters:
K- the key type - 键类型V- the value type - 值类型- Parameters:
keyType- the key type - 键类型valueType- the value type - 值类型- Returns:
- the type reference - 类型引用
-
collectionOf
Creates a TypeReference for Collection type. 为 Collection 类型创建 TypeReference。- Type Parameters:
T- the element type - 元素类型- Parameters:
elementType- the element type - 元素类型- Returns:
- the type reference - 类型引用
-
optionalOf
Creates a TypeReference for Optional type. 为 Optional 类型创建 TypeReference。- Type Parameters:
T- the element type - 元素类型- Parameters:
elementType- the element type - 元素类型- Returns:
- the type reference - 类型引用
-
getType
-
getRawType
Returns the raw class of the referenced type. 返回引用类型的原始类。- Returns:
- the raw class - 原始类
-
isParameterized
public boolean isParameterized()Returns whether this is a parameterized type. 返回此是否为参数化类型。- Returns:
- true if parameterized - 如果是参数化类型则返回 true
-
getTypeArguments
Returns the type arguments if this is a parameterized type. 如果是参数化类型则返回类型参数。- Returns:
- the type arguments, or empty array - 类型参数,或空数组
-
equals
-
hashCode
-
toString
-