Class OpenReflect

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

public final class OpenReflect extends Object
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 Details

    • forName

      public static Class<?> forName(String className)
      Loads a class by name 按名称加载类
      Parameters:
      className - the class name | 类名
      Returns:
      the class | 类
      Throws:
      OpenReflectException - if not found | 如果未找到
    • forNameSafe

      public static Optional<Class<?>> forNameSafe(String className)
      Loads a class by name (Optional) 按名称加载类(Optional)
      Parameters:
      className - the class name | 类名
      Returns:
      Optional of class | 类的Optional
    • classExists

      public static boolean classExists(String className)
      Checks if class exists 检查类是否存在
      Parameters:
      className - the class name | 类名
      Returns:
      true if exists | 如果存在返回true
    • typeOf

      public static <T> TypeToken<T> typeOf(Class<T> clazz)
      Creates a TypeToken for a class 为类创建TypeToken
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the TypeToken | TypeToken
    • getField

      public static Field getField(Class<?> clazz, String fieldName)
      Gets a field 获取字段
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field | 字段
    • getAllFields

      public static List<Field> getAllFields(Class<?> clazz)
      Gets all fields (including inherited) 获取所有字段(包含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      list of fields | 字段列表
    • readField

      public static Object readField(Object target, String fieldName)
      Reads a field value 读取字段值
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      Returns:
      the value | 值
    • writeField

      public static void writeField(Object target, String fieldName, Object value)
      Writes a field value 写入字段值
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      value - the value | 值
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Gets a method 获取方法
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method | 方法
    • getAllMethods

      public static List<Method> getAllMethods(Class<?> clazz)
      Gets all methods (including inherited) 获取所有方法(包含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      list of methods | 方法列表
    • invokeMethod

      public static Object invokeMethod(Object target, String methodName, Object... args)
      Invokes a method 调用方法
      Parameters:
      target - the target object | 目标对象
      methodName - the method name | 方法名
      args - the arguments | 参数
      Returns:
      the result | 结果
    • invokeStaticMethod

      public static Object invokeStaticMethod(Class<?> clazz, String methodName, Object... args)
      Invokes a static method 调用静态方法
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      args - the arguments | 参数
      Returns:
      the result | 结果
    • getConstructor

      public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Gets a constructor 获取构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      Returns:
      the constructor | 构造器
    • newInstance

      public static <T> T newInstance(Class<T> clazz)
      Creates a new instance 创建新实例
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the instance | 实例
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Object... args)
      Creates a new instance with arguments 创建新实例(带参数)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • 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

      public static boolean isPublic(Member member)
      Checks if public 检查是否public
      Parameters:
      member - the member | 成员
      Returns:
      true if public | 如果是public返回true
    • isPrivate

      public static boolean isPrivate(Member member)
      Checks if private 检查是否private
      Parameters:
      member - the member | 成员
      Returns:
      true if private | 如果是private返回true
    • isStatic

      public static boolean isStatic(Member member)
      Checks if static 检查是否static
      Parameters:
      member - the member | 成员
      Returns:
      true if static | 如果是static返回true
    • isFinal

      public static boolean isFinal(Member member)
      Checks if final 检查是否final
      Parameters:
      member - the member | 成员
      Returns:
      true if final | 如果是final返回true
    • toInvokable

      public static <T> Invokable<T,Object> toInvokable(Method method)
      Creates an Invokable from a method 从方法创建Invokable
      Type Parameters:
      T - the declaring class type | 声明类类型
      Parameters:
      method - the method | 方法
      Returns:
      the Invokable | Invokable
    • toInvokable

      public static <T> Invokable<T,T> toInvokable(Constructor<T> constructor)
      Creates an Invokable from a constructor 从构造器创建Invokable
      Type Parameters:
      T - the type | 类型
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      the Invokable | Invokable
    • isPrimitive

      public static boolean isPrimitive(Class<?> clazz)
      Checks if class is primitive 检查是否为原始类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if primitive | 如果是原始类型返回true
    • isWrapper

      public static boolean isWrapper(Class<?> clazz)
      Checks if class is wrapper 检查是否为包装类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if wrapper | 如果是包装类型返回true
    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Checks if class is a record 检查是否为Record类
      Parameters:
      clazz - the class | 类
      Returns:
      true if record | 如果是Record返回true
    • isSealed

      public static boolean isSealed(Class<?> clazz)
      Checks if class is sealed 检查是否为密封类
      Parameters:
      clazz - the class | 类
      Returns:
      true if sealed | 如果是密封类返回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 | 原始类型
    • getAllSuperclasses

      public static List<Class<?>> getAllSuperclasses(Class<?> clazz)
      Gets all superclasses 获取所有父类
      Parameters:
      clazz - the class | 类
      Returns:
      list of superclasses | 父类列表
    • getAllInterfaces

      public static List<Class<?>> getAllInterfaces(Class<?> clazz)
      Gets all interfaces (including inherited) 获取所有接口(包含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      list of interfaces | 接口列表
    • getClassHierarchy

      public static List<Class<?>> getClassHierarchy(Class<?> clazz)
      Gets the full class hierarchy 获取完整类层次结构
      Parameters:
      clazz - the class | 类
      Returns:
      list of all classes in hierarchy | 层次结构中的所有类
    • makeAccessible

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

      public static boolean isSamePackage(Class<?> class1, Class<?> class2)
      Checks if two classes are in the same package 检查两个类是否在同一包中
      Parameters:
      class1 - the first class | 第一个类
      class2 - the second class | 第二个类
      Returns:
      true if same package | 如果在同一包返回true
    • getSimpleName

      public static String getSimpleName(Class<?> clazz)
      Gets the simple name of a class (handles arrays and inner classes) 获取类的简单名称(处理数组和内部类)
      Parameters:
      clazz - the class | 类
      Returns:
      the simple name | 简单名称
    • getCanonicalNameOrName

      public static String getCanonicalNameOrName(Class<?> clazz)
      Gets the canonical name or falls back to name 获取规范名称或回退到名称
      Parameters:
      clazz - the class | 类
      Returns:
      the canonical name or name | 规范名称或名称