Class OpenMethod

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

public final class OpenMethod extends Object
Method Facade Entry Class 方法门面入口类

Provides common method operations API, internally delegates to MethodUtil. Similar to Commons Lang MethodUtils.

提供常用方法操作API,内部委托给MethodUtil。 对标Commons Lang MethodUtils。

Features | 主要功能:

  • Method retrieval (with inheritance) - 方法获取(含继承)
  • Method invocation - 方法调用
  • Getter/Setter detection - Getter/Setter检测
  • Annotation-based filtering - 基于注解的过滤

Usage Examples | 使用示例:

// Invoke a method
Object result = OpenMethod.invokeMethod(target, "getName");

// Get all methods with annotation
List<Method> methods = OpenMethod.getMethodsWithAnnotation(MyClass.class, Override.class);

// Check if method is getter
boolean isGetter = OpenMethod.isGetter(method);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • 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

    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Gets a method (exact parameter types) 获取方法(精确参数类型匹配)
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method | 方法
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, boolean forceAccess, Class<?>... parameterTypes)
      Gets a method with optional force access 获取方法(可选强制访问)
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      forceAccess - whether to force access | 是否强制访问
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method | 方法
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getMatchingMethod

      public static Method getMatchingMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Gets a matching method (compatible parameter types) 获取匹配方法(兼容参数类型)
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method | 方法
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getAllMethods

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

      public static List<Method> getDeclaredMethods(Class<?> clazz)
      Gets declared methods (not inherited) 获取声明方法(不含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      list of methods | 方法列表
    • getMethodsWithAnnotation

      public static List<Method> getMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass)
      Gets methods with specific annotation 按注解获取方法
      Parameters:
      clazz - the class | 类
      annotationClass - the annotation class | 注解类
      Returns:
      list of methods | 方法列表
    • getOverloadMethods

      public static List<Method> getOverloadMethods(Class<?> clazz, String methodName)
      Gets overloaded methods by name 按名称获取所有重载方法
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      Returns:
      list of methods | 方法列表
    • getGetters

      public static List<Method> getGetters(Class<?> clazz)
      Gets all getter methods 获取所有Getter方法
      Parameters:
      clazz - the class | 类
      Returns:
      list of getter methods | Getter方法列表
    • getSetters

      public static List<Method> getSetters(Class<?> clazz)
      Gets all setter methods 获取所有Setter方法
      Parameters:
      clazz - the class | 类
      Returns:
      list of setter methods | Setter方法列表
    • 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 | 结果
    • invokeMethod

      public static Object invokeMethod(Object target, boolean forceAccess, String methodName, Object... args)
      Invokes a method with optional force access 调用方法(可选强制访问)
      Parameters:
      target - the target object | 目标对象
      forceAccess - whether to force access | 是否强制访问
      methodName - the method name | 方法名
      args - the arguments | 参数
      Returns:
      the result | 结果
    • invokeMethod

      public static <T> T invokeMethod(Object target, String methodName, Class<T> returnType, Object... args)
      Invokes a method with type-safe return 调用方法(泛型安全)
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      target - the target object | 目标对象
      methodName - the method name | 方法名
      returnType - the return type | 返回类型
      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 | 结果
    • invokeStaticMethod

      public static <T> T invokeStaticMethod(Class<?> clazz, String methodName, Class<T> returnType, Object... args)
      Invokes a static method with type-safe return 调用静态方法(泛型安全)
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      returnType - the return type | 返回类型
      args - the arguments | 参数
      Returns:
      the result | 结果
    • hasMethod

      public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Checks if a method exists 检查方法是否存在
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      true if exists | 如果存在返回true
    • isGetter

      public static boolean isGetter(Method method)
      Checks if a method is a getter 检查是否为Getter方法
      Parameters:
      method - the method | 方法
      Returns:
      true if getter | 如果是Getter返回true
    • isSetter

      public static boolean isSetter(Method method)
      Checks if a method is a setter 检查是否为Setter方法
      Parameters:
      method - the method | 方法
      Returns:
      true if setter | 如果是Setter返回true
    • forEach

      public static void forEach(Class<?> clazz, Consumer<Method> action)
      Iterates all methods 遍历所有方法
      Parameters:
      clazz - the class | 类
      action - the action | 操作
    • findFirst

      public static Optional<Method> findFirst(Class<?> clazz, Predicate<Method> predicate)
      Finds first matching method 查找第一个匹配方法
      Parameters:
      clazz - the class | 类
      predicate - the predicate | 谓词
      Returns:
      Optional of method | 方法的Optional
    • stream

      public static Stream<Method> stream(Class<?> clazz)
      Creates a method stream 创建方法流
      Parameters:
      clazz - the class | 类
      Returns:
      stream of methods | 方法流
    • toInvokable

      public static <T> Invokable<T,Object> toInvokable(Method method)
      Converts to Invokable 转为Invokable
      Type Parameters:
      T - the declaring class type | 声明类类型
      Parameters:
      method - the method | 方法
      Returns:
      Invokable | Invokable