【方法工具类】MethodUtils

这里使用的是:3.9 的版本,还是比较新的

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
     <version>3.9</version>
</dependency>

 

 

Object invokeMethod(final Object object, final String methodName) 
调用无参pubclic修饰的方法,第一个参数是对象,第二个参数是方法名

Object invokeMethod(final Object object, final String methodName, Object... args) 
调用有参方法pubclic修饰的方法,第一个参数是对象,第二个参数是方法名,第三个参数是参数

Object invokeMethod(final Object object, final String methodName, 
                    final Object[] args, final Class<?>[] parameterTypes) 

调用有参方法pubclic修饰的方法,第一个参数是对象,第二个参数是方法名,第三个参数是参数,第四个参数是方法形参类型

TestMethod testMethod = new TestMethod();
Object methodOne = MethodUtils.invokeMethod(testMethod, "methodOne",
ArrayUtils.toArray("123"), new Class[]{String.class});
System.out.println(methodOne); // 123one

 

Object invokeMethod(final Object object, final boolean forceAccess, final String methodName)
调用无参的方法,第一个参数是对象,第二个参数是任何修饰的方法都可以访问(默认、protected、private、public),第三个是方法名

Object invokeMethod(final Object object, final boolean forceAccess, 
                    final String methodName, Object... args)

调用有参的方法,第一个参数是对象,第二个参数是任何修饰的方法都可以访问(默认、protected、private、public),第三个是方法名,第四个参数是参数

Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, 
                    Object[] args, Class<?>[] parameterTypes)

调用有参的方法,第一个参数是对象,第二个参数是任何修饰的方法都可以访问(默认、protected、private、public),第三个是方法名,第四个参数是参数,第五个参数是方法形参类型

TestMethod testMethod = new TestMethod();
Object methodOne = MethodUtils.invokeMethod(testMethod, true, "methodOne",
ArrayUtils.toArray("123"), new Class[]{String.class});
System.out.println(methodOne); // 123one


Object invokeExactMethod(final Object object, final String methodName)
调用无参pubclic修饰的方法,第一个参数是对象,第二个参数是方法名

Object invokeExactMethod(final Object object, final String methodName, Object... args)
调用有参方法pubclic修饰的方法,第一个参数是对象,第二个参数是方法名,第三个参数是参数

Object invokeExactMethod(final Object object, final String methodName, 
                         Object[] args, Class<?>[] parameterTypes)

调用有参方法pubclic修饰的方法,第一个参数是对象,第二个参数是方法名,第三个参数是参数,第四个参数是方法形参类型


invokeMethod 和 invokeExactMethod 的区别?
从名字上来看,明显是后者准确性更高,或者说要求更严格。invokeExact方法在调用时要求严格的类型匹配,比如:方法形参是int,parameterTypes里面传的是Integer.class,此时就会报错

 

Object invokeStaticMethod(final Class<?> cls, final String methodName, Object... args)

Object invokeStaticMethod(final Class<?> cls, final String methodName, Object[] args, Class<?>[] parameterTypes)

调用有参方法pubclic修饰的静态方法

Object methodOne = MethodUtils.invokeStaticMethod(TestMethod.class, "methodOne",
ArrayUtils.toArray("123", 1), new Class[]{String.class, int.class});
System.out.println(methodOne); // 123one1

 

Object invokeExactStaticMethod(final Class<?> cls, final String methodName, Object... args) 

Object invokeExactStaticMethod(final Class<?> cls, final String methodName, Object[] args, Class<?>[] parameterTypes)

调用有参方法pubclic修饰的静态方法,比 invokeStaticMethod 对参数类型的要求更严格

 

Method getAccessibleMethod(Method method) 获取可访问的方法
Method getAccessibleMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes)

Method method = MethodUtils.getAccessibleMethod(TestMethod.class, "methodOne", new Class[]{String.class, Integer.class});
System.out.println(method); // public static java.lang.String com.hbsc.common.TestMethod.methodOne(java.lang.String,java.lang.Integer)

 


Method getMatchingAccessibleMethod(final Class<?> cls, 
                                   final String methodName, 
                                   final Class<?>... parameterTypes)

查找与给定名称匹配并具有兼容参数的可访问方法。

 

Method getMatchingMethod(final Class<?> cls, 
                         final String methodName, 
                         final Class<?>... parameterTypes)

检索方法是否可访问

 

Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior)
获取重新覆盖的方法类集

Method method = MethodUtils.getAccessibleMethod(TestMethod.class, "methodOne",
                                                new Class[]{String.class, Integer.class});
Set<Method> overrideHierarchy = MethodUtils.getOverrideHierarchy(method,
                                                                 ClassUtils.Interfaces.INCLUDE);


Method[] getMethodsWithAnnotation(final Class<?> cls, 
                                  final Class<? extends Annotation> annotationCls)
Method[] getMethodsWithAnnotation(final Class<?> cls, 
                                  final Class<? extends Annotation> annotationCls,
                                  final boolean searchSupers, final boolean ignoreAccess)

获取方法上包含指定注解的方法,返回一个数组,第一个参数是类的class,第二个参数是注解的class,第三个数表示是否查询父类的方法,第四个参数是否考虑非public修饰的方法

 

List<Method> getMethodsListWithAnnotation(final Class<?> cls, 
                                          final Class<? extends Annotation> annotationCls)
List<Method> getMethodsListWithAnnotation(final Class<?> cls,
                                          final Class<? extends Annotation> annotationCls,
                                          final boolean searchSupers, final boolean ignoreAccess)

获取方法上包含指定注解的方法,返回一个集合,第一个参数是类的class,第二个参数是注解的class,第三个数表示是否查询父类的方法,第四个参数是否考虑非public修饰的方法

 

<A extends Annotation> A getAnnotation(final Method method, final Class<A> annotationCls,
                                       final boolean searchSupers, final boolean ignoreAccess)

获取方法上的注解,返回注解实例,第一个参数是方法,第二个参数是注解的class,第三个数表示是否查询父类的方法,第四个参数是否考虑非public修饰的方法


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章