Class TypeReference<T>

java.lang.Object
cloud.opencode.base.serialization.TypeReference<T>
Type Parameters:
T - the referenced type - 引用的类型

public abstract class TypeReference<T> extends Object
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 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

      public static <T> TypeReference<T> of(Class<T> clazz)
      Creates a TypeReference from a Class. 从 Class 创建 TypeReference。
      Type Parameters:
      T - the type - 类型
      Parameters:
      clazz - the class - 类
      Returns:
      the type reference - 类型引用
    • of

      public static <T> TypeReference<T> of(Type type)
      Creates a TypeReference from a Type. 从 Type 创建 TypeReference。
      Type Parameters:
      T - the type - 类型
      Parameters:
      type - the type - 类型
      Returns:
      the type reference - 类型引用
    • listOf

      public static <T> TypeReference<List<T>> listOf(Class<T> elementType)
      Creates a TypeReference for List type. 为 List 类型创建 TypeReference。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      elementType - the element type - 元素类型
      Returns:
      the type reference - 类型引用
    • setOf

      public static <T> TypeReference<Set<T>> setOf(Class<T> elementType)
      Creates a TypeReference for Set type. 为 Set 类型创建 TypeReference。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      elementType - the element type - 元素类型
      Returns:
      the type reference - 类型引用
    • mapOf

      public static <K,V> TypeReference<Map<K,V>> mapOf(Class<K> keyType, Class<V> valueType)
      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

      public static <T> TypeReference<Collection<T>> collectionOf(Class<T> elementType)
      Creates a TypeReference for Collection type. 为 Collection 类型创建 TypeReference。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      elementType - the element type - 元素类型
      Returns:
      the type reference - 类型引用
    • optionalOf

      public static <T> TypeReference<Optional<T>> optionalOf(Class<T> elementType)
      Creates a TypeReference for Optional type. 为 Optional 类型创建 TypeReference。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      elementType - the element type - 元素类型
      Returns:
      the type reference - 类型引用
    • getType

      public Type getType()
      Returns the referenced type. 返回引用的类型。
      Returns:
      the type - 类型
    • getRawType

      public Class<?> 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

      public Type[] getTypeArguments()
      Returns the type arguments if this is a parameterized type. 如果是参数化类型则返回类型参数。
      Returns:
      the type arguments, or empty array - 类型参数,或空数组
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object