Class MethodUtil

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

public final class MethodUtil extends Object
Method Utility Class - Method reflection operations 方法工具类 - 方法反射操作

Provides utilities for method discovery, getter/setter detection, and invocation.

提供方法发现、Getter/Setter 检测和调用操作工具。

Features | 主要功能:

  • Get all/declared methods (with inheritance) - 获取全部/声明方法(含继承)
  • Getter/Setter detection and property extraction - Getter/Setter 检测和属性提取
  • Filter by annotation, return type, modifier - 按注解、返回类型、修饰符过滤
  • Method invocation (instance/static) - 方法调用(实例/静态)

Usage Examples | 使用示例:

// Get getter methods - 获取 Getter 方法
List<Method> getters = MethodUtil.getGetterMethods(User.class);

// Check if getter - 检查是否为 Getter
boolean isGetter = MethodUtil.isGetter(method);

// Get property name - 获取属性名
String prop = MethodUtil.getPropertyNameFromGetter(method);

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: No - 空值安全: 否

Performance | 性能特性:

  • Time complexity: O(n) where n = declared methods - O(n), n为声明的方法数
  • Space complexity: O(1) per invocation - 每次调用 O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getAllMethods

      public static List<Method> getAllMethods(Class<?> clazz)
      Gets all methods (including superclass) 获取所有方法(包括父类)
    • getDeclaredMethods

      public static List<Method> getDeclaredMethods(Class<?> clazz)
      Gets declared methods (excluding superclass) 获取声明的方法(不包括父类)
    • getMethodsByName

      public static List<Method> getMethodsByName(Class<?> clazz, String name)
      Gets methods by name 按名称获取方法
    • getMethod

      public static Optional<Method> getMethod(Class<?> clazz, String name, Class<?>... paramTypes)
      Gets a method (exact parameter type match) 获取方法(精确匹配参数类型)
    • getMethodsWithAnnotation

      public static List<Method> getMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
      Gets methods with the specified annotation 获取带有指定注解的方法
    • getMethodsByReturnType

      public static List<Method> getMethodsByReturnType(Class<?> clazz, Class<?> returnType)
      Gets methods with the specified return type 获取指定返回类型的方法
    • getGetterMethods

      public static List<Method> getGetterMethods(Class<?> clazz)
      Gets getter methods 获取 Getter 方法
    • getSetterMethods

      public static List<Method> getSetterMethods(Class<?> clazz)
      Gets setter methods 获取 Setter 方法
    • getStaticMethods

      public static List<Method> getStaticMethods(Class<?> clazz)
      Gets static methods 获取静态方法
    • getPublicMethods

      public static List<Method> getPublicMethods(Class<?> clazz)
      Gets public methods 获取公共方法
    • isGetter

      public static boolean isGetter(Method method)
      Checks if the method is a getter 检查是否为 Getter 方法
    • isSetter

      public static boolean isSetter(Method method)
      Checks if the method is a setter 检查是否为 Setter 方法
    • getPropertyNameFromGetter

      public static String getPropertyNameFromGetter(Method method)
      Gets the property name from a getter method name 从 Getter 方法名获取属性名
    • getPropertyNameFromSetter

      public static String getPropertyNameFromSetter(Method method)
      Gets the property name from a setter method name 从 Setter 方法名获取属性名
    • getGenericReturnType

      public static Type getGenericReturnType(Method method)
      Gets the generic return type of a method 获取方法的泛型返回类型
    • getGenericParameterTypes

      public static Type[] getGenericParameterTypes(Method method)
      Gets the generic parameter types of a method 获取方法的泛型参数类型
    • invoke

      public static <T> T invoke(Object obj, Method method, Object... args)
      Invokes a method 调用方法
    • invokeStatic

      public static <T> T invokeStatic(Method method, Object... args)
      Invokes a static method 调用静态方法
    • isAbstract

      public static boolean isAbstract(Method method)
      Checks if the method is abstract 检查方法是否为 abstract
    • isSynchronized

      public static boolean isSynchronized(Method method)
      Checks if the method is synchronized 检查方法是否为 synchronized
    • isNative

      public static boolean isNative(Method method)
      Checks if the method is native 检查方法是否为 native
    • isDefault

      public static boolean isDefault(Method method)
      Checks if the method is a default interface method 检查方法是否为 default 接口方法