Class ReflectUtil

java.lang.Object
cloud.opencode.base.reflect.ReflectUtil

public final class ReflectUtil extends Object
Reflection Utility Class 反射工具类

Provides low-level reflection utility methods.

提供底层反射工具方法。

Features | 主要功能:

  • Accessibility management - 可访问性管理
  • Method/field/constructor lookup - 方法/字段/构造器查找
  • Reflection invocation wrappers - 反射调用包装

Usage Examples | 使用示例:

ReflectUtil.makeAccessible(field);
Object value = ReflectUtil.getFieldValue(obj, field);
ReflectUtil.invokeMethod(obj, method, args);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) per operation (accessibility set, field get/set, method invoke) - 时间复杂度: 每次操作 O(1)(可访问性设置、字段读写、方法调用)
  • Space complexity: O(1) - 空间复杂度: O(1)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • setAccessible

      public static <T extends AccessibleObject> T setAccessible(T accessible)
      Makes accessible object accessible 设置可访问性
      Type Parameters:
      T - the type | 类型
      Parameters:
      accessible - the accessible object | 可访问对象
      Returns:
      the accessible object | 可访问对象
    • setAccessible

      public static <T extends AccessibleObject> T setAccessible(T accessible, Object target)
      Makes accessible object accessible with target check 设置可访问性(检查目标)
      Type Parameters:
      T - the type | 类型
      Parameters:
      accessible - the accessible object | 可访问对象
      target - the target object | 目标对象
      Returns:
      the accessible object | 可访问对象
    • isAccessible

      public static boolean isAccessible(AccessibleObject accessible, Object target)
      Checks if accessible 检查是否可访问
      Parameters:
      accessible - the accessible object | 可访问对象
      target - the target object | 目标对象
      Returns:
      true if accessible | 如果可访问返回true
    • getDeclaringClass

      public static Class<?> getDeclaringClass(Member member)
      Gets declaring class of member 获取成员的声明类
      Parameters:
      member - the member | 成员
      Returns:
      the declaring class | 声明类
    • getName

      public static String getName(Member member)
      Gets member name 获取成员名称
      Parameters:
      member - the member | 成员
      Returns:
      the name | 名称
    • getModifiers

      public static int getModifiers(Member member)
      Gets member modifiers 获取成员修饰符
      Parameters:
      member - the member | 成员
      Returns:
      the modifiers | 修饰符
    • resolveReturnType

      public static Type resolveReturnType(Method method, Class<?> targetClass)
      Resolves generic return type of method 解析方法的泛型返回类型
      Parameters:
      method - the method | 方法
      targetClass - the target class | 目标类
      Returns:
      the resolved type | 解析后的类型
    • resolveParameterTypes

      public static Type[] resolveParameterTypes(Executable executable, Class<?> targetClass)
      Resolves generic parameter types 解析泛型参数类型
      Parameters:
      executable - the executable | 可执行对象
      targetClass - the target class | 目标类
      Returns:
      the resolved types | 解析后的类型数组
    • resolveType

      public static Type resolveType(Type type, Class<?> targetClass)
      Resolves type variable in context of target class 在目标类上下文中解析类型变量
      Parameters:
      type - the type | 类型
      targetClass - the target class | 目标类
      Returns:
      the resolved type | 解析后的类型
    • newArray

      public static Object newArray(Class<?> componentType, int length)
      Creates array instance 创建数组实例
      Parameters:
      componentType - the component type | 组件类型
      length - the length | 长度
      Returns:
      the array | 数组
    • newArray

      public static Object newArray(Class<?> componentType, int... dimensions)
      Creates multi-dimensional array 创建多维数组
      Parameters:
      componentType - the component type | 组件类型
      dimensions - the dimensions | 维度
      Returns:
      the array | 数组
    • getArrayLength

      public static int getArrayLength(Object array)
      Gets array length 获取数组长度
      Parameters:
      array - the array | 数组
      Returns:
      the length | 长度
    • getArrayElement

      public static Object getArrayElement(Object array, int index)
      Gets array element 获取数组元素
      Parameters:
      array - the array | 数组
      index - the index | 索引
      Returns:
      the element | 元素
    • setArrayElement

      public static void setArrayElement(Object array, int index, Object value)
      Sets array element 设置数组元素
      Parameters:
      array - the array | 数组
      index - the index | 索引
      value - the value | 值
    • unwrapInvocationTargetException

      public static Throwable unwrapInvocationTargetException(Throwable ex)
      Unwraps InvocationTargetException 解包InvocationTargetException
      Parameters:
      ex - the exception | 异常
      Returns:
      the cause or original | 原因或原始异常
    • sneakyThrow

      public static <T extends Throwable> RuntimeException sneakyThrow(Throwable ex) throws T
      Rethrows exception as unchecked 将异常作为非受检异常重新抛出
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      ex - the exception | 异常
      Returns:
      never returns | 永不返回
      Throws:
      T
    • isAssignable

      public static boolean isAssignable(Class<?>[] parameterTypes, Object[] args)
      Checks if parameter types match 检查参数类型是否匹配
      Parameters:
      parameterTypes - the parameter types | 参数类型
      args - the arguments | 参数
      Returns:
      true if match | 如果匹配返回true
    • isAssignable

      public static boolean isAssignable(Class<?> target, Class<?> source)
      Checks if types are assignable (with primitive widening) 检查类型是否可赋值(含原始类型拓宽)
      Parameters:
      target - the target type | 目标类型
      source - the source type | 源类型
      Returns:
      true if assignable | 如果可赋值返回true
    • primitiveToWrapper

      public static Class<?> primitiveToWrapper(Class<?> primitiveType)
      Converts primitive to wrapper 原始类型转包装类型
      Parameters:
      primitiveType - the primitive type | 原始类型
      Returns:
      the wrapper type | 包装类型
    • wrapperToPrimitive

      public static Class<?> wrapperToPrimitive(Class<?> wrapperType)
      Converts wrapper to primitive 包装类型转原始类型
      Parameters:
      wrapperType - the wrapper type | 包装类型
      Returns:
      the primitive type or null | 原始类型或null
    • getDefaultValue

      public static Object getDefaultValue(Class<?> type)
      Gets default value for type 获取类型的默认值
      Parameters:
      type - the type | 类型
      Returns:
      the default value | 默认值