這裏使用的是: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修飾的方法