Class TypeReference<T>

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

Features | 主要功能:

  • Captures generic type information at runtime - 运行时捕获泛型类型信息
  • Solves Java type erasure for JSON deserialization - 解决JSON反序列化中的Java类型擦除问题
  • Factory methods for Class and Type creation - Class和Type创建的工厂方法

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)

public abstract class TypeReference<T> extends Object
Type Reference - Generic Type Token for JSON Deserialization 类型引用 - 用于 JSON 反序列化的泛型类型令牌

This class captures generic type information at runtime by using subclassing with an anonymous class. It solves Java's type erasure problem for JSON deserialization.

此类通过使用匿名类子类化在运行时捕获泛型类型信息。 它解决了 JSON 反序列化中 Java 类型擦除的问题。

Example | 示例:

// Without TypeReference - loses generic type info
List<User> users = OpenJson.fromJson(json, List.class);  // Returns List<Object>

// With TypeReference - preserves generic type info
List<User> users = OpenJson.fromJson(json, new TypeReference<List<User>>() {});

// For Map types
Map<String, List<Order>> orders = OpenJson.fromJson(json,
    new TypeReference<Map<String, List<Order>>>() {});
Since:
JDK 25, opencode-base-json 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 for a simple 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 for a Type. 为 Type 创建 TypeReference。
      Type Parameters:
      T - the type - 类型
      Parameters:
      type - the type - 类型
      Returns:
      the type reference - 类型引用
    • getType

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

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