Class TypeToken<T>

java.lang.Object
cloud.opencode.base.reflect.type.TypeToken<T>
Type Parameters:
T - the type to capture | 要捕获的类型
All Implemented Interfaces:
Serializable

public abstract class TypeToken<T> extends Object implements Serializable
Generic Type Token (Similar to Guava TypeToken) 泛型类型令牌(对标 Guava TypeToken)

Captures and preserves generic type information at runtime, solving Java's type erasure problem.

在运行时捕获和保留泛型类型信息,解决Java的类型擦除问题。

Features | 主要功能:

  • Runtime generic type capture - 运行时泛型类型捕获
  • Type parameter extraction - 类型参数提取
  • Type relationship checking - 类型关系检查
  • Field/Method type resolution - 字段/方法类型解析

Usage Examples | 使用示例:

// Capture generic type via anonymous subclass
TypeToken<List<String>> listType = new TypeToken<List<String>>() {};

// Get raw type
Class<?> rawType = listType.getRawType(); // List.class

// Get type parameter
TypeToken<?> elementType = listType.getTypeParameter(0); // String

// Convenience methods
TypeToken<List<Integer>> intList = TypeToken.listOf(Integer.class);

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a TypeToken by capturing generic type from anonymous subclass 通过匿名子类捕获泛型类型创建TypeToken
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    Gets the component type (for arrays) 获取组件类型(用于数组)
    Class<? super T>
    Gets the raw type (without generic parameters) 获取原始类型(无泛型参数)
    Gets the underlying Type 获取底层Type
    getTypeParameter(int index)
    Gets the type parameter at the specified index 获取指定索引的类型参数
    Gets all type parameters 获取所有类型参数
    int
     
    boolean
    Checks if this is an array type 检查是否为数组类型
    boolean
    Checks if this type is assignable from the specified type 检查此类型是否可从指定类型赋值
    boolean
    Checks if this is a parameterized type 检查是否为参数化类型
    boolean
    Checks if this is a primitive type 检查是否为原始类型
    boolean
    Checks if this type is a subtype of the specified type 检查此类型是否为指定类型的子类型
    boolean
    Checks if this type is a supertype of the specified type 检查此类型是否为指定类型的父类型
    boolean
    Checks if this is a type variable 检查是否为类型变量
    boolean
    Checks if this is a wildcard type 检查是否为通配符类型
    static <E> TypeToken<List<E>>
    listOf(TypeToken<E> elementType)
    Creates a List<E> TypeToken 创建 List<E> TypeToken
    static <E> TypeToken<List<E>>
    listOf(Class<E> elementType)
    Creates a List<E> TypeToken 创建 List<E> TypeToken
    static <K,V> TypeToken<Map<K,V>>
    mapOf(TypeToken<K> keyType, TypeToken<V> valueType)
    Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken
    static <K,V> TypeToken<Map<K,V>>
    mapOf(Class<K> keyType, Class<V> valueType)
    Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken
    static <T> TypeToken<T>
    of(Class<T> type)
    Creates a TypeToken from Class 从Class创建TypeToken
    static TypeToken<?>
    of(Type type)
    Creates a TypeToken from Type 从Type创建TypeToken
    static <T> TypeToken<Optional<T>>
    optionalOf(TypeToken<T> valueType)
    Creates an Optional<T> TypeToken 创建 Optional<T> TypeToken
    static <T> TypeToken<Optional<T>>
    optionalOf(Class<T> valueType)
    Creates an Optional<T> TypeToken 创建 Optional<T> TypeToken
    Resolves field type in context of this type 在此类型上下文中解析字段类型
    Resolves method parameter types in context of this type 在此类型上下文中解析方法参数类型
    Resolves method return type in context of this type 在此类型上下文中解析方法返回类型
    resolveType(Type toResolve)
    Resolves a type in the context of this type 在此类型上下文中解析类型
    static <E> TypeToken<Set<E>>
    setOf(TypeToken<E> elementType)
    Creates a Set<E> TypeToken 创建 Set<E> TypeToken
    static <E> TypeToken<Set<E>>
    setOf(Class<E> elementType)
    Creates a Set<E> TypeToken 创建 Set<E> TypeToken
     
    Gets the primitive type (for wrappers) 获取原始类型(对于包装类型)
    Gets the wrapper type (for primitives) 获取包装类型(对于原始类型)

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypeToken

      protected TypeToken()
      Creates a TypeToken by capturing generic type from anonymous subclass 通过匿名子类捕获泛型类型创建TypeToken
  • Method Details

    • of

      public static <T> TypeToken<T> of(Class<T> type)
      Creates a TypeToken from Class 从Class创建TypeToken
      Type Parameters:
      T - the type | 类型
      Parameters:
      type - the class | 类
      Returns:
      TypeToken instance | TypeToken实例
    • of

      public static TypeToken<?> of(Type type)
      Creates a TypeToken from Type 从Type创建TypeToken
      Parameters:
      type - the type | 类型
      Returns:
      TypeToken instance | TypeToken实例
    • getType

      public Type getType()
      Gets the underlying Type 获取底层Type
      Returns:
      the Type | 类型
    • getRawType

      public Class<? super T> getRawType()
      Gets the raw type (without generic parameters) 获取原始类型(无泛型参数)
      Returns:
      the raw type | 原始类型
    • getTypeParameter

      public TypeToken<?> getTypeParameter(int index)
      Gets the type parameter at the specified index 获取指定索引的类型参数
      Parameters:
      index - the index | 索引
      Returns:
      the type parameter | 类型参数
    • getTypeParameters

      public List<TypeToken<?>> getTypeParameters()
      Gets all type parameters 获取所有类型参数
      Returns:
      list of type parameters | 类型参数列表
    • getComponentType

      public TypeToken<?> getComponentType()
      Gets the component type (for arrays) 获取组件类型(用于数组)
      Returns:
      the component type or null | 组件类型或null
    • isPrimitive

      public boolean isPrimitive()
      Checks if this is a primitive type 检查是否为原始类型
      Returns:
      true if primitive | 如果是原始类型返回true
    • isArray

      public boolean isArray()
      Checks if this is an array type 检查是否为数组类型
      Returns:
      true if array | 如果是数组返回true
    • isParameterized

      public boolean isParameterized()
      Checks if this is a parameterized type 检查是否为参数化类型
      Returns:
      true if parameterized | 如果是参数化类型返回true
    • isWildcard

      public boolean isWildcard()
      Checks if this is a wildcard type 检查是否为通配符类型
      Returns:
      true if wildcard | 如果是通配符返回true
    • isTypeVariable

      public boolean isTypeVariable()
      Checks if this is a type variable 检查是否为类型变量
      Returns:
      true if type variable | 如果是类型变量返回true
    • isSupertypeOf

      public boolean isSupertypeOf(TypeToken<?> other)
      Checks if this type is a supertype of the specified type 检查此类型是否为指定类型的父类型
      Parameters:
      other - the other type | 另一类型
      Returns:
      true if supertype | 如果是父类型返回true
    • isSubtypeOf

      public boolean isSubtypeOf(TypeToken<?> other)
      Checks if this type is a subtype of the specified type 检查此类型是否为指定类型的子类型
      Parameters:
      other - the other type | 另一类型
      Returns:
      true if subtype | 如果是子类型返回true
    • isAssignableFrom

      public boolean isAssignableFrom(TypeToken<?> other)
      Checks if this type is assignable from the specified type 检查此类型是否可从指定类型赋值
      Parameters:
      other - the other type | 另一类型
      Returns:
      true if assignable | 如果可赋值返回true
    • resolveType

      public TypeToken<?> resolveType(Type toResolve)
      Resolves a type in the context of this type 在此类型上下文中解析类型
      Parameters:
      toResolve - the type to resolve | 要解析的类型
      Returns:
      resolved TypeToken | 解析后的TypeToken
    • wrap

      public TypeToken<T> wrap()
      Gets the wrapper type (for primitives) 获取包装类型(对于原始类型)
      Returns:
      wrapper TypeToken | 包装类型TypeToken
    • unwrap

      public TypeToken<T> unwrap()
      Gets the primitive type (for wrappers) 获取原始类型(对于包装类型)
      Returns:
      primitive TypeToken | 原始类型TypeToken
    • resolveFieldType

      public TypeToken<?> resolveFieldType(Field field)
      Resolves field type in context of this type 在此类型上下文中解析字段类型
      Parameters:
      field - the field | 字段
      Returns:
      resolved TypeToken | 解析后的TypeToken
    • resolveReturnType

      public TypeToken<?> resolveReturnType(Method method)
      Resolves method return type in context of this type 在此类型上下文中解析方法返回类型
      Parameters:
      method - the method | 方法
      Returns:
      resolved TypeToken | 解析后的TypeToken
    • resolveParameterTypes

      public List<TypeToken<?>> resolveParameterTypes(Method method)
      Resolves method parameter types in context of this type 在此类型上下文中解析方法参数类型
      Parameters:
      method - the method | 方法
      Returns:
      list of resolved TypeTokens | 解析后的TypeToken列表
    • listOf

      public static <E> TypeToken<List<E>> listOf(TypeToken<E> elementType)
      Creates a List<E> TypeToken 创建 List<E> TypeToken
      Type Parameters:
      E - the element type | 元素类型
      Parameters:
      elementType - the element type | 元素类型
      Returns:
      TypeToken for List<E> | List<E>的TypeToken
    • listOf

      public static <E> TypeToken<List<E>> listOf(Class<E> elementType)
      Creates a List<E> TypeToken 创建 List<E> TypeToken
      Type Parameters:
      E - the element type | 元素类型
      Parameters:
      elementType - the element class | 元素类
      Returns:
      TypeToken for List<E> | List<E>的TypeToken
    • setOf

      public static <E> TypeToken<Set<E>> setOf(TypeToken<E> elementType)
      Creates a Set<E> TypeToken 创建 Set<E> TypeToken
      Type Parameters:
      E - the element type | 元素类型
      Parameters:
      elementType - the element type | 元素类型
      Returns:
      TypeToken for Set<E> | Set<E>的TypeToken
    • setOf

      public static <E> TypeToken<Set<E>> setOf(Class<E> elementType)
      Creates a Set<E> TypeToken 创建 Set<E> TypeToken
      Type Parameters:
      E - the element type | 元素类型
      Parameters:
      elementType - the element class | 元素类
      Returns:
      TypeToken for Set<E> | Set<E>的TypeToken
    • mapOf

      public static <K,V> TypeToken<Map<K,V>> mapOf(TypeToken<K> keyType, TypeToken<V> valueType)
      Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken
      Type Parameters:
      K - the key type | 键类型
      V - the value type | 值类型
      Parameters:
      keyType - the key type | 键类型
      valueType - the value type | 值类型
      Returns:
      TypeToken for Map<K,V> | Map<K,V>的TypeToken
    • mapOf

      public static <K,V> TypeToken<Map<K,V>> mapOf(Class<K> keyType, Class<V> valueType)
      Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken
      Type Parameters:
      K - the key type | 键类型
      V - the value type | 值类型
      Parameters:
      keyType - the key class | 键类
      valueType - the value class | 值类
      Returns:
      TypeToken for Map<K,V> | Map<K,V>的TypeToken
    • optionalOf

      public static <T> TypeToken<Optional<T>> optionalOf(TypeToken<T> valueType)
      Creates an Optional<T> TypeToken 创建 Optional<T> TypeToken
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      valueType - the value type | 值类型
      Returns:
      TypeToken for Optional<T> | Optional<T>的TypeToken
    • optionalOf

      public static <T> TypeToken<Optional<T>> optionalOf(Class<T> valueType)
      Creates an Optional<T> TypeToken 创建 Optional<T> TypeToken
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      valueType - the value class | 值类
      Returns:
      TypeToken for Optional<T> | Optional<T>的TypeToken
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object