| Constructor and Description |
|---|
MethodUtil() |
| Modifier and Type | Method and Description |
|---|---|
static Object[] |
actualArgs(Method method,
Object[] args)
检查用户传入参数:
1、忽略多余的参数
2、参数不够补齐默认值
3、通过NullWrapperBean传递的参数,会直接赋值null
4、传入参数为null,但是目标参数类型为原始类型,做转换
5、传入参数类型不对应,尝试转换类型
|
static Method[] |
getDeclaredMethods(Class<?> clazz)
获得类中所有直接声明方法,不包括其父类中的方法
|
static Method[] |
getDeclaredMethods(Class<?> clazz,
Predicate<Method> predicate)
获得类中所有直接声明方法,不包括其父类中的方法
|
static Method |
getMethod(Class<?> clazz,
boolean ignoreCase,
String methodName,
Class<?>... paramTypes)
查找指定方法 如果找不到对应的方法则返回
null此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回 null。 |
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... paramTypes)
查找指定方法 如果找不到对应的方法则返回
null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。 |
static Method |
getMethod(Method[] methods,
boolean ignoreCase,
String methodName,
Class<?>... paramTypes)
查找指定方法 如果找不到对应的方法则返回
null此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回 null。 |
static Method |
getMethod(Method[] methods,
Predicate<Method> predicate)
通过给定的条件(Predicate)从一个Method数组中查找第一个匹配的方法。
|
static Method |
getMethodByName(Class<?> clazz,
boolean ignoreCase,
String methodName)
按照方法名查找指定方法名的方法,只返回匹配到的第一个方法,如果找不到对应的方法则返回
null
此方法只检查方法名是否一致,并不检查参数的一致性。 |
static Method |
getMethodByName(Class<?> clazz,
String methodName)
按照方法名查找指定方法名的方法,只返回匹配到的第一个方法,如果找不到对应的方法则返回
null
此方法只检查方法名是否一致,并不检查参数的一致性。 |
static Method |
getMethodByNameIgnoreCase(Class<?> clazz,
String methodName)
按照方法名查找指定方法名的方法,只返回匹配到的第一个方法,如果找不到对应的方法则返回
null
此方法只检查方法名是否一致(忽略大小写),并不检查参数的一致性。 |
static Method |
getMethodIgnoreCase(Class<?> clazz,
String methodName,
Class<?>... paramTypes)
忽略大小写查找指定方法,如果找不到对应的方法则返回
null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。 |
static Set<String> |
getMethodNames(Class<?> clazz)
获得指定类中的Public方法名
去重重载的方法 |
static Method |
getMethodOfObj(Object obj,
String methodName,
Object... args)
查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回
null。 |
static Method[] |
getMethods(Class<?> clazz)
获得一个类中所有方法列表,包括其父类中的方法
|
static Method[] |
getMethods(Class<?> clazz,
Predicate<Method> predicate)
获得一个类中所有方法列表,包括其父类中的方法
|
static Method[] |
getMethodsDirectly(Class<?> beanClass,
boolean withSupers,
boolean withMethodFromObject)
获得一个类中所有方法列表,直接反射获取,无缓存
接口获取方法和默认方法,获取的方法包括: 本类中的所有方法(包括static方法) 父类中的所有方法(包括static方法) Object中(包括static方法) |
static Method |
getPublicMethod(Class<?> clazz,
boolean ignoreCase,
String methodName,
Class<?>... paramTypes)
查找指定Public方法 如果找不到对应的方法或方法不为public的则返回
null |
static Set<String> |
getPublicMethodNames(Class<?> clazz)
获得指定类本类及其父类中的Public方法名
去重重载的方法 |
static Method[] |
getPublicMethods(Class<?> clazz)
获得本类及其父类所有Public方法
|
static Method[] |
getPublicMethods(Class<?> clazz,
Predicate<Method> predicate)
获得本类及其父类所有Public方法
|
static <T> T |
invoke(Object obj,
Method method,
Object... args)
执行方法
对于用户传入参数会做必要检查,包括:
1、忽略多余的参数
2、参数不够补齐默认值
3、传入参数为null,但是目标参数类型为原始类型,做转换
|
static <T> T |
invoke(Object obj,
String methodName,
Object... args)
执行对象中指定方法
如果需要传递的参数为null,请使用NullWrapperBean来传递,不然会丢失类型信息
|
static <T> T |
invoke(String classNameWithMethodName,
boolean isSingleton,
Object... args)
执行方法
可执行Private方法,也可执行static方法 执行非static方法时,必须满足对象有默认构造方法 |
static <T> T |
invoke(String classNameWithMethodName,
Object[] args)
执行方法
可执行Private方法,也可执行static方法 执行非static方法时,必须满足对象有默认构造方法 非单例模式,如果是非静态方法,每次创建一个新对象 |
static <T> T |
invoke(String className,
String methodName,
boolean isSingleton,
Object... args)
执行方法
可执行Private方法,也可执行static方法 执行非static方法时,必须满足对象有默认构造方法 |
static <T> T |
invoke(String className,
String methodName,
Object[] args)
执行方法
可执行Private方法,也可执行static方法 执行非static方法时,必须满足对象有默认构造方法 非单例模式,如果是非静态方法,每次创建一个新对象 |
static <T> T |
invokeStatic(Method method,
Object... args)
执行静态方法
|
static <T> T |
invokeWithCheck(Object obj,
Method method,
Object... args)
执行方法
执行前要检查给定参数: 1. |
static boolean |
isEmptyParam(Method method)
是否为无参数方法
|
static boolean |
isEqualsMethod(Method method)
是否为equals方法
|
static boolean |
isGetter(Method method,
boolean ignoreCase)
检查给定方法是否为Getter方法,规则为:
方法参数必须为0个 方法名称不能是getClass "is"开头返回必须为boolean或Boolean 是否以“get” |
static boolean |
isGetterOrSetter(Method method,
boolean ignoreCase)
检查给定方法是否为Getter或者Setter方法,规则为:
方法参数必须为0个或1个 方法名称不能是getClass 如果是无参方法,则判断是否以“get”或“is”开头 如果方法参数1个,则判断是否以“set”开头 |
static boolean |
isGetterOrSetterIgnoreCase(Method method)
检查给定方法是否为Getter或者Setter方法,规则为:
方法参数必须为0个或1个 如果是无参方法,则判断是否以“get”或“is”开头 如果方法参数1个,则判断是否以“set”开头 |
static boolean |
isHashCodeMethod(Method method)
是否为hashCode方法
|
static boolean |
isSetter(Method method,
boolean ignoreCase)
检查给定方法是否为Setter方法,规则为:
方法参数必须为1个 判断是否以“set”开头 |
static boolean |
isToStringMethod(Method method)
是否为toString方法
|
public static Method getMethod(Method[] methods, Predicate<Method> predicate)
methods - Method数组,是被搜索的目标对象。predicate - 一个Predicate接口实例,用于定义查找方法的条件。public static Set<String> getPublicMethodNames(Class<?> clazz)
clazz - 类public static Method getPublicMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
nullclazz - 类ignoreCase - 是否忽略大小写methodName - 方法名paramTypes - 参数类型SecurityException - 无权访问抛出异常public static Method getMethodOfObj(Object obj, String methodName, Object... args) throws SecurityException
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。
obj - 被查找的对象,如果为null返回nullmethodName - 方法名,如果为空字符串返回nullargs - 参数SecurityException - 无访问权限抛出异常public static Method getMethodIgnoreCase(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。
clazz - 类,如果为null返回nullmethodName - 方法名,如果为空字符串返回nullparamTypes - 参数类型,指定参数类型如果是方法的子类也算SecurityException - 无权访问抛出异常public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。
clazz - 类,如果为null返回nullmethodName - 方法名,如果为空字符串返回nullparamTypes - 参数类型,指定参数类型如果是方法的子类也算SecurityException - 无权访问抛出异常public static Method getMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
nullnull。clazz - 类,如果为null返回nullignoreCase - 是否忽略大小写methodName - 方法名,如果为空字符串返回nullparamTypes - 参数类型,指定参数类型如果是方法的子类也算SecurityException - 无权访问抛出异常public static Method getMethod(Method[] methods, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
nullnull。methods - 方法列表ignoreCase - 是否忽略大小写methodName - 方法名,如果为空字符串返回nullparamTypes - 参数类型,指定参数类型如果是方法的子类也算SecurityException - 无权访问抛出异常public static Method getMethodByName(Class<?> clazz, String methodName) throws SecurityException
null
此方法只检查方法名是否一致,并不检查参数的一致性。
clazz - 类,如果为null返回nullmethodName - 方法名,如果为空字符串返回nullSecurityException - 无权访问抛出异常public static Method getMethodByNameIgnoreCase(Class<?> clazz, String methodName) throws SecurityException
null
此方法只检查方法名是否一致(忽略大小写),并不检查参数的一致性。
clazz - 类,如果为null返回nullmethodName - 方法名,如果为空字符串返回nullSecurityException - 无权访问抛出异常public static Method getMethodByName(Class<?> clazz, boolean ignoreCase, String methodName) throws SecurityException
null
此方法只检查方法名是否一致,并不检查参数的一致性。
clazz - 类,如果为null返回nullignoreCase - 是否忽略大小写methodName - 方法名,如果为空字符串返回nullSecurityException - 无权访问抛出异常public static Set<String> getMethodNames(Class<?> clazz) throws SecurityException
clazz - 类SecurityException - 安全异常public static Method[] getMethods(Class<?> clazz) throws SecurityException
clazz - 类,非nullSecurityException - 安全检查异常public static Method[] getMethods(Class<?> clazz, Predicate<Method> predicate) throws SecurityException
clazz - 类,非nullpredicate - 方法过滤器,null表示无过滤SecurityException - 安全检查异常public static Method[] getPublicMethods(Class<?> clazz)
clazz - 查找方法的类public static Method[] getPublicMethods(Class<?> clazz, Predicate<Method> predicate)
clazz - 查找方法的类predicate - 方法过滤器,null表示无过滤public static Method[] getDeclaredMethods(Class<?> clazz) throws SecurityException
clazz - 类,非nullSecurityException - 安全检查异常public static Method[] getDeclaredMethods(Class<?> clazz, Predicate<Method> predicate) throws SecurityException
clazz - 类,非nullpredicate - 方法过滤器,null表示无过滤SecurityException - 安全检查异常public static Method[] getMethodsDirectly(Class<?> beanClass, boolean withSupers, boolean withMethodFromObject) throws SecurityException
beanClass - 类或接口withSupers - 是否包括父类或接口的方法列表withMethodFromObject - 是否包括Object中的方法SecurityException - 安全检查异常public static boolean isEqualsMethod(Method method)
method - 方法public static boolean isHashCodeMethod(Method method)
method - 方法public static boolean isToStringMethod(Method method)
method - 方法public static boolean isEmptyParam(Method method)
method - 方法public static boolean isGetterOrSetterIgnoreCase(Method method)
method - 方法public static boolean isGetterOrSetter(Method method, boolean ignoreCase)
method - 方法ignoreCase - 是否忽略方法名的大小写public static boolean isSetter(Method method, boolean ignoreCase)
method - 方法ignoreCase - 是否忽略方法名的大小写public static boolean isGetter(Method method, boolean ignoreCase)
method - 方法ignoreCase - 是否忽略方法名的大小写public static <T> T invokeStatic(Method method, Object... args) throws HutoolException
T - 对象类型method - 方法(对象方法或static方法都可)args - 参数对象HutoolException - 多种异常包装public static <T> T invokeWithCheck(Object obj, Method method, Object... args) throws HutoolException
1. 参数个数是否与方法参数个数一致 2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
T - 返回对象类型obj - 对象,如果执行静态方法,此值为nullmethod - 方法(对象方法或static方法都可)args - 参数对象HutoolException - 一些列异常的包装public static <T> T invoke(Object obj, Method method, Object... args) throws HutoolException
对于用户传入参数会做必要检查,包括:
1、忽略多余的参数
2、参数不够补齐默认值
3、传入参数为null,但是目标参数类型为原始类型,做转换
T - 返回对象类型obj - 对象,如果执行静态方法,此值为nullmethod - 方法(对象方法或static方法都可)args - 参数对象HutoolException - 一些列异常的包装MethodHandleUtil.invoke(Object, Method, Object...)public static <T> T invoke(Object obj, String methodName, Object... args) throws HutoolException
T - 返回对象类型obj - 方法所在对象methodName - 方法名args - 参数列表HutoolException - IllegalAccessException包装NullWrapperBeanpublic static <T> T invoke(String classNameWithMethodName, Object[] args)
T - 对象类型classNameWithMethodName - 类名和方法名表达式,类名与方法名用.或#连接
例如:org.dromara.hutool.core.text.StrUtil.isEmpty 或 org.dromara.hutool.core.text.StrUtil#isEmptyargs - 参数,必须严格对应指定方法的参数类型和数量public static <T> T invoke(String classNameWithMethodName, boolean isSingleton, Object... args)
T - 对象类型classNameWithMethodName - 类名和方法名表达式,
例如:org.dromara.hutool.core.text.StrUtil#isEmpty或org.dromara.hutool.core.text.StrUtil.isEmptyisSingleton - 是否为单例对象,如果此参数为false,每次执行方法时创建一个新对象args - 参数,必须严格对应指定方法的参数类型和数量public static <T> T invoke(String className, String methodName, Object[] args)
T - 对象类型className - 类名,完整类路径methodName - 方法名args - 参数,必须严格对应指定方法的参数类型和数量public static <T> T invoke(String className, String methodName, boolean isSingleton, Object... args)
T - 对象类型className - 类名,完整类路径methodName - 方法名isSingleton - 是否为单例对象,如果此参数为false,每次执行方法时创建一个新对象args - 参数,必须严格对应指定方法的参数类型和数量Copyright © 2025. All rights reserved.