Class OpenReflect
java.lang.Object
cloud.opencode.base.reflect.OpenReflect
Reflection Main Facade Entry Class
反射主门面入口类
Unified entry point for all reflection operations. Delegates to specialized facade classes.
所有反射操作的统一入口点。 委托给专门的门面类。
Features | 主要功能:
- Class operations - 类操作
- Field operations - 字段操作
- Method operations - 方法操作
- Constructor operations - 构造器操作
- Annotation operations - 注解操作
- Type operations - 类型操作
Usage Examples | 使用示例:
// Load class and create instance
Class<?> clazz = OpenReflect.forName("com.example.User");
User user = OpenReflect.newInstance(User.class);
// Read/write fields
Object name = OpenReflect.readField(user, "name");
OpenReflect.writeField(user, "name", "Alice");
// Invoke method
Object result = OpenReflect.invokeMethod(user, "getName");
Security | 安全性:
- Thread-safe: Yes (stateless facade, delegates to thread-safe classes) - 线程安全: 是(无状态门面,委托给线程安全类)
- Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanclassExists(String className) Checks if class exists 检查类是否存在static <A extends Annotation>
Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationClass) Finds an annotation (Optional) 查找注解(Optional)static Class<?> Loads a class by name 按名称加载类forNameSafe(String className) Loads a class by name (Optional) 按名称加载类(Optional)getAllFields(Class<?> clazz) Gets all fields (including inherited) 获取所有字段(包含继承)getAllInterfaces(Class<?> clazz) Gets all interfaces (including inherited) 获取所有接口(包含继承)getAllMethods(Class<?> clazz) Gets all methods (including inherited) 获取所有方法(包含继承)getAllSuperclasses(Class<?> clazz) Gets all superclasses 获取所有父类static <A extends Annotation>
AgetAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation 获取注解static StringgetCanonicalNameOrName(Class<?> clazz) Gets the canonical name or falls back to name 获取规范名称或回退到名称getClassHierarchy(Class<?> clazz) Gets the full class hierarchy 获取完整类层次结构static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes) Gets a constructor 获取构造器static FieldGets a field 获取字段static MethodGets a method 获取方法static StringgetSimpleName(Class<?> clazz) Gets the simple name of a class (handles arrays and inner classes) 获取类的简单名称(处理数组和内部类)static booleanhasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationClass) Checks if annotation is present 检查注解是否存在static ObjectinvokeMethod(Object target, String methodName, Object... args) Invokes a method 调用方法static ObjectinvokeStaticMethod(Class<?> clazz, String methodName, Object... args) Invokes a static method 调用静态方法static booleanChecks if final 检查是否finalstatic booleanisPrimitive(Class<?> clazz) Checks if class is primitive 检查是否为原始类型static booleanChecks if private 检查是否privatestatic booleanChecks if public 检查是否publicstatic booleanChecks if class is a record 检查是否为Record类static booleanisSamePackage(Class<?> class1, Class<?> class2) Checks if two classes are in the same package 检查两个类是否在同一包中static booleanChecks if class is sealed 检查是否为密封类static booleanChecks if static 检查是否staticstatic booleanChecks if class is wrapper 检查是否为包装类型static <T extends AccessibleObject>
TmakeAccessible(T accessible) Makes an accessible object accessible 设置可访问对象为可访问static <T> TnewInstance(Class<T> clazz) Creates a new instance 创建新实例static <T> TnewInstance(Class<T> clazz, Object... args) Creates a new instance with arguments 创建新实例(带参数)static Class<?> primitiveToWrapper(Class<?> primitiveType) Converts primitive to wrapper 原始类型转包装类型static ObjectReads a field value 读取字段值static <T> Invokable<T, T> toInvokable(Constructor<T> constructor) Creates an Invokable from a constructor 从构造器创建InvokabletoInvokable(Method method) Creates an Invokable from a method 从方法创建Invokablestatic <T> TypeToken<T> Creates a TypeToken for a class 为类创建TypeTokenstatic Class<?> wrapperToPrimitive(Class<?> wrapperType) Converts wrapper to primitive 包装类型转原始类型static voidwriteField(Object target, String fieldName, Object value) Writes a field value 写入字段值
-
Method Details
-
forName
Loads a class by name 按名称加载类- Parameters:
className- the class name | 类名- Returns:
- the class | 类
- Throws:
OpenReflectException- if not found | 如果未找到
-
forNameSafe
-
classExists
Checks if class exists 检查类是否存在- Parameters:
className- the class name | 类名- Returns:
- true if exists | 如果存在返回true
-
typeOf
-
getField
-
getAllFields
-
readField
-
writeField
-
getMethod
-
getAllMethods
-
invokeMethod
-
invokeStaticMethod
-
getConstructor
Gets a constructor 获取构造器- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类parameterTypes- the parameter types | 参数类型- Returns:
- the constructor | 构造器
-
newInstance
Creates a new instance 创建新实例- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- the instance | 实例
-
newInstance
-
getAnnotation
public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation 获取注解- Type Parameters:
A- the annotation type | 注解类型- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- the annotation or null | 注解或null
-
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationClass) Finds an annotation (Optional) 查找注解(Optional)- Type Parameters:
A- the annotation type | 注解类型- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- Optional of annotation | 注解的Optional
-
hasAnnotation
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationClass) Checks if annotation is present 检查注解是否存在- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- true if present | 如果存在返回true
-
isPublic
Checks if public 检查是否public- Parameters:
member- the member | 成员- Returns:
- true if public | 如果是public返回true
-
isPrivate
Checks if private 检查是否private- Parameters:
member- the member | 成员- Returns:
- true if private | 如果是private返回true
-
isStatic
Checks if static 检查是否static- Parameters:
member- the member | 成员- Returns:
- true if static | 如果是static返回true
-
isFinal
Checks if final 检查是否final- Parameters:
member- the member | 成员- Returns:
- true if final | 如果是final返回true
-
toInvokable
-
toInvokable
Creates an Invokable from a constructor 从构造器创建Invokable- Type Parameters:
T- the type | 类型- Parameters:
constructor- the constructor | 构造器- Returns:
- the Invokable | Invokable
-
isPrimitive
Checks if class is primitive 检查是否为原始类型- Parameters:
clazz- the class | 类- Returns:
- true if primitive | 如果是原始类型返回true
-
isWrapper
Checks if class is wrapper 检查是否为包装类型- Parameters:
clazz- the class | 类- Returns:
- true if wrapper | 如果是包装类型返回true
-
isRecord
Checks if class is a record 检查是否为Record类- Parameters:
clazz- the class | 类- Returns:
- true if record | 如果是Record返回true
-
isSealed
Checks if class is sealed 检查是否为密封类- Parameters:
clazz- the class | 类- Returns:
- true if sealed | 如果是密封类返回true
-
primitiveToWrapper
-
wrapperToPrimitive
-
getAllSuperclasses
-
getAllInterfaces
-
getClassHierarchy
-
makeAccessible
Makes an accessible object accessible 设置可访问对象为可访问- Type Parameters:
T- the accessible type | 可访问类型- Parameters:
accessible- the accessible object | 可访问对象- Returns:
- the accessible object | 可访问对象
-
isSamePackage
-
getSimpleName
-
getCanonicalNameOrName
-