Class ReflectUtil
java.lang.Object
cloud.opencode.base.reflect.ReflectUtil
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 Summary
Modifier and TypeMethodDescriptionstatic ObjectgetArrayElement(Object array, int index) Gets array element 获取数组元素static intgetArrayLength(Object array) Gets array length 获取数组长度static Class<?> getDeclaringClass(Member member) Gets declaring class of member 获取成员的声明类static ObjectgetDefaultValue(Class<?> type) Gets default value for type 获取类型的默认值static intgetModifiers(Member member) Gets member modifiers 获取成员修饰符static StringGets member name 获取成员名称static booleanisAccessible(AccessibleObject accessible, Object target) Checks if accessible 检查是否可访问static booleanisAssignable(Class<?>[] parameterTypes, Object[] args) Checks if parameter types match 检查参数类型是否匹配static booleanisAssignable(Class<?> target, Class<?> source) Checks if types are assignable (with primitive widening) 检查类型是否可赋值(含原始类型拓宽)static ObjectCreates array instance 创建数组实例static ObjectCreates multi-dimensional array 创建多维数组static Class<?> primitiveToWrapper(Class<?> primitiveType) Converts primitive to wrapper 原始类型转包装类型static Type[]resolveParameterTypes(Executable executable, Class<?> targetClass) Resolves generic parameter types 解析泛型参数类型static TyperesolveReturnType(Method method, Class<?> targetClass) Resolves generic return type of method 解析方法的泛型返回类型static TyperesolveType(Type type, Class<?> targetClass) Resolves type variable in context of target class 在目标类上下文中解析类型变量static <T extends AccessibleObject>
TsetAccessible(T accessible) Makes accessible object accessible 设置可访问性static <T extends AccessibleObject>
TsetAccessible(T accessible, Object target) Makes accessible object accessible with target check 设置可访问性(检查目标)static voidsetArrayElement(Object array, int index, Object value) Sets array element 设置数组元素static <T extends Throwable>
RuntimeExceptionsneakyThrow(Throwable ex) Rethrows exception as unchecked 将异常作为非受检异常重新抛出static ThrowableUnwraps InvocationTargetException 解包InvocationTargetExceptionstatic Class<?> wrapperToPrimitive(Class<?> wrapperType) Converts wrapper to primitive 包装类型转原始类型
-
Method Details
-
setAccessible
Makes accessible object accessible 设置可访问性- Type Parameters:
T- the type | 类型- Parameters:
accessible- the accessible object | 可访问对象- Returns:
- the accessible object | 可访问对象
-
setAccessible
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
Checks if accessible 检查是否可访问- Parameters:
accessible- the accessible object | 可访问对象target- the target object | 目标对象- Returns:
- true if accessible | 如果可访问返回true
-
getDeclaringClass
-
getName
-
getModifiers
Gets member modifiers 获取成员修饰符- Parameters:
member- the member | 成员- Returns:
- the modifiers | 修饰符
-
resolveReturnType
-
resolveParameterTypes
Resolves generic parameter types 解析泛型参数类型- Parameters:
executable- the executable | 可执行对象targetClass- the target class | 目标类- Returns:
- the resolved types | 解析后的类型数组
-
resolveType
-
newArray
-
newArray
-
getArrayLength
Gets array length 获取数组长度- Parameters:
array- the array | 数组- Returns:
- the length | 长度
-
getArrayElement
-
setArrayElement
-
unwrapInvocationTargetException
-
sneakyThrow
Rethrows exception as unchecked 将异常作为非受检异常重新抛出- Type Parameters:
T- the return type | 返回类型- Parameters:
ex- the exception | 异常- Returns:
- never returns | 永不返回
- Throws:
T
-
isAssignable
-
isAssignable
-
primitiveToWrapper
-
wrapperToPrimitive
-
getDefaultValue
-