Coverage Report - org.apache.commons.lang3.reflect.MethodUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodUtils
94%
174/185
84%
88/104
3,654
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3.reflect;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.Array;
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.lang.reflect.Method;
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.lang.reflect.Type;
 25  
 import java.lang.reflect.TypeVariable;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Arrays;
 28  
 import java.util.Iterator;
 29  
 import java.util.LinkedHashSet;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 import java.util.Set;
 33  
 
 34  
 import org.apache.commons.lang3.ArrayUtils;
 35  
 import org.apache.commons.lang3.ClassUtils;
 36  
 import org.apache.commons.lang3.ClassUtils.Interfaces;
 37  
 import org.apache.commons.lang3.Validate;
 38  
 
 39  
 /**
 40  
  * <p>Utility reflection methods focused on {@link Method}s, originally from Commons BeanUtils.
 41  
  * Differences from the BeanUtils version may be noted, especially where similar functionality
 42  
  * already existed within Lang.
 43  
  * </p>
 44  
  *
 45  
  * <h3>Known Limitations</h3>
 46  
  * <h4>Accessing Public Methods In A Default Access Superclass</h4>
 47  
  * <p>There is an issue when invoking {@code public} methods contained in a default access superclass on JREs prior to 1.4.
 48  
  * Reflection locates these methods fine and correctly assigns them as {@code public}.
 49  
  * However, an {@link IllegalAccessException} is thrown if the method is invoked.</p>
 50  
  *
 51  
  * <p>{@link MethodUtils} contains a workaround for this situation. 
 52  
  * It will attempt to call {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} on this method.
 53  
  * If this call succeeds, then the method can be invoked as normal.
 54  
  * This call will only succeed when the application has sufficient security privileges. 
 55  
  * If this call fails then the method may fail.</p>
 56  
  *
 57  
  * @since 2.5
 58  
  */
 59  
 public class MethodUtils {
 60  
 
 61  
     /**
 62  
      * <p>{@link MethodUtils} instances should NOT be constructed in standard programming.
 63  
      * Instead, the class should be used as
 64  
      * {@code MethodUtils.getAccessibleMethod(method)}.</p>
 65  
      *
 66  
      * <p>This constructor is {@code public} to permit tools that require a JavaBean
 67  
      * instance to operate.</p>
 68  
      */
 69  
     public MethodUtils() {
 70  4
         super();
 71  4
     }
 72  
 
 73  
     /**
 74  
      * <p>Invokes a named method without parameters.</p>
 75  
      *
 76  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 77  
      *
 78  
      * <p>This is a convenient wrapper for
 79  
      * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
 80  
      * </p>
 81  
      *
 82  
      * @param object invoke method on this object
 83  
      * @param methodName get method with this name
 84  
      * @return The value returned by the invoked method
 85  
      *
 86  
      * @throws NoSuchMethodException if there is no such accessible method
 87  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 88  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 89  
      *  
 90  
      *  @since 3.4
 91  
      */
 92  
     public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException,
 93  
             IllegalAccessException, InvocationTargetException {
 94  6
         return invokeMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
 95  
     }
 96  
     
 97  
     /**
 98  
      * <p>Invokes a named method without parameters.</p>
 99  
      *
 100  
      * <p>This is a convenient wrapper for
 101  
      * {@link #invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)}.
 102  
      * </p>
 103  
      *
 104  
      * @param object invoke method on this object
 105  
      * @param forceAccess force access to invoke method even if it's not accessible
 106  
      * @param methodName get method with this name
 107  
      * @return The value returned by the invoked method
 108  
      *
 109  
      * @throws NoSuchMethodException if there is no such accessible method
 110  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 111  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 112  
      *  
 113  
      * @since 3.5
 114  
      */
 115  
     public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName) 
 116  
                     throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
 117  2
         return invokeMethod(object, forceAccess, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
 118  
     }
 119  
 
 120  
     /**
 121  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 122  
      *
 123  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 124  
      *
 125  
      * <p>This method supports calls to methods taking primitive parameters 
 126  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 127  
      * would match a {@code boolean} primitive.</p>
 128  
      *
 129  
      * <p>This is a convenient wrapper for
 130  
      * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
 131  
      * </p>
 132  
      *
 133  
      * @param object invoke method on this object
 134  
      * @param methodName get method with this name
 135  
      * @param args use these arguments - treat null as empty array
 136  
      * @return The value returned by the invoked method
 137  
      *
 138  
      * @throws NoSuchMethodException if there is no such accessible method
 139  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 140  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 141  
      */
 142  
     public static Object invokeMethod(final Object object, final String methodName,
 143  
             Object... args) throws NoSuchMethodException,
 144  
             IllegalAccessException, InvocationTargetException {
 145  50
         args = ArrayUtils.nullToEmpty(args);
 146  50
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 147  50
         return invokeMethod(object, methodName, args, parameterTypes);
 148  
     }
 149  
     
 150  
     /**
 151  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 152  
      *
 153  
      * <p>This method supports calls to methods taking primitive parameters 
 154  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 155  
      * would match a {@code boolean} primitive.</p>
 156  
      *
 157  
      * <p>This is a convenient wrapper for
 158  
      * {@link #invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)}.
 159  
      * </p>
 160  
      *
 161  
      * @param object invoke method on this object
 162  
      * @param methodName get method with this name
 163  
      * @param args use these arguments - treat null as empty array
 164  
      * @return The value returned by the invoked method
 165  
      *
 166  
      * @throws NoSuchMethodException if there is no such accessible method
 167  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 168  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 169  
      * 
 170  
      * @since 3.5
 171  
      */
 172  
     public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName,
 173  
             Object... args) throws NoSuchMethodException,
 174  
             IllegalAccessException, InvocationTargetException {
 175  8
         args = ArrayUtils.nullToEmpty(args);
 176  8
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 177  8
         return invokeMethod(object, forceAccess, methodName, args, parameterTypes);
 178  
     }
 179  
 
 180  
     /**
 181  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 182  
      *
 183  
      * <p>This method supports calls to methods taking primitive parameters 
 184  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 185  
      * would match a {@code boolean} primitive.</p>
 186  
      *
 187  
      * @param object invoke method on this object
 188  
      * @param forceAccess force access to invoke method even if it's not accessible
 189  
      * @param methodName get method with this name
 190  
      * @param args use these arguments - treat null as empty array
 191  
      * @param parameterTypes match these parameters - treat null as empty array
 192  
      * @return The value returned by the invoked method
 193  
      *
 194  
      * @throws NoSuchMethodException if there is no such accessible method
 195  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 196  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 197  
      */
 198  
     public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName,
 199  
             Object[] args, Class<?>[] parameterTypes)
 200  
             throws NoSuchMethodException, IllegalAccessException,
 201  
             InvocationTargetException {
 202  68
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 203  68
         args = ArrayUtils.nullToEmpty(args);
 204  
         
 205  
         final String messagePrefix;
 206  68
         Method method = null;
 207  68
         boolean isOriginallyAccessible = false;
 208  68
         Object result = null;
 209  
         
 210  
         try {
 211  68
             if (forceAccess) {
 212  10
                     messagePrefix = "No such method: ";
 213  10
                     method = getMatchingMethod(object.getClass(),
 214  
                         methodName, parameterTypes);
 215  10
                     if (method != null) {
 216  10
                         isOriginallyAccessible = method.isAccessible();
 217  10
                         if (!isOriginallyAccessible) {
 218  10
                             method.setAccessible(true);
 219  
                         }
 220  
                     }
 221  
             }  else {
 222  58
                     messagePrefix = "No such accessible method: ";
 223  58
                     method = getMatchingAccessibleMethod(object.getClass(),
 224  
                         methodName, parameterTypes);
 225  
             }
 226  
             
 227  68
             if (method == null) {
 228  4
                 throw new NoSuchMethodException(messagePrefix
 229  
                         + methodName + "() on object: "
 230  
                         + object.getClass().getName());
 231  
             }
 232  64
             args = toVarArgs(method, args);
 233  
             
 234  64
             result = method.invoke(object, args);
 235  
         }
 236  
         finally {
 237  68
             if (method != null && forceAccess && method.isAccessible() != isOriginallyAccessible) {
 238  10
                 method.setAccessible(isOriginallyAccessible);
 239  
             }
 240  
         }
 241  
         
 242  62
         return result;
 243  
     }
 244  
     
 245  
     /**
 246  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 247  
      *
 248  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 249  
      *
 250  
      * <p>This method supports calls to methods taking primitive parameters 
 251  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 252  
      * would match a {@code boolean} primitive.</p>
 253  
      *
 254  
      * @param object invoke method on this object
 255  
      * @param methodName get method with this name
 256  
      * @param args use these arguments - treat null as empty array
 257  
      * @param parameterTypes match these parameters - treat null as empty array
 258  
      * @return The value returned by the invoked method
 259  
      *
 260  
      * @throws NoSuchMethodException if there is no such accessible method
 261  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 262  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 263  
      */
 264  
     public static Object invokeMethod(final Object object, final String methodName, 
 265  
             Object[] args, Class<?>[] parameterTypes)
 266  
             throws NoSuchMethodException, IllegalAccessException,
 267  
             InvocationTargetException {
 268  58
             return invokeMethod(object, false, methodName, args, parameterTypes);
 269  
     }
 270  
 
 271  
     /**
 272  
      * <p>Invokes a method whose parameter types match exactly the object
 273  
      * types.</p>
 274  
      *
 275  
      * <p>This uses reflection to invoke the method obtained from a call to
 276  
      * {@link #getAccessibleMethod}(Class,String,Class[])}.</p>
 277  
      *
 278  
      * @param object invoke method on this object
 279  
      * @param methodName get method with this name
 280  
      * @return The value returned by the invoked method
 281  
      *
 282  
      * @throws NoSuchMethodException if there is no such accessible method
 283  
      * @throws InvocationTargetException wraps an exception thrown by the
 284  
      *  method invoked
 285  
      * @throws IllegalAccessException if the requested method is not accessible
 286  
      *  via reflection
 287  
      *  
 288  
      *  @since 3.4
 289  
      */
 290  
     public static Object invokeExactMethod(final Object object, final String methodName) throws NoSuchMethodException,
 291  
             IllegalAccessException, InvocationTargetException {
 292  2
         return invokeExactMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
 293  
     }
 294  
 
 295  
     /**
 296  
      * <p>Invokes a method with no parameters.</p>
 297  
      *
 298  
      * <p>This uses reflection to invoke the method obtained from a call to
 299  
      * {@link #getAccessibleMethod}(Class,String,Class[])}.</p>
 300  
      *
 301  
      * @param object invoke method on this object
 302  
      * @param methodName get method with this name
 303  
      * @param args use these arguments - treat null as empty array
 304  
      * @return The value returned by the invoked method
 305  
      *
 306  
      * @throws NoSuchMethodException if there is no such accessible method
 307  
      * @throws InvocationTargetException wraps an exception thrown by the
 308  
      *  method invoked
 309  
      * @throws IllegalAccessException if the requested method is not accessible
 310  
      *  via reflection
 311  
      */
 312  
     public static Object invokeExactMethod(final Object object, final String methodName,
 313  
             Object... args) throws NoSuchMethodException,
 314  
             IllegalAccessException, InvocationTargetException {
 315  16
         args = ArrayUtils.nullToEmpty(args);
 316  16
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 317  16
         return invokeExactMethod(object, methodName, args, parameterTypes);
 318  
     }
 319  
 
 320  
     /**
 321  
      * <p>Invokes a method whose parameter types match exactly the parameter
 322  
      * types given.</p>
 323  
      *
 324  
      * <p>This uses reflection to invoke the method obtained from a call to
 325  
      * {@link #getAccessibleMethod(Class,String,Class[])}.</p>
 326  
      *
 327  
      * @param object invoke method on this object
 328  
      * @param methodName get method with this name
 329  
      * @param args use these arguments - treat null as empty array
 330  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 331  
      * @return The value returned by the invoked method
 332  
      *
 333  
      * @throws NoSuchMethodException if there is no such accessible method
 334  
      * @throws InvocationTargetException wraps an exception thrown by the
 335  
      *  method invoked
 336  
      * @throws IllegalAccessException if the requested method is not accessible
 337  
      *  via reflection
 338  
      */
 339  
     public static Object invokeExactMethod(final Object object, final String methodName,
 340  
             Object[] args, Class<?>[] parameterTypes)
 341  
             throws NoSuchMethodException, IllegalAccessException,
 342  
             InvocationTargetException {
 343  22
         args = ArrayUtils.nullToEmpty(args);
 344  22
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 345  22
         final Method method = getAccessibleMethod(object.getClass(), methodName,
 346  
                 parameterTypes);
 347  22
         if (method == null) {
 348  6
             throw new NoSuchMethodException("No such accessible method: "
 349  
                     + methodName + "() on object: "
 350  
                     + object.getClass().getName());
 351  
         }
 352  16
         return method.invoke(object, args);
 353  
     }
 354  
 
 355  
     /**
 356  
      * <p>Invokes a {@code static} method whose parameter types match exactly the parameter
 357  
      * types given.</p>
 358  
      *
 359  
      * <p>This uses reflection to invoke the method obtained from a call to
 360  
      * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 361  
      *
 362  
      * @param cls invoke static method on this class
 363  
      * @param methodName get method with this name
 364  
      * @param args use these arguments - treat {@code null} as empty array
 365  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 366  
      * @return The value returned by the invoked method
 367  
      *
 368  
      * @throws NoSuchMethodException if there is no such accessible method
 369  
      * @throws InvocationTargetException wraps an exception thrown by the
 370  
      *  method invoked
 371  
      * @throws IllegalAccessException if the requested method is not accessible
 372  
      *  via reflection
 373  
      */
 374  
     public static Object invokeExactStaticMethod(final Class<?> cls, final String methodName,
 375  
             Object[] args, Class<?>[] parameterTypes)
 376  
             throws NoSuchMethodException, IllegalAccessException,
 377  
             InvocationTargetException {
 378  20
         args = ArrayUtils.nullToEmpty(args);
 379  20
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 380  20
         final Method method = getAccessibleMethod(cls, methodName, parameterTypes);
 381  20
         if (method == null) {
 382  6
             throw new NoSuchMethodException("No such accessible method: "
 383  
                     + methodName + "() on class: " + cls.getName());
 384  
         }
 385  14
         return method.invoke(null, args);
 386  
     }
 387  
 
 388  
     /**
 389  
      * <p>Invokes a named {@code static} method whose parameter type matches the object type.</p>
 390  
      *
 391  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 392  
      *
 393  
      * <p>This method supports calls to methods taking primitive parameters 
 394  
      * via passing in wrapping classes. So, for example, a {@code Boolean} class
 395  
      * would match a {@code boolean} primitive.</p>
 396  
      *
 397  
      * <p>This is a convenient wrapper for
 398  
      * {@link #invokeStaticMethod(Class, String, Object[], Class[])}.
 399  
      * </p>
 400  
      *
 401  
      * @param cls invoke static method on this class
 402  
      * @param methodName get method with this name
 403  
      * @param args use these arguments - treat {@code null} as empty array
 404  
      * @return The value returned by the invoked method
 405  
      *
 406  
      * @throws NoSuchMethodException if there is no such accessible method
 407  
      * @throws InvocationTargetException wraps an exception thrown by the
 408  
      *  method invoked
 409  
      * @throws IllegalAccessException if the requested method is not accessible
 410  
      *  via reflection
 411  
      */
 412  
     public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
 413  
             Object... args) throws NoSuchMethodException,
 414  
             IllegalAccessException, InvocationTargetException {
 415  74
         args = ArrayUtils.nullToEmpty(args);
 416  74
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 417  74
         return invokeStaticMethod(cls, methodName, args, parameterTypes);
 418  
     }
 419  
 
 420  
     /**
 421  
      * <p>Invokes a named {@code static} method whose parameter type matches the object type.</p>
 422  
      *
 423  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 424  
      *
 425  
      * <p>This method supports calls to methods taking primitive parameters 
 426  
      * via passing in wrapping classes. So, for example, a {@code Boolean} class
 427  
      * would match a {@code boolean} primitive.</p>
 428  
      *
 429  
      *
 430  
      * @param cls invoke static method on this class
 431  
      * @param methodName get method with this name
 432  
      * @param args use these arguments - treat {@code null} as empty array
 433  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 434  
      * @return The value returned by the invoked method
 435  
      *
 436  
      * @throws NoSuchMethodException if there is no such accessible method
 437  
      * @throws InvocationTargetException wraps an exception thrown by the
 438  
      *  method invoked
 439  
      * @throws IllegalAccessException if the requested method is not accessible
 440  
      *  via reflection
 441  
      */
 442  
     public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
 443  
             Object[] args, Class<?>[] parameterTypes)
 444  
             throws NoSuchMethodException, IllegalAccessException,
 445  
             InvocationTargetException {
 446  76
         args = ArrayUtils.nullToEmpty(args);
 447  76
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 448  76
         final Method method = getMatchingAccessibleMethod(cls, methodName,
 449  
                 parameterTypes);
 450  76
         if (method == null) {
 451  2
             throw new NoSuchMethodException("No such accessible method: "
 452  
                     + methodName + "() on class: " + cls.getName());
 453  
         }
 454  74
         args = toVarArgs(method, args);
 455  74
         return method.invoke(null, args);
 456  
     }
 457  
 
 458  
     private static Object[] toVarArgs(Method method, Object[] args) {
 459  138
         if (method.isVarArgs()) {
 460  70
             Class<?>[] methodParameterTypes = method.getParameterTypes();
 461  70
             args = getVarArgs(args, methodParameterTypes);
 462  
         }
 463  138
         return args;
 464  
     }
 465  
 
 466  
     /**
 467  
      * <p>Given an arguments array passed to a varargs method, return an array of arguments in the canonical form,
 468  
      * i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type.
 469  
      * </p>
 470  
      *
 471  
      * @param args the array of arguments passed to the varags method
 472  
      * @param methodParameterTypes the declared array of method parameter types
 473  
      * @return an array of the variadic arguments passed to the method
 474  
      * @since 3.5
 475  
      */
 476  
     static Object[] getVarArgs(Object[] args, Class<?>[] methodParameterTypes) {
 477  76
         if (args.length == methodParameterTypes.length
 478  
                 && args[args.length - 1].getClass().equals(methodParameterTypes[methodParameterTypes.length - 1])) {
 479  
             // The args array is already in the canonical form for the method.
 480  0
             return args;
 481  
         }
 482  
 
 483  
         // Construct a new array matching the method's declared parameter types.
 484  76
         Object[] newArgs = new Object[methodParameterTypes.length];
 485  
 
 486  
         // Copy the normal (non-varargs) parameters
 487  76
         System.arraycopy(args, 0, newArgs, 0, methodParameterTypes.length - 1);
 488  
 
 489  
         // Construct a new array for the variadic parameters
 490  76
         Class<?> varArgComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType();
 491  76
         int varArgLength = args.length - methodParameterTypes.length + 1;
 492  
 
 493  76
         Object varArgsArray = Array.newInstance(ClassUtils.primitiveToWrapper(varArgComponentType), varArgLength);
 494  
         // Copy the variadic arguments into the varargs array.
 495  76
         System.arraycopy(args, methodParameterTypes.length - 1, varArgsArray, 0, varArgLength);
 496  
 
 497  76
         if(varArgComponentType.isPrimitive()) {
 498  
             // unbox from wrapper type to primitive type
 499  4
             varArgsArray = ArrayUtils.toPrimitive(varArgsArray);
 500  
         }
 501  
 
 502  
         // Store the varargs array in the last position of the array to return
 503  76
         newArgs[methodParameterTypes.length - 1] = varArgsArray;
 504  
 
 505  
         // Return the canonical varargs array.
 506  76
         return newArgs;
 507  
     }
 508  
 
 509  
     /**
 510  
      * <p>Invokes a {@code static} method whose parameter types match exactly the object
 511  
      * types.</p>
 512  
      *
 513  
      * <p>This uses reflection to invoke the method obtained from a call to
 514  
      * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 515  
      *
 516  
      * @param cls invoke static method on this class
 517  
      * @param methodName get method with this name
 518  
      * @param args use these arguments - treat {@code null} as empty array
 519  
      * @return The value returned by the invoked method
 520  
      *
 521  
      * @throws NoSuchMethodException if there is no such accessible method
 522  
      * @throws InvocationTargetException wraps an exception thrown by the
 523  
      *  method invoked
 524  
      * @throws IllegalAccessException if the requested method is not accessible
 525  
      *  via reflection
 526  
      */
 527  
     public static Object invokeExactStaticMethod(final Class<?> cls, final String methodName,
 528  
             Object... args) throws NoSuchMethodException,
 529  
             IllegalAccessException, InvocationTargetException {
 530  16
         args = ArrayUtils.nullToEmpty(args);
 531  16
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 532  16
         return invokeExactStaticMethod(cls, methodName, args, parameterTypes);
 533  
     }
 534  
 
 535  
     /**
 536  
      * <p>Returns an accessible method (that is, one that can be invoked via
 537  
      * reflection) with given name and parameters. If no such method
 538  
      * can be found, return {@code null}.
 539  
      * This is just a convenience wrapper for
 540  
      * {@link #getAccessibleMethod(Method)}.</p>
 541  
      *
 542  
      * @param cls get method from this class
 543  
      * @param methodName get method with this name
 544  
      * @param parameterTypes with these parameters types
 545  
      * @return The accessible method
 546  
      */
 547  
     public static Method getAccessibleMethod(final Class<?> cls, final String methodName,
 548  
             final Class<?>... parameterTypes) {
 549  
         try {
 550  60
             return getAccessibleMethod(cls.getMethod(methodName,
 551  
                     parameterTypes));
 552  16
         } catch (final NoSuchMethodException e) {
 553  16
             return null;
 554  
         }
 555  
     }
 556  
 
 557  
     /**
 558  
      * <p>Returns an accessible method (that is, one that can be invoked via
 559  
      * reflection) that implements the specified Method. If no such method
 560  
      * can be found, return {@code null}.</p>
 561  
      *
 562  
      * @param method The method that we wish to call
 563  
      * @return The accessible method
 564  
      */
 565  
     public static Method getAccessibleMethod(Method method) {
 566  362
         if (!MemberUtils.isAccessible(method)) {
 567  2
             return null;
 568  
         }
 569  
         // If the declaring class is public, we are done
 570  360
         final Class<?> cls = method.getDeclaringClass();
 571  360
         if (Modifier.isPublic(cls.getModifiers())) {
 572  350
             return method;
 573  
         }
 574  10
         final String methodName = method.getName();
 575  10
         final Class<?>[] parameterTypes = method.getParameterTypes();
 576  
 
 577  
         // Check the implemented interfaces and subinterfaces
 578  10
         method = getAccessibleMethodFromInterfaceNest(cls, methodName,
 579  
                 parameterTypes);
 580  
 
 581  
         // Check the superclass chain
 582  10
         if (method == null) {
 583  2
             method = getAccessibleMethodFromSuperclass(cls, methodName,
 584  
                     parameterTypes);
 585  
         }
 586  10
         return method;
 587  
     }
 588  
 
 589  
     /**
 590  
      * <p>Returns an accessible method (that is, one that can be invoked via
 591  
      * reflection) by scanning through the superclasses. If no such method
 592  
      * can be found, return {@code null}.</p>
 593  
      *
 594  
      * @param cls Class to be checked
 595  
      * @param methodName Method name of the method we wish to call
 596  
      * @param parameterTypes The parameter type signatures
 597  
      * @return the accessible method or {@code null} if not found
 598  
      */
 599  
     private static Method getAccessibleMethodFromSuperclass(final Class<?> cls,
 600  
             final String methodName, final Class<?>... parameterTypes) {
 601  2
         Class<?> parentClass = cls.getSuperclass();
 602  2
         while (parentClass != null) {
 603  2
             if (Modifier.isPublic(parentClass.getModifiers())) {
 604  
                 try {
 605  2
                     return parentClass.getMethod(methodName, parameterTypes);
 606  2
                 } catch (final NoSuchMethodException e) {
 607  2
                     return null;
 608  
                 }
 609  
             }
 610  0
             parentClass = parentClass.getSuperclass();
 611  
         }
 612  0
         return null;
 613  
     }
 614  
 
 615  
     /**
 616  
      * <p>Returns an accessible method (that is, one that can be invoked via
 617  
      * reflection) that implements the specified method, by scanning through
 618  
      * all implemented interfaces and subinterfaces. If no such method
 619  
      * can be found, return {@code null}.</p>
 620  
      *
 621  
      * <p>There isn't any good reason why this method must be {@code private}.
 622  
      * It is because there doesn't seem any reason why other classes should
 623  
      * call this rather than the higher level methods.</p>
 624  
      *
 625  
      * @param cls Parent class for the interfaces to be checked
 626  
      * @param methodName Method name of the method we wish to call
 627  
      * @param parameterTypes The parameter type signatures
 628  
      * @return the accessible method or {@code null} if not found
 629  
      */
 630  
     private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls,
 631  
             final String methodName, final Class<?>... parameterTypes) {
 632  
         // Search up the superclass chain
 633  18
         for (; cls != null; cls = cls.getSuperclass()) {
 634  
 
 635  
             // Check the implemented interfaces of the parent class
 636  12
             final Class<?>[] interfaces = cls.getInterfaces();
 637  14
             for (int i = 0; i < interfaces.length; i++) {
 638  
                 // Is this interface public?
 639  10
                 if (!Modifier.isPublic(interfaces[i].getModifiers())) {
 640  2
                     continue;
 641  
                 }
 642  
                 // Does the method exist on this interface?
 643  
                 try {
 644  8
                     return interfaces[i].getDeclaredMethod(methodName,
 645  
                             parameterTypes);
 646  0
                 } catch (final NoSuchMethodException e) { // NOPMD
 647  
                     /*
 648  
                      * Swallow, if no method is found after the loop then this
 649  
                      * method returns null.
 650  
                      */
 651  
                 }
 652  
                 // Recursively check our parent interfaces
 653  0
                 final Method method = getAccessibleMethodFromInterfaceNest(interfaces[i],
 654  
                         methodName, parameterTypes);
 655  0
                 if (method != null) {
 656  0
                     return method;
 657  
                 }
 658  
             }
 659  
         }
 660  2
         return null;
 661  
     }
 662  
 
 663  
     /**
 664  
      * <p>Finds an accessible method that matches the given name and has compatible parameters.
 665  
      * Compatible parameters mean that every method parameter is assignable from 
 666  
      * the given parameters.
 667  
      * In other words, it finds a method with the given name 
 668  
      * that will take the parameters given.</p>
 669  
      *
 670  
      * <p>This method is used by 
 671  
      * {@link 
 672  
      * #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
 673  
      * </p>
 674  
      *
 675  
      * <p>This method can match primitive parameter by passing in wrapper classes.
 676  
      * For example, a {@code Boolean} will match a primitive {@code boolean}
 677  
      * parameter.
 678  
      * </p>
 679  
      *
 680  
      * @param cls find method in this class
 681  
      * @param methodName find method with this name
 682  
      * @param parameterTypes find method with most compatible parameters 
 683  
      * @return The accessible method
 684  
      */
 685  
     public static Method getMatchingAccessibleMethod(final Class<?> cls,
 686  
             final String methodName, final Class<?>... parameterTypes) {
 687  
         try {
 688  198
             final Method method = cls.getMethod(methodName, parameterTypes);
 689  50
             MemberUtils.setAccessibleWorkaround(method);
 690  50
             return method;
 691  148
         } catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception
 692  
         }
 693  
         // search through all methods
 694  148
         Method bestMatch = null;
 695  148
         final Method[] methods = cls.getMethods();
 696  6494
         for (final Method method : methods) {
 697  
             // compare name and parameters
 698  6346
             if (method.getName().equals(methodName) &&
 699  
                     MemberUtils.isMatchingMethod(method, parameterTypes)) {
 700  
                 // get accessible version of method
 701  310
                 final Method accessibleMethod = getAccessibleMethod(method);
 702  310
                 if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit(
 703  
                             accessibleMethod,
 704  
                             bestMatch,
 705  
                             parameterTypes) < 0)) {
 706  211
                     bestMatch = accessibleMethod;
 707  
                 }
 708  
             }
 709  
         }
 710  148
         if (bestMatch != null) {
 711  138
             MemberUtils.setAccessibleWorkaround(bestMatch);
 712  
         }
 713  148
         return bestMatch;
 714  
     }
 715  
     
 716  
     /**
 717  
      * <p>Retrieves a method whether or not it's accessible. If no such method
 718  
      * can be found, return {@code null}.</p>
 719  
      * @param cls The class that will be subjected to the method search
 720  
      * @param methodName The method that we wish to call
 721  
      * @param parameterTypes Argument class types
 722  
      * @return The method
 723  
      * 
 724  
      * @since 3.5
 725  
      */
 726  
     public static Method getMatchingMethod(final Class<?> cls, final String methodName,
 727  
             final Class<?>... parameterTypes) {
 728  14
             Validate.notNull(cls, "Null class not allowed.");
 729  14
             Validate.notEmpty(methodName, "Null or blank methodName not allowed.");
 730  
             
 731  
             // Address methods in superclasses
 732  14
             Method[] methodArray = cls.getDeclaredMethods();
 733  14
             List<Class<?>> superclassList = ClassUtils.getAllSuperclasses(cls);
 734  14
             for (Class<?> klass: superclassList) {
 735  14
                     methodArray = ArrayUtils.addAll(methodArray, klass.getDeclaredMethods());
 736  14
             }
 737  
             
 738  14
             Method inexactMatch = null;
 739  671
             for (Method method: methodArray) {
 740  667
                     if (methodName.equals(method.getName()) && 
 741  
                                     ArrayUtils.isEquals(parameterTypes, method.getParameterTypes())) {
 742  10
                             return method;
 743  657
                     } else if (methodName.equals(method.getName()) &&  
 744  
                                     ClassUtils.isAssignable(parameterTypes, method.getParameterTypes(), true)) {
 745  11
                             if (inexactMatch == null) {
 746  7
                                     inexactMatch = method;
 747  4
                             } else if (distance(parameterTypes, method.getParameterTypes()) 
 748  
                                             < distance(parameterTypes, inexactMatch.getParameterTypes())) {
 749  0
                                     inexactMatch = method;
 750  
                             }
 751  
                     }
 752  
                     
 753  
             }
 754  4
             return inexactMatch;
 755  
     }
 756  
     
 757  
     /**
 758  
      * <p>Returns the aggregate number of inheritance hops between assignable argument class types.  Returns -1
 759  
      * if the arguments aren't assignable.  Fills a specific purpose for getMatchingMethod and is not generalized.</p>
 760  
      * @param classArray
 761  
      * @param toClassArray
 762  
      * @return the aggregate number of inheritance hops between assignable argument class types.
 763  
      */
 764  
     private static int distance(Class<?>[] classArray, Class<?>[] toClassArray) {
 765  16
             int answer=0;
 766  
             
 767  16
             if (!ClassUtils.isAssignable(classArray, toClassArray, true)) {
 768  2
                     return -1;
 769  
             }
 770  28
             for (int offset = 0; offset < classArray.length; offset++) {
 771  
                     // Note InheritanceUtils.distance() uses different scoring system.
 772  14
                     if (classArray[offset].equals(toClassArray[offset])) {
 773  2
                             continue;
 774  12
                     } else if (ClassUtils.isAssignable(classArray[offset], toClassArray[offset], true) 
 775  
                                     && !ClassUtils.isAssignable(classArray[offset], toClassArray[offset], false)) {
 776  6
                             answer++;
 777  
                     } else {
 778  6
                             answer = answer+2;
 779  
                     }
 780  
             }
 781  
             
 782  14
             return answer;
 783  
     }
 784  
 
 785  
     /**
 786  
      * Get the hierarchy of overridden methods down to {@code result} respecting generics.
 787  
      * @param method lowest to consider
 788  
      * @param interfacesBehavior whether to search interfaces, {@code null} {@code implies} false
 789  
      * @return Set&lt;Method&gt; in ascending order from sub- to superclass
 790  
      * @throws NullPointerException if the specified method is {@code null}
 791  
      * @since 3.2
 792  
      */
 793  
     public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
 794  4
         Validate.notNull(method);
 795  4
         final Set<Method> result = new LinkedHashSet<Method>();
 796  4
         result.add(method);
 797  
 
 798  4
         final Class<?>[] parameterTypes = method.getParameterTypes();
 799  
 
 800  4
         final Class<?> declaringClass = method.getDeclaringClass();
 801  
 
 802  4
         final Iterator<Class<?>> hierarchy = ClassUtils.hierarchy(declaringClass, interfacesBehavior).iterator();
 803  
         //skip the declaring class :P
 804  4
         hierarchy.next();
 805  14
         hierarchyTraversal: while (hierarchy.hasNext()) {
 806  10
             final Class<?> c = hierarchy.next();
 807  10
             final Method m = getMatchingAccessibleMethod(c, method.getName(), parameterTypes);
 808  10
             if (m == null) {
 809  4
                 continue;
 810  
             }
 811  6
             if (Arrays.equals(m.getParameterTypes(), parameterTypes)) {
 812  
                 // matches without generics
 813  0
                 result.add(m);
 814  0
                 continue;
 815  
             }
 816  
             // necessary to get arguments every time in the case that we are including interfaces
 817  6
             final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(declaringClass, m.getDeclaringClass());
 818  12
             for (int i = 0; i < parameterTypes.length; i++) {
 819  6
                 final Type childType = TypeUtils.unrollVariables(typeArguments, method.getGenericParameterTypes()[i]);
 820  6
                 final Type parentType = TypeUtils.unrollVariables(typeArguments, m.getGenericParameterTypes()[i]);
 821  6
                 if (!TypeUtils.equals(childType, parentType)) {
 822  0
                     continue hierarchyTraversal;
 823  
                 }
 824  
             }
 825  6
             result.add(m);
 826  6
         }
 827  4
         return result;
 828  
     }
 829  
 
 830  
     /**
 831  
      * Gets all methods of the given class that are annotated with the given annotation.
 832  
      * @param cls
 833  
      *            the {@link Class} to query
 834  
      * @param annotationCls
 835  
      *            the {@link java.lang.annotation.Annotation} that must be present on a method to be matched
 836  
      * @return an array of Methods (possibly empty).
 837  
      * @throws IllegalArgumentException
 838  
      *            if the class or annotation are {@code null}
 839  
      * @since 3.4
 840  
      */
 841  
     public static Method[] getMethodsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
 842  10
         final List<Method> annotatedMethodsList = getMethodsListWithAnnotation(cls, annotationCls);
 843  4
         return annotatedMethodsList.toArray(new Method[annotatedMethodsList.size()]);
 844  
     }
 845  
 
 846  
     /**
 847  
      * Gets all methods of the given class that are annotated with the given annotation.
 848  
      * @param cls
 849  
      *            the {@link Class} to query
 850  
      * @param annotationCls
 851  
      *            the {@link Annotation} that must be present on a method to be matched
 852  
      * @return a list of Methods (possibly empty).
 853  
      * @throws IllegalArgumentException
 854  
      *            if the class or annotation are {@code null}
 855  
      * @since 3.4
 856  
      */
 857  
     public static List<Method> getMethodsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
 858  20
         Validate.isTrue(cls != null, "The class must not be null");
 859  12
         Validate.isTrue(annotationCls != null, "The annotation class must not be null");
 860  8
         final Method[] allMethods = cls.getMethods();
 861  8
         final List<Method> annotatedMethods = new ArrayList<Method>();
 862  200
         for (final Method method : allMethods) {
 863  192
             if (method.getAnnotation(annotationCls) != null) {
 864  8
                 annotatedMethods.add(method);
 865  
             }
 866  
         }
 867  8
         return annotatedMethods;
 868  
     }
 869  
 
 870  
 }