Class TypeResolver

java.lang.Object
cloud.opencode.base.reflect.type.TypeResolver

public final class TypeResolver extends Object
Type Resolver 类型解析器

Resolves type variables in the context of a specific type, enabling proper generic type inference.

在特定类型上下文中解析类型变量,实现正确的泛型类型推断。

Features | 主要功能:

  • Type variable resolution - 类型变量解析
  • Parameterized type resolution - 参数化类型解析
  • Inheritance chain traversal - 继承链遍历

Usage Examples | 使用示例:

// Resolve type variable in context
Type resolved = TypeResolver.resolveType(StringList.class, listElementType);

// Get type variable map
Map<TypeVariable<?>, Type> typeMap = TypeResolver.getTypeVariableMap(StringList.class);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (caller must ensure non-null type arguments) - 空值安全: 否(调用方须确保非空类型参数)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • resolveType

      public static Type resolveType(Type context, Type toResolve)
      Resolves a type in the context of another type 在另一类型上下文中解析类型
      Parameters:
      context - the context type | 上下文类型
      toResolve - the type to resolve | 要解析的类型
      Returns:
      the resolved type | 解析后的类型
    • getTypeVariableMap

      public static Map<TypeVariable<?>, Type> getTypeVariableMap(Type type)
      Gets the type variable map for a type 获取类型的类型变量映射
      Parameters:
      type - the type | 类型
      Returns:
      map of type variables to their actual types | 类型变量到实际类型的映射
    • resolveReturnType

      public static Type resolveReturnType(Type context, Method method)
      Resolves the return type of a method in a specific type context 在特定类型上下文中解析方法返回类型
      Parameters:
      context - the context type | 上下文类型
      method - the method | 方法
      Returns:
      the resolved return type | 解析后的返回类型
    • resolveParameterTypes

      public static Type[] resolveParameterTypes(Type context, Method method)
      Resolves the parameter types of a method in a specific type context 在特定类型上下文中解析方法参数类型
      Parameters:
      context - the context type | 上下文类型
      method - the method | 方法
      Returns:
      array of resolved parameter types | 解析后的参数类型数组
    • resolveFieldType

      public static Type resolveFieldType(Type context, Field field)
      Resolves the type of a field in a specific type context 在特定类型上下文中解析字段类型
      Parameters:
      context - the context type | 上下文类型
      field - the field | 字段
      Returns:
      the resolved field type | 解析后的字段类型
    • getTypeArgument

      public static Type getTypeArgument(Type type, int index)
      Gets the type argument at a specific index from a parameterized type 从参数化类型获取指定索引的类型参数
      Parameters:
      type - the parameterized type | 参数化类型
      index - the index | 索引
      Returns:
      the type argument or null | 类型参数或null
    • findTypeArgument

      public static Type findTypeArgument(Type type, Class<?> targetType, int index)
      Finds the type argument for a specific type variable in the inheritance chain 在继承链中查找特定类型变量的类型参数
      Parameters:
      type - the type to search | 要搜索的类型
      targetType - the target parameterized type | 目标参数化类型
      index - the type argument index | 类型参数索引
      Returns:
      the resolved type argument or null | 解析后的类型参数或null