public final class MethodUtil extends Object
这两个是调用常规方法的
注意,底层调用的是MethodUtils.invokeMethod,这个方法会调用MethodUtils.getMatchingAccessibleMethod获得最佳匹配方法下面两个是调用静态方法的:
MethodUtils:如果你要调用精准的方法,可以使用
MethodUtils原生方法:
MethodUtils.invokeExactMethod(Object, String)MethodUtils.invokeExactMethod(Object, String, Object...)MethodUtils.invokeExactMethod(Object, String, Object[], Class[])MethodUtils.invokeExactStaticMethod(Class, String, Object...)MethodUtils.invokeExactStaticMethod(Class, String, Object[], Class[])当然,你还可以调用
MethodUtils其他方法:
MethodUtils.getAccessibleMethod(java.lang.reflect.Method)MethodUtils.getAccessibleMethod(Class, String, Class...)MethodUtils.getMatchingAccessibleMethod(Class, String, Class...)MethodUtils.getMethodsListWithAnnotation(Class, Class)获得一个类中,指定泛型的方法(集合形式),比较实用MethodUtils.getMethodsWithAnnotation(Class, Class)获得一个类中,指定泛型的方法(数组形式),比较实用MethodUtils.getOverrideHierarchy(java.lang.reflect.Method, org.apache.commons.lang3.ClassUtils.Interfaces)
当然,您也可以调用 java 原生态, 需要注意的是:
方法 说明 Class.getMethods()返回一个包含某些 Method 对象的数组, 反映此 Class 所表示的类或接口(包括由该类或接口声明的以及继承的类或接口)的公共(public) member 方法.
返回数组中的元素没有排序.
如果类声明了带有相同参数类型的多个公共成员方法,则它们都会包含在返回的数组中.
如果此 Class 对象表示没有公共成员方法的类或接口,或者表示一个基本类型或 void,则此方法返回长度为 0 的数组.
类初始化方法<clinit>不包含在返回的数组中.Class.getDeclaredMethods()返回 Method 对象的一个数组, 反映此 Class 对象表示的类或接口声明的所有方法,
如果该类声明带有相同参数类型的多个公共成员方法,则它们都包含在返回的数组中.
包括公共(public) 、保护(protected) 、默认(包)访问(default (package) access) 和私有方法(private) ,
但 不包括继承(inherited)的方法.返回数组中的元素没有排序.
如果该类或接口不声明任何方法,或者此 Class 对象表示一个基本类型、一个数组类或 void,则此方法返回一个长度为 0 的数组.
类初始化方法<clinit>包含在返回数组中.
MethodUtils,
ClassUtils.getPublicMethod(Class, String, Class...),
jdk1.6 ServiceLoader| Modifier and Type | Method and Description |
|---|---|
static <T> T |
invokeMethod(Object obj,
String methodName,
Object... params)
执行指定对象
object 的指定方法 methodName. |
static <T> T |
invokeMethod(Object object,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
执行指定对象
object 的指定方法 methodName. |
static <T> T |
invokeStaticMethod(Class<?> klass,
String staticMethodName,
Object... params)
执行指定类型
klass 的指定静态方法 staticMethodName (同时支持 私有静态方法). |
static <T> T |
invokeStaticMethod(Class<?> klass,
String staticMethodName,
Object[] args,
Class<?>[] parameterTypes)
执行指定类型
klass 的指定静态方法 staticMethodName (同时支持 私有静态方法). |
public static <T> T invokeMethod(Object obj, String methodName, Object... params)
object 的指定方法 methodName.
- 使用场景:适合比如上传下载 service有很多相同类型的方法,比如 importXX1,importXX2,对于这种,可以使用调用此方法来快速调用方法
- 支持调用对象父类方法
- 也支持调用自己对象或者父类的静态方法,结果相似于
invokeStaticMethod(Class, String, Object...),除了参数不同,一个是对象,一个是class,不过通常不建议这么使用- 调用的是
MethodUtils.invokeMethod(Object, String, Object...),内部调用的是Class.getMethods()来处理,不支持 调用private 方法- params将会转成包装类型来寻找method
示例:
如有以下的
public class OverloadMethod{ public String age(int age){ return "age int:" + age; } public String age(Integer age){ return "age Integer:" + age; } }测试调用public void testInvokeMethod(){ LOGGER.debug("" + MethodUtil.invokeMethod(new OverloadMethod(), "age", 5)); LOGGER.debug("" + MethodUtil.invokeMethod(new OverloadMethod(), "age", Integer.parseInt("5"))); }结果都是:age Integer:5 age Integer:5- 如果你要精准调用,请使用
invokeMethod(Object, String, Object[], Class[])示例:
Class<?>[] parameterTypes1 = { Integer.TYPE }; assertEquals("age int:5", MethodUtil.invokeMethod(new OverloadMethod(), "age", toArray(5), parameterTypes1));
T - the generic typeobj - 对象methodName - 方法名params - 参数obj 是null,抛出 NullPointerExceptionmethodName 是null,抛出 NullPointerExceptionmethodName 是blank,抛出 IllegalArgumentExceptionobj没有指定的methodName方法,抛出 ReflectExceptionparams 是null,系统内部会使用 empty 的class 数组params 是empty,表示不需要参数Method.invoke(Object, Object...),
ClassUtil.toClass(Object...),
MethodUtils.invokeMethod(Object, String, Object...),
MethodUtils.getMatchingAccessibleMethod(Class, String, Class...)public static <T> T invokeMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes)
object 的指定方法 methodName.
- 使用场景:适合比如上传下载 service有很多相同类型的方法,比如 importXX1,importXX2,对于这种,可以使用调用此方法来快速调用方法
- 支持调用对象父类方法
- 也支持调用自己对象或者父类的静态方法,结果相似于
invokeStaticMethod(Class, String, Object[], Class[]),除了参数不同,一个是对象,一个是class,不过通常不建议这么使用- 调用的是
MethodUtils.invokeMethod(Object, String, Object...),内部调用的是Class.getMethods()来处理,不支持 调用private 方法
如有以下的
public class OverloadMethod{ public String age(int age){ return "age int:" + age; } public String age(Integer age){ return "age Integer:" + age; } }测试调用
Class<?>[] parameterTypes1 = { Integer.TYPE }; assertEquals("age int:5", MethodUtil.invokeMethod(new OverloadMethod(), "age", toArray(5), parameterTypes1)); Class<?>[] parameterTypes2 = { Integer.class }; assertEquals("age Integer:5", MethodUtil.invokeMethod(new OverloadMethod(), "age", toArray(Integer.parseInt("5")), parameterTypes2));
T - the generic typeobject - the objectmethodName - the method nameargs - the argsparameterTypes - the parameter typesobject 是null,抛出 NullPointerExceptionmethodName 是null,抛出 NullPointerExceptionmethodName 是blank,抛出 IllegalArgumentExceptionobj没有指定的methodName方法,抛出 ReflectExceptionparameterTypes 是null,系统内部会使用 empty 的class 数组parameterTypes 是empty,表示不需要参数MethodUtils.invokeMethod(Object, String, Object[], Class[]),
MethodUtils.getMatchingAccessibleMethod(Class, String, Class...)public static <T> T invokeStaticMethod(Class<?> klass, String staticMethodName, Object... params)
klass 的指定静态方法 staticMethodName (同时支持 私有静态方法).
- 支持调用对象父类静态方法
- 不可以调用非静态的方法
- 支持调用私有的静态的方法(since 1.11.5 )
- params将会转成包装类型来寻找method
MethodUtils.invokeStaticMethod(Class, String, Object[], Class[])内部调用的是Class.getMethods()来处理, 直接使用的话,不支持 调用 private 方法- 该方法进行容错处理,如果
MethodUtils.invokeStaticMethod(Class, String, Object[], Class[])找不到对应的 公共的静态方法, 将会去尝试找私有额静态方法
如有以下的
public class OverloadStaticMethod{ public static String age(int age){ return "static age int:" + age; } public static String age(Integer age){ return "static age Integer:" + age; } }测试调用MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", 5); MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", Integer.parseInt("5"));结果都是:static age Integer:5 static age Integer:5
如果你要精准调用,请使用 invokeStaticMethod(Class, String, Object[], Class[])
Class<?>[] parameterTypes1 = { Integer.TYPE }; assertEquals("static age int:5", MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", toArray(5), parameterTypes1)); Class<?>[] parameterTypes2 = { Integer.class }; assertEquals( "static age Integer:5", MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", toArray(Integer.parseInt("5")), parameterTypes2));
T - the generic typeklass - the klassstaticMethodName - 静态方法名params - 动态参数klass 是null,抛出 NullPointerExceptionstaticMethodName 是null,抛出 NullPointerExceptionstaticMethodName 是blank,抛出 IllegalArgumentExceptionstaticMethodName 是实例方法,非静态方法,抛出 ReflectExceptionklass没有指定的staticMethodName方法,抛出 ReflectExceptionparameterTypes 是null,系统内部会使用 empty 的class 数组parameterTypes 是empty,表示不需要参数Method.invoke(Object, Object...),
MethodUtils.invokeStaticMethod(Class, String, Object...)public static <T> T invokeStaticMethod(Class<?> klass, String staticMethodName, Object[] args, Class<?>[] parameterTypes)
klass 的指定静态方法 staticMethodName (同时支持 私有静态方法).
- 支持调用对象父类静态方法
- 不可以调用非静态的方法
- 支持调用私有的静态的方法(since 1.11.5 )
MethodUtils.invokeStaticMethod(Class, String, Object[], Class[])内部调用的是Class.getMethods()来处理, 直接使用的话,不支持 调用 private 方法- 该方法进行容错处理,如果
MethodUtils.invokeStaticMethod(Class, String, Object[], Class[])找不到对应的 公共的静态方法, 将会去尝试找私有额静态方法
如有以下的
public class OverloadStaticMethod{ public static String age(int age){ return "static age int:" + age; } public static String age(Integer age){ return "static age Integer:" + age; } }测试调用Class<?>[] parameterTypes1 = { Integer.TYPE }; assertEquals("static age int:5", MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", toArray(5), parameterTypes1)); Class<?>[] parameterTypes2 = { Integer.class }; assertEquals( "static age Integer:5", MethodUtil.invokeStaticMethod(OverloadStaticMethod.class, "age", toArray(Integer.parseInt("5")), parameterTypes2));
T - the generic typeklass - the klassstaticMethodName - 静态方法名args - the argsparameterTypes - the parameter typesklass 是null,抛出 NullPointerExceptionstaticMethodName 是null,抛出 NullPointerExceptionstaticMethodName 是blank,抛出 IllegalArgumentExceptionstaticMethodName 是实例方法,非静态方法,抛出 ReflectExceptionklass没有指定的staticMethodName方法,抛出 ReflectExceptionparameterTypes 是null,系统内部会使用 empty 的class 数组parameterTypes 是empty,表示不需要参数MethodUtils.invokeStaticMethod(Class, String, Object[], Class[])Copyright © 2008-2019 by feilong