Class MethodUtil

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

public final class MethodUtil extends Object
Method Utility Class 方法工具类

Provides low-level method operation utilities with caching.

提供带缓存的底层方法操作工具。

Features | 主要功能:

  • Method discovery with caching - 带缓存的方法发现
  • Inherited method resolution - 继承方法解析
  • Method filtering by name, annotation, modifier - 按名称、注解、修饰符过滤方法

Usage Examples | 使用示例:

List<Method> methods = MethodUtil.getAllMethods(User.class);
Method method = MethodUtil.getMethod(User.class, "getName");

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(m) for first access where m is the number of methods (including inherited) - 时间复杂度: 缓存命中时 O(1);首次访问为 O(m),m为方法数量(含继承)
  • Space complexity: O(m) for the cached methods per class - 空间复杂度: O(m),每类缓存方法
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getDeclaredMethods

      public static Method[] getDeclaredMethods(Class<?> clazz)
      Gets all declared methods (no inheritance) 获取所有声明的方法(不含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      array of methods | 方法数组
    • getAllMethods

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

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Gets method by name and parameter types (cached) 按名称和参数类型获取方法(缓存)
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method or null | 方法或null
    • getMethodOrThrow

      public static Method getMethodOrThrow(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Gets method or throws exception 获取方法或抛出异常
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      parameterTypes - the parameter types | 参数类型
      Returns:
      the method | 方法
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getMethodsByName

      public static List<Method> getMethodsByName(Class<?> clazz, String methodName)
      Finds method by name (any parameters) 按名称查找方法(任意参数)
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      Returns:
      list of methods | 方法列表
    • getMethods

      public static List<Method> getMethods(Class<?> clazz, Predicate<Method> predicate)
      Gets methods matching predicate 获取匹配条件的方法
      Parameters:
      clazz - the class | 类
      predicate - the predicate | 谓词
      Returns:
      list of matching methods | 匹配的方法列表
    • getMethodsWithAnnotation

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

      public static List<Method> getMethodsWithReturnType(Class<?> clazz, Class<?> returnType)
      Gets methods with specific return type 获取特定返回类型的方法
      Parameters:
      clazz - the class | 类
      returnType - the return type | 返回类型
      Returns:
      list of methods | 方法列表
    • getGetters

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

      public static List<Method> getSetters(Class<?> clazz)
      Gets setter methods 获取setter方法
      Parameters:
      clazz - the class | 类
      Returns:
      list of methods | 方法列表
    • invoke

      public static Object invoke(Method method, Object target, Object... args)
      Invokes method 调用方法
      Parameters:
      method - the method | 方法
      target - the target object | 目标对象
      args - the arguments | 参数
      Returns:
      the result | 结果
    • invoke

      public static <T> T invoke(Method method, Object target, Class<T> returnType, Object... args)
      Invokes method with type cast 调用方法并转型
      Type Parameters:
      T - the type | 类型
      Parameters:
      method - the method | 方法
      target - the target object | 目标对象
      returnType - the return type | 返回类型
      args - the arguments | 参数
      Returns:
      the result | 结果
    • invokeStatic

      public static Object invokeStatic(Method method, Object... args)
      Invokes static method 调用静态方法
      Parameters:
      method - the method | 方法
      args - the arguments | 参数
      Returns:
      the result | 结果
    • getReturnType

      public static Class<?> getReturnType(Method method)
      Gets method return type 获取方法返回类型
      Parameters:
      method - the method | 方法
      Returns:
      the return type | 返回类型
    • getGenericReturnType

      public static Type getGenericReturnType(Method method)
      Gets method generic return type 获取方法泛型返回类型
      Parameters:
      method - the method | 方法
      Returns:
      the generic return type | 泛型返回类型
    • getParameterTypes

      public static Class<?>[] getParameterTypes(Method method)
      Gets method parameter types 获取方法参数类型
      Parameters:
      method - the method | 方法
      Returns:
      the parameter types | 参数类型
    • getParameterCount

      public static int getParameterCount(Method method)
      Gets method parameter count 获取方法参数数量
      Parameters:
      method - the method | 方法
      Returns:
      the parameter count | 参数数量
    • getMethodSignature

      public static String getMethodSignature(Method method)
      Gets method signature 获取方法签名
      Parameters:
      method - the method | 方法
      Returns:
      the signature | 签名
    • isGetter

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

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

      public static String getPropertyName(Method method)
      Gets property name from getter/setter 从getter/setter获取属性名
      Parameters:
      method - the method | 方法
      Returns:
      the property name or null | 属性名或null
    • isStatic

      public static boolean isStatic(Method method)
      Checks if method is static 检查方法是否为静态
      Parameters:
      method - the method | 方法
      Returns:
      true if static | 如果是静态返回true
    • isAbstract

      public static boolean isAbstract(Method method)
      Checks if method is abstract 检查方法是否为抽象
      Parameters:
      method - the method | 方法
      Returns:
      true if abstract | 如果是抽象返回true
    • isDefault

      public static boolean isDefault(Method method)
      Checks if method is default interface method 检查是否为接口默认方法
      Parameters:
      method - the method | 方法
      Returns:
      true if default | 如果是默认方法返回true
    • clearCache

      public static void clearCache()
      Clears method cache 清除方法缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for specific class 清除特定类的缓存
      Parameters:
      clazz - the class | 类