Class MethodUtil
java.lang.Object
cloud.opencode.base.reflect.MethodUtil
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 Summary
Modifier and TypeMethodDescriptionstatic voidClears method cache 清除方法缓存static voidclearCache(Class<?> clazz) Clears cache for specific class 清除特定类的缓存getAllMethods(Class<?> clazz) Gets all methods including inherited (cached) 获取所有方法包含继承(缓存)static Method[]getDeclaredMethods(Class<?> clazz) Gets all declared methods (no inheritance) 获取所有声明的方法(不含继承)static TypegetGenericReturnType(Method method) Gets method generic return type 获取方法泛型返回类型getGetters(Class<?> clazz) Gets getter methods 获取getter方法static MethodGets method by name and parameter types (cached) 按名称和参数类型获取方法(缓存)static MethodgetMethodOrThrow(Class<?> clazz, String methodName, Class<?>... parameterTypes) Gets method or throws exception 获取方法或抛出异常getMethods(Class<?> clazz, Predicate<Method> predicate) Gets methods matching predicate 获取匹配条件的方法getMethodsByName(Class<?> clazz, String methodName) Finds method by name (any parameters) 按名称查找方法(任意参数)static StringgetMethodSignature(Method method) Gets method signature 获取方法签名getMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets methods with annotation 获取带注解的方法getMethodsWithReturnType(Class<?> clazz, Class<?> returnType) Gets methods with specific return type 获取特定返回类型的方法static intgetParameterCount(Method method) Gets method parameter count 获取方法参数数量static Class<?>[]getParameterTypes(Method method) Gets method parameter types 获取方法参数类型static StringgetPropertyName(Method method) Gets property name from getter/setter 从getter/setter获取属性名static Class<?> getReturnType(Method method) Gets method return type 获取方法返回类型getSetters(Class<?> clazz) Gets setter methods 获取setter方法static <T> TInvokes method with type cast 调用方法并转型static ObjectInvokes method 调用方法static ObjectinvokeStatic(Method method, Object... args) Invokes static method 调用静态方法static booleanisAbstract(Method method) Checks if method is abstract 检查方法是否为抽象static booleanChecks if method is default interface method 检查是否为接口默认方法static booleanChecks if method is getter 检查是否为getter方法static booleanChecks if method is setter 检查是否为setter方法static booleanChecks if method is static 检查方法是否为静态
-
Method Details
-
getDeclaredMethods
-
getAllMethods
-
getMethod
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
-
getMethods
-
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
-
getGetters
-
getSetters
-
invoke
-
invoke
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
-
getReturnType
-
getGenericReturnType
-
getParameterTypes
-
getParameterCount
Gets method parameter count 获取方法参数数量- Parameters:
method- the method | 方法- Returns:
- the parameter count | 参数数量
-
getMethodSignature
-
isGetter
Checks if method is getter 检查是否为getter方法- Parameters:
method- the method | 方法- Returns:
- true if getter | 如果是getter返回true
-
isSetter
Checks if method is setter 检查是否为setter方法- Parameters:
method- the method | 方法- Returns:
- true if setter | 如果是setter返回true
-
getPropertyName
-
isStatic
Checks if method is static 检查方法是否为静态- Parameters:
method- the method | 方法- Returns:
- true if static | 如果是静态返回true
-
isAbstract
Checks if method is abstract 检查方法是否为抽象- Parameters:
method- the method | 方法- Returns:
- true if abstract | 如果是抽象返回true
-
isDefault
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
Clears cache for specific class 清除特定类的缓存- Parameters:
clazz- the class | 类
-