@@ -67,7 +67,7 @@ public class MethodUtils {
6767 private static final Comparator <Method > METHOD_BY_SIGNATURE = Comparator .comparing (Method ::toString );
6868
6969 /**
70- * Returns the aggregate number of inheritance hops between assignable argument class types. Returns -1
70+ * Computes the aggregate number of inheritance hops between assignable argument class types. Returns -1
7171 * if the arguments aren't assignable. Fills a specific purpose for getMatchingMethod and is not generalized.
7272 *
7373 * @param fromClassArray the Class array to calculate the distance from.
@@ -76,7 +76,6 @@ public class MethodUtils {
7676 */
7777 private static int distance (final Class <?>[] fromClassArray , final Class <?>[] toClassArray ) {
7878 int answer = 0 ;
79-
8079 if (!ClassUtils .isAssignable (fromClassArray , toClassArray , true )) {
8180 return -1 ;
8281 }
@@ -87,14 +86,12 @@ private static int distance(final Class<?>[] fromClassArray, final Class<?>[] to
8786 if (aClass == null || aClass .equals (toClass )) {
8887 continue ;
8988 }
90- if (ClassUtils .isAssignable (aClass , toClass , true )
91- && !ClassUtils .isAssignable (aClass , toClass , false )) {
89+ if (ClassUtils .isAssignable (aClass , toClass , true ) && !ClassUtils .isAssignable (aClass , toClass , false )) {
9290 answer ++;
9391 } else {
9492 answer += 2 ;
9593 }
9694 }
97-
9895 return answer ;
9996 }
10097
@@ -189,10 +186,10 @@ private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls, final S
189186 * reflection) by scanning through the superclasses. If no such method
190187 * can be found, return {@code null}.
191188 *
192- * @param cls Class to be checked
193- * @param methodName Method name of the method we wish to call
194- * @param parameterTypes The parameter type signatures
195- * @return the accessible method or {@code null} if not found
189+ * @param cls Class to be checked.
190+ * @param methodName Method name of the method we wish to call.
191+ * @param parameterTypes The parameter type signatures.
192+ * @return the accessible method or {@code null} if not found.
196193 */
197194 private static Method getAccessibleMethodFromSuperclass (final Class <?> cls , final String methodName , final Class <?>... parameterTypes ) {
198195 Class <?> parentClass = cls .getSuperclass ();
@@ -295,15 +292,14 @@ public static <A extends Annotation> A getAnnotation(final Method method, final
295292 * <p>
296293 * This method is used by {@link #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
297294 * </p>
298- *
299295 * <p>
300296 * This method can match primitive parameter by passing in wrapper classes. For example, a {@link Boolean} will match a primitive {@code boolean} parameter.
301297 * </p>
302298 *
303- * @param cls find method in this class
304- * @param methodName find method with this name
305- * @param parameterTypes find method with most compatible parameters
306- * @return The accessible method
299+ * @param cls find method in this class.
300+ * @param methodName find method with this name.
301+ * @param parameterTypes find method with most compatible parameters.
302+ * @return The accessible method or null.
307303 * @throws SecurityException if an underlying accessible object's method denies the request.
308304 * @see SecurityManager#checkPermission
309305 */
@@ -630,10 +626,10 @@ public static Object invokeExactMethod(final Object object, final String methodN
630626 * @throws NullPointerException Thrown if the specified {@code object} is null.
631627 * @throws ExceptionInInitializerError Thrown if the initialization provoked by this method fails.
632628 */
633- public static Object invokeExactMethod (final Object object , final String methodName , Object ... args )
629+ public static Object invokeExactMethod (final Object object , final String methodName , final Object ... args )
634630 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
635- args = ArrayUtils .nullToEmpty (args );
636- return invokeExactMethod (object , methodName , args , ClassUtils .toClass (args ));
631+ final Object [] actuals = ArrayUtils .nullToEmpty (args );
632+ return invokeExactMethod (object , methodName , actuals , ClassUtils .toClass (actuals ));
637633 }
638634
639635 /**
@@ -664,17 +660,14 @@ public static Object invokeExactMethod(final Object object, final String methodN
664660 * @throws NullPointerException Thrown if the specified {@code object} is null.
665661 * @throws ExceptionInInitializerError Thrown if the initialization provoked by this method fails.
666662 */
667- public static Object invokeExactMethod (final Object object , final String methodName , Object [] args , Class <?>[] parameterTypes )
663+ public static Object invokeExactMethod (final Object object , final String methodName , final Object [] args , final Class <?>[] parameterTypes )
668664 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
669- Objects .requireNonNull (object , "object" );
670- args = ArrayUtils .nullToEmpty (args );
671- parameterTypes = ArrayUtils .nullToEmpty (parameterTypes );
672- final Class <?> cls = object .getClass ();
673- final Method method = getAccessibleMethod (cls , methodName , parameterTypes );
665+ final Class <?> cls = Objects .requireNonNull (object , "object" ).getClass ();
666+ final Method method = getAccessibleMethod (cls , methodName , ArrayUtils .nullToEmpty (parameterTypes ));
674667 if (method == null ) {
675668 throw new NoSuchMethodException ("No such accessible method: " + methodName + "() on object: " + cls .getName ());
676669 }
677- return method .invoke (object , args );
670+ return method .invoke (object , ArrayUtils . nullToEmpty ( args ) );
678671 }
679672
680673 /**
@@ -692,10 +685,10 @@ public static Object invokeExactMethod(final Object object, final String methodN
692685 * @throws InvocationTargetException wraps an exception thrown by the method invoked
693686 * @throws IllegalAccessException if the requested method is not accessible via reflection
694687 */
695- public static Object invokeExactStaticMethod (final Class <?> cls , final String methodName , Object ... args )
688+ public static Object invokeExactStaticMethod (final Class <?> cls , final String methodName , final Object ... args )
696689 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
697- args = ArrayUtils .nullToEmpty (args );
698- return invokeExactStaticMethod (cls , methodName , args , ClassUtils .toClass (args ));
690+ final Object [] actuals = ArrayUtils .nullToEmpty (args );
691+ return invokeExactStaticMethod (cls , methodName , actuals , ClassUtils .toClass (actuals ));
699692 }
700693
701694 /**
@@ -714,15 +707,13 @@ public static Object invokeExactStaticMethod(final Class<?> cls, final String me
714707 * @throws InvocationTargetException wraps an exception thrown by the method invoked
715708 * @throws IllegalAccessException if the requested method is not accessible via reflection
716709 */
717- public static Object invokeExactStaticMethod (final Class <?> cls , final String methodName , Object [] args , Class <?>[] parameterTypes )
710+ public static Object invokeExactStaticMethod (final Class <?> cls , final String methodName , final Object [] args , final Class <?>[] parameterTypes )
718711 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
719- args = ArrayUtils .nullToEmpty (args );
720- parameterTypes = ArrayUtils .nullToEmpty (parameterTypes );
721- final Method method = getAccessibleMethod (cls , methodName , parameterTypes );
712+ final Method method = getAccessibleMethod (cls , methodName , ArrayUtils .nullToEmpty (parameterTypes ));
722713 if (method == null ) {
723714 throw new NoSuchMethodException ("No such accessible method: " + methodName + "() on class: " + cls .getName ());
724715 }
725- return method .invoke (null , args );
716+ return method .invoke (null , ArrayUtils . nullToEmpty ( args ) );
726717 }
727718
728719 /**
@@ -775,10 +766,10 @@ public static Object invokeMethod(final Object object, final boolean forceAccess
775766 * @see SecurityManager#checkPermission
776767 * @since 3.5
777768 */
778- public static Object invokeMethod (final Object object , final boolean forceAccess , final String methodName , Object ... args )
769+ public static Object invokeMethod (final Object object , final boolean forceAccess , final String methodName , final Object ... args )
779770 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
780- args = ArrayUtils .nullToEmpty (args );
781- return invokeMethod (object , forceAccess , methodName , args , ClassUtils .toClass (args ));
771+ final Object [] actuals = ArrayUtils .nullToEmpty (args );
772+ return invokeMethod (object , forceAccess , methodName , actuals , ClassUtils .toClass (actuals ));
782773 }
783774
784775 /**
@@ -804,11 +795,10 @@ public static Object invokeMethod(final Object object, final boolean forceAccess
804795 * @see SecurityManager#checkPermission
805796 * @since 3.5
806797 */
807- public static Object invokeMethod (final Object object , final boolean forceAccess , final String methodName , Object [] args , Class <?>[] parameterTypes )
798+ public static Object invokeMethod (final Object object , final boolean forceAccess , final String methodName , final Object [] args , Class <?>[] parameterTypes )
808799 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
809800 Objects .requireNonNull (object , "object" );
810801 parameterTypes = ArrayUtils .nullToEmpty (parameterTypes );
811- args = ArrayUtils .nullToEmpty (args );
812802 final String messagePrefix ;
813803 final Method method ;
814804 final Class <? extends Object > cls = object .getClass ();
@@ -825,8 +815,7 @@ public static Object invokeMethod(final Object object, final boolean forceAccess
825815 if (method == null ) {
826816 throw new NoSuchMethodException (messagePrefix + methodName + "() on object: " + cls .getName ());
827817 }
828- args = toVarArgs (method , args );
829- return method .invoke (object , args );
818+ return method .invoke (object , toVarArgs (method , ArrayUtils .nullToEmpty (args )));
830819 }
831820
832821 /**
@@ -882,10 +871,10 @@ public static Object invokeMethod(final Object object, final String methodName)
882871 * @throws SecurityException if an underlying accessible object's method denies the request.
883872 * @see SecurityManager#checkPermission
884873 */
885- public static Object invokeMethod (final Object object , final String methodName , Object ... args )
874+ public static Object invokeMethod (final Object object , final String methodName , final Object ... args )
886875 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
887- args = ArrayUtils .nullToEmpty (args );
888- return invokeMethod (object , methodName , args , ClassUtils .toClass (args ));
876+ final Object [] actuals = ArrayUtils .nullToEmpty (args );
877+ return invokeMethod (object , methodName , actuals , ClassUtils .toClass (actuals ));
889878 }
890879
891880 /**
@@ -911,10 +900,8 @@ public static Object invokeMethod(final Object object, final String methodName,
911900 * @throws SecurityException if an underlying accessible object's method denies the request.
912901 * @see SecurityManager#checkPermission
913902 */
914- public static Object invokeMethod (final Object object , final String methodName ,
915- final Object [] args , final Class <?>[] parameterTypes )
916- throws NoSuchMethodException , IllegalAccessException ,
917- InvocationTargetException {
903+ public static Object invokeMethod (final Object object , final String methodName , final Object [] args , final Class <?>[] parameterTypes )
904+ throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
918905 return invokeMethod (object , false , methodName , args , parameterTypes );
919906 }
920907
@@ -944,11 +931,10 @@ public static Object invokeMethod(final Object object, final String methodName,
944931 * @throws SecurityException if an underlying accessible object's method denies the request.
945932 * @see SecurityManager#checkPermission
946933 */
947- public static Object invokeStaticMethod (final Class <?> cls , final String methodName ,
948- Object ... args ) throws NoSuchMethodException ,
949- IllegalAccessException , InvocationTargetException {
950- args = ArrayUtils .nullToEmpty (args );
951- return invokeStaticMethod (cls , methodName , args , ClassUtils .toClass (args ));
934+ public static Object invokeStaticMethod (final Class <?> cls , final String methodName , final Object ... args )
935+ throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
936+ final Object [] actuals = ArrayUtils .nullToEmpty (args );
937+ return invokeStaticMethod (cls , methodName , actuals , ClassUtils .toClass (actuals ));
952938 }
953939
954940 /**
@@ -974,16 +960,13 @@ public static Object invokeStaticMethod(final Class<?> cls, final String methodN
974960 * @throws SecurityException if an underlying accessible object's method denies the request.
975961 * @see SecurityManager#checkPermission
976962 */
977- public static Object invokeStaticMethod (final Class <?> cls , final String methodName , Object [] args , Class <?>[] parameterTypes )
963+ public static Object invokeStaticMethod (final Class <?> cls , final String methodName , final Object [] args , final Class <?>[] parameterTypes )
978964 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
979- args = ArrayUtils .nullToEmpty (args );
980- parameterTypes = ArrayUtils .nullToEmpty (parameterTypes );
981- final Method method = getMatchingAccessibleMethod (cls , methodName , parameterTypes );
965+ final Method method = getMatchingAccessibleMethod (cls , methodName , ArrayUtils .nullToEmpty (parameterTypes ));
982966 if (method == null ) {
983967 throw new NoSuchMethodException ("No such accessible method: " + methodName + "() on class: " + cls .getName ());
984968 }
985- args = toVarArgs (method , args );
986- return method .invoke (null , args );
969+ return method .invoke (null , toVarArgs (method , ArrayUtils .nullToEmpty (args )));
987970 }
988971
989972 private static Object [] toVarArgs (final Method method , Object [] args ) {
0 commit comments