Class ReflectUtil

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

public final class ReflectUtil extends Object
Reflection Utility Class - Core reflection operations with caching 反射工具类 - 核心反射操作,支持缓存优化

Provides cached reflection operations for fields, methods, and constructors.

提供字段、方法和构造器的缓存反射操作。

Features | 主要功能:

  • Instance creation (with/without args) - 实例创建(带参/无参)
  • Method invocation (instance/static) - 方法调用(实例/静态)
  • Field access (get/set, instance/static) - 字段访问(读写,实例/静态)
  • ClassValue-based caching for performance - 基于 ClassValue 的高性能缓存

Usage Examples | 使用示例:

// Create instance - 创建实例
User user = ReflectUtil.newInstance(User.class);

// Invoke method - 调用方法
String name = ReflectUtil.invoke(user, "getName");

// Get/Set field - 获取/设置字段
Object value = ReflectUtil.getFieldValue(user, "name");
ReflectUtil.setFieldValue(user, "name", "Leon");

Security | 安全性:

  • Thread-safe: Yes (ClassValue caching) - 线程安全: 是 (ClassValue 缓存)
  • Null-safe: No (throws exceptions) - 空值安全: 否 (抛出异常)

Performance | 性能特性:

  • Time complexity: O(n) for class hierarchy traversal - 类层次遍历 O(n)
  • Space complexity: O(1) per cached lookup - 每次缓存查找 O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • newInstance

      public static <T> T newInstance(Class<T> clazz)
      Creates an instance (no-arg constructor) 创建实例(无参构造)
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Object... args)
      Creates an instance (with constructor arguments) 创建实例(带参构造)
    • invoke

      public static <T> T invoke(Object obj, String methodName, Object... args)
      Invokes a method 调用方法
    • invokeStatic

      public static <T> T invokeStatic(Class<?> clazz, String methodName, Object... args)
      Invokes a static method 调用静态方法
    • getFieldValue

      public static <T> T getFieldValue(Object obj, String fieldName)
      Gets the field value 获取字段值
    • setFieldValue

      public static void setFieldValue(Object obj, String fieldName, Object value)
      Sets the field value 设置字段值
    • getStaticFieldValue

      public static <T> T getStaticFieldValue(Class<?> clazz, String fieldName)
      Gets the static field value 获取静态字段值
    • setStaticFieldValue

      public static void setStaticFieldValue(Class<?> clazz, String fieldName, Object value)
      Sets the static field value 设置静态字段值
    • getFields

      public static Field[] getFields(Class<?> clazz)
      Gets all fields (including superclass) 获取所有字段(包括父类)
    • getField

      public static Field getField(Class<?> clazz, String fieldName)
      Gets the specified field 获取指定字段
    • getMethods

      public static Method[] getMethods(Class<?> clazz)
      Gets all methods (including superclass) 获取所有方法(包括父类)
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes)
      Gets the specified method 获取指定方法
    • getConstructors

      public static Constructor<?>[] getConstructors(Class<?> clazz)
      Gets all constructors 获取所有构造器
    • getDefaultConstructor

      public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz)
      Gets the default constructor 获取默认构造器