Class ReflectUtil
java.lang.Object
cloud.opencode.base.core.reflect.ReflectUtil
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 Summary
Modifier and TypeMethodDescriptionstatic Constructor<?>[]getConstructors(Class<?> clazz) Gets all constructors 获取所有构造器static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) Gets the default constructor 获取默认构造器static FieldGets the specified field 获取指定字段static Field[]Gets all fields (including superclass) 获取所有字段(包括父类)static <T> TgetFieldValue(Object obj, String fieldName) Gets the field value 获取字段值static MethodGets the specified method 获取指定方法static Method[]getMethods(Class<?> clazz) Gets all methods (including superclass) 获取所有方法(包括父类)static <T> TgetStaticFieldValue(Class<?> clazz, String fieldName) Gets the static field value 获取静态字段值static <T> TInvokes a method 调用方法static <T> TinvokeStatic(Class<?> clazz, String methodName, Object... args) Invokes a static method 调用静态方法static <T> TnewInstance(Class<T> clazz) Creates an instance (no-arg constructor) 创建实例(无参构造)static <T> TnewInstance(Class<T> clazz, Object... args) Creates an instance (with constructor arguments) 创建实例(带参构造)static voidsetFieldValue(Object obj, String fieldName, Object value) Sets the field value 设置字段值static voidsetStaticFieldValue(Class<?> clazz, String fieldName, Object value) Sets the static field value 设置静态字段值
-
Method Details
-
newInstance
Creates an instance (no-arg constructor) 创建实例(无参构造) -
newInstance
-
invoke
-
invokeStatic
-
getFieldValue
-
setFieldValue
-
getStaticFieldValue
-
setStaticFieldValue
-
getFields
-
getField
-
getMethods
-
getMethod
-
getConstructors
Gets all constructors 获取所有构造器 -
getDefaultConstructor
Gets the default constructor 获取默认构造器
-