Coverage Report - org.apache.commons.lang3.reflect.MethodUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodUtils
92%
112/121
87%
51/58
3,263
 
 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.InvocationTargetException;
 21  
 import java.lang.reflect.Method;
 22  
 import java.lang.reflect.Modifier;
 23  
 import java.lang.reflect.Type;
 24  
 import java.lang.reflect.TypeVariable;
 25  
 import java.util.Arrays;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Iterator;
 28  
 import java.util.LinkedHashSet;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.Set;
 32  
 
 33  
 import org.apache.commons.lang3.ArrayUtils;
 34  
 import org.apache.commons.lang3.ClassUtils;
 35  
 import org.apache.commons.lang3.ClassUtils.Interfaces;
 36  
 import org.apache.commons.lang3.Validate;
 37  
 
 38  
 /**
 39  
  * <p>Utility reflection methods focused on {@link Method}s, originally from Commons BeanUtils.
 40  
  * Differences from the BeanUtils version may be noted, especially where similar functionality
 41  
  * already existed within Lang.
 42  
  * </p>
 43  
  *
 44  
  * <h3>Known Limitations</h3>
 45  
  * <h4>Accessing Public Methods In A Default Access Superclass</h4>
 46  
  * <p>There is an issue when invoking {@code public} methods contained in a default access superclass on JREs prior to 1.4.
 47  
  * Reflection locates these methods fine and correctly assigns them as {@code public}.
 48  
  * However, an {@link IllegalAccessException} is thrown if the method is invoked.</p>
 49  
  *
 50  
  * <p>{@link MethodUtils} contains a workaround for this situation. 
 51  
  * It will attempt to call {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} on this method.
 52  
  * If this call succeeds, then the method can be invoked as normal.
 53  
  * This call will only succeed when the application has sufficient security privileges. 
 54  
  * If this call fails then the method may fail.</p>
 55  
  *
 56  
  * @since 2.5
 57  
  */
 58  
 public class MethodUtils {
 59  
 
 60  
     /**
 61  
      * <p>{@link MethodUtils} instances should NOT be constructed in standard programming.
 62  
      * Instead, the class should be used as
 63  
      * {@code MethodUtils.getAccessibleMethod(method)}.</p>
 64  
      *
 65  
      * <p>This constructor is {@code public} to permit tools that require a JavaBean
 66  
      * instance to operate.</p>
 67  
      */
 68  
     public MethodUtils() {
 69  2
         super();
 70  2
     }
 71  
 
 72  
     /**
 73  
      * <p>Invokes a named method without parameters.</p>
 74  
      *
 75  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 76  
      *
 77  
      * <p>This is a convenient wrapper for
 78  
      * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
 79  
      * </p>
 80  
      *
 81  
      * @param object invoke method on this object
 82  
      * @param methodName get method with this name
 83  
      * @return The value returned by the invoked method
 84  
      *
 85  
      * @throws NoSuchMethodException if there is no such accessible method
 86  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 87  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 88  
      *  
 89  
      *  @since 3.4
 90  
      */
 91  
     public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException,
 92  
             IllegalAccessException, InvocationTargetException {
 93  3
         return invokeMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
 94  
     }
 95  
 
 96  
     /**
 97  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 98  
      *
 99  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 100  
      *
 101  
      * <p>This method supports calls to methods taking primitive parameters 
 102  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 103  
      * would match a {@code boolean} primitive.</p>
 104  
      *
 105  
      * <p>This is a convenient wrapper for
 106  
      * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
 107  
      * </p>
 108  
      *
 109  
      * @param object invoke method on this object
 110  
      * @param methodName get method with this name
 111  
      * @param args use these arguments - treat null as empty array
 112  
      * @return The value returned by the invoked method
 113  
      *
 114  
      * @throws NoSuchMethodException if there is no such accessible method
 115  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 116  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 117  
      */
 118  
     public static Object invokeMethod(final Object object, final String methodName,
 119  
             Object... args) throws NoSuchMethodException,
 120  
             IllegalAccessException, InvocationTargetException {
 121  17
         args = ArrayUtils.nullToEmpty(args);
 122  17
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 123  17
         return invokeMethod(object, methodName, args, parameterTypes);
 124  
     }
 125  
 
 126  
     /**
 127  
      * <p>Invokes a named method whose parameter type matches the object type.</p>
 128  
      *
 129  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 130  
      *
 131  
      * <p>This method supports calls to methods taking primitive parameters 
 132  
      * via passing in wrapping classes. So, for example, a {@code Boolean} object
 133  
      * would match a {@code boolean} primitive.</p>
 134  
      *
 135  
      * @param object invoke method on this object
 136  
      * @param methodName get method with this name
 137  
      * @param args use these arguments - treat null as empty array
 138  
      * @param parameterTypes match these parameters - treat null as empty array
 139  
      * @return The value returned by the invoked method
 140  
      *
 141  
      * @throws NoSuchMethodException if there is no such accessible method
 142  
      * @throws InvocationTargetException wraps an exception thrown by the method invoked
 143  
      * @throws IllegalAccessException if the requested method is not accessible via reflection
 144  
      */
 145  
     public static Object invokeMethod(final Object object, final String methodName,
 146  
             Object[] args, Class<?>[] parameterTypes)
 147  
             throws NoSuchMethodException, IllegalAccessException,
 148  
             InvocationTargetException {
 149  21
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 150  21
         args = ArrayUtils.nullToEmpty(args);
 151  21
         final Method method = getMatchingAccessibleMethod(object.getClass(),
 152  
                 methodName, parameterTypes);
 153  21
         if (method == null) {
 154  2
             throw new NoSuchMethodException("No such accessible method: "
 155  
                     + methodName + "() on object: "
 156  
                     + object.getClass().getName());
 157  
         }
 158  19
         return method.invoke(object, args);
 159  
     }
 160  
 
 161  
     /**
 162  
      * <p>Invokes a method whose parameter types match exactly the object
 163  
      * types.</p>
 164  
      *
 165  
      * <p>This uses reflection to invoke the method obtained from a call to
 166  
      * {@link #getAccessibleMethod}(Class,String,Class[])}.</p>
 167  
      *
 168  
      * @param object invoke method on this object
 169  
      * @param methodName get method with this name
 170  
      * @return The value returned by the invoked method
 171  
      *
 172  
      * @throws NoSuchMethodException if there is no such accessible method
 173  
      * @throws InvocationTargetException wraps an exception thrown by the
 174  
      *  method invoked
 175  
      * @throws IllegalAccessException if the requested method is not accessible
 176  
      *  via reflection
 177  
      *  
 178  
      *  @since 3.4
 179  
      */
 180  
     public static Object invokeExactMethod(final Object object, final String methodName) throws NoSuchMethodException,
 181  
             IllegalAccessException, InvocationTargetException {
 182  1
         return invokeExactMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
 183  
     }
 184  
 
 185  
     /**
 186  
      * <p>Invokes a method with no parameters.</p>
 187  
      *
 188  
      * <p>This uses reflection to invoke the method obtained from a call to
 189  
      * {@link #getAccessibleMethod}(Class,String,Class[])}.</p>
 190  
      *
 191  
      * @param object invoke method on this object
 192  
      * @param methodName get method with this name
 193  
      * @param args use these arguments - treat null as empty array
 194  
      * @return The value returned by the invoked method
 195  
      *
 196  
      * @throws NoSuchMethodException if there is no such accessible method
 197  
      * @throws InvocationTargetException wraps an exception thrown by the
 198  
      *  method invoked
 199  
      * @throws IllegalAccessException if the requested method is not accessible
 200  
      *  via reflection
 201  
      */
 202  
     public static Object invokeExactMethod(final Object object, final String methodName,
 203  
             Object... args) throws NoSuchMethodException,
 204  
             IllegalAccessException, InvocationTargetException {
 205  8
         args = ArrayUtils.nullToEmpty(args);
 206  8
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 207  8
         return invokeExactMethod(object, methodName, args, parameterTypes);
 208  
     }
 209  
 
 210  
     /**
 211  
      * <p>Invokes a method whose parameter types match exactly the parameter
 212  
      * types given.</p>
 213  
      *
 214  
      * <p>This uses reflection to invoke the method obtained from a call to
 215  
      * {@link #getAccessibleMethod(Class,String,Class[])}.</p>
 216  
      *
 217  
      * @param object invoke method on this object
 218  
      * @param methodName get method with this name
 219  
      * @param args use these arguments - treat null as empty array
 220  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 221  
      * @return The value returned by the invoked method
 222  
      *
 223  
      * @throws NoSuchMethodException if there is no such accessible method
 224  
      * @throws InvocationTargetException wraps an exception thrown by the
 225  
      *  method invoked
 226  
      * @throws IllegalAccessException if the requested method is not accessible
 227  
      *  via reflection
 228  
      */
 229  
     public static Object invokeExactMethod(final Object object, final String methodName,
 230  
             Object[] args, Class<?>[] parameterTypes)
 231  
             throws NoSuchMethodException, IllegalAccessException,
 232  
             InvocationTargetException {
 233  11
         args = ArrayUtils.nullToEmpty(args);
 234  11
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 235  11
         final Method method = getAccessibleMethod(object.getClass(), methodName,
 236  
                 parameterTypes);
 237  11
         if (method == null) {
 238  3
             throw new NoSuchMethodException("No such accessible method: "
 239  
                     + methodName + "() on object: "
 240  
                     + object.getClass().getName());
 241  
         }
 242  8
         return method.invoke(object, args);
 243  
     }
 244  
 
 245  
     /**
 246  
      * <p>Invokes a {@code static} method whose parameter types match exactly the parameter
 247  
      * types given.</p>
 248  
      *
 249  
      * <p>This uses reflection to invoke the method obtained from a call to
 250  
      * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 251  
      *
 252  
      * @param cls invoke static method on this class
 253  
      * @param methodName get method with this name
 254  
      * @param args use these arguments - treat {@code null} as empty array
 255  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 256  
      * @return The value returned by the invoked method
 257  
      *
 258  
      * @throws NoSuchMethodException if there is no such accessible method
 259  
      * @throws InvocationTargetException wraps an exception thrown by the
 260  
      *  method invoked
 261  
      * @throws IllegalAccessException if the requested method is not accessible
 262  
      *  via reflection
 263  
      */
 264  
     public static Object invokeExactStaticMethod(final Class<?> cls, final String methodName,
 265  
             Object[] args, Class<?>[] parameterTypes)
 266  
             throws NoSuchMethodException, IllegalAccessException,
 267  
             InvocationTargetException {
 268  10
         args = ArrayUtils.nullToEmpty(args);
 269  10
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 270  10
         final Method method = getAccessibleMethod(cls, methodName, parameterTypes);
 271  10
         if (method == null) {
 272  3
             throw new NoSuchMethodException("No such accessible method: "
 273  
                     + methodName + "() on class: " + cls.getName());
 274  
         }
 275  7
         return method.invoke(null, args);
 276  
     }
 277  
 
 278  
     /**
 279  
      * <p>Invokes a named {@code static} method whose parameter type matches the object type.</p>
 280  
      *
 281  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 282  
      *
 283  
      * <p>This method supports calls to methods taking primitive parameters 
 284  
      * via passing in wrapping classes. So, for example, a {@code Boolean} class
 285  
      * would match a {@code boolean} primitive.</p>
 286  
      *
 287  
      * <p>This is a convenient wrapper for
 288  
      * {@link #invokeStaticMethod(Class, String, Object[], Class[])}.
 289  
      * </p>
 290  
      *
 291  
      * @param cls invoke static method on this class
 292  
      * @param methodName get method with this name
 293  
      * @param args use these arguments - treat {@code null} as empty array
 294  
      * @return The value returned by the invoked method
 295  
      *
 296  
      * @throws NoSuchMethodException if there is no such accessible method
 297  
      * @throws InvocationTargetException wraps an exception thrown by the
 298  
      *  method invoked
 299  
      * @throws IllegalAccessException if the requested method is not accessible
 300  
      *  via reflection
 301  
      */
 302  
     public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
 303  
             Object... args) throws NoSuchMethodException,
 304  
             IllegalAccessException, InvocationTargetException {
 305  10
         args = ArrayUtils.nullToEmpty(args);
 306  10
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 307  10
         return invokeStaticMethod(cls, methodName, args, parameterTypes);
 308  
     }
 309  
 
 310  
     /**
 311  
      * <p>Invokes a named {@code static} method whose parameter type matches the object type.</p>
 312  
      *
 313  
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 314  
      *
 315  
      * <p>This method supports calls to methods taking primitive parameters 
 316  
      * via passing in wrapping classes. So, for example, a {@code Boolean} class
 317  
      * would match a {@code boolean} primitive.</p>
 318  
      *
 319  
      *
 320  
      * @param cls invoke static method on this class
 321  
      * @param methodName get method with this name
 322  
      * @param args use these arguments - treat {@code null} as empty array
 323  
      * @param parameterTypes match these parameters - treat {@code null} as empty array
 324  
      * @return The value returned by the invoked method
 325  
      *
 326  
      * @throws NoSuchMethodException if there is no such accessible method
 327  
      * @throws InvocationTargetException wraps an exception thrown by the
 328  
      *  method invoked
 329  
      * @throws IllegalAccessException if the requested method is not accessible
 330  
      *  via reflection
 331  
      */
 332  
     public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
 333  
             Object[] args, Class<?>[] parameterTypes)
 334  
             throws NoSuchMethodException, IllegalAccessException,
 335  
             InvocationTargetException {
 336  11
         args = ArrayUtils.nullToEmpty(args);
 337  11
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
 338  11
         final Method method = getMatchingAccessibleMethod(cls, methodName,
 339  
                 parameterTypes);
 340  11
         if (method == null) {
 341  1
             throw new NoSuchMethodException("No such accessible method: "
 342  
                     + methodName + "() on class: " + cls.getName());
 343  
         }
 344  10
         return method.invoke(null, args);
 345  
     }
 346  
 
 347  
     /**
 348  
      * <p>Invokes a {@code static} method whose parameter types match exactly the object
 349  
      * types.</p>
 350  
      *
 351  
      * <p>This uses reflection to invoke the method obtained from a call to
 352  
      * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 353  
      *
 354  
      * @param cls invoke static method on this class
 355  
      * @param methodName get method with this name
 356  
      * @param args use these arguments - treat {@code null} as empty array
 357  
      * @return The value returned by the invoked method
 358  
      *
 359  
      * @throws NoSuchMethodException if there is no such accessible method
 360  
      * @throws InvocationTargetException wraps an exception thrown by the
 361  
      *  method invoked
 362  
      * @throws IllegalAccessException if the requested method is not accessible
 363  
      *  via reflection
 364  
      */
 365  
     public static Object invokeExactStaticMethod(final Class<?> cls, final String methodName,
 366  
             Object... args) throws NoSuchMethodException,
 367  
             IllegalAccessException, InvocationTargetException {
 368  8
         args = ArrayUtils.nullToEmpty(args);
 369  8
         final Class<?>[] parameterTypes = ClassUtils.toClass(args);
 370  8
         return invokeExactStaticMethod(cls, methodName, args, parameterTypes);
 371  
     }
 372  
 
 373  
     /**
 374  
      * <p>Returns an accessible method (that is, one that can be invoked via
 375  
      * reflection) with given name and parameters. If no such method
 376  
      * can be found, return {@code null}.
 377  
      * This is just a convenience wrapper for
 378  
      * {@link #getAccessibleMethod(Method)}.</p>
 379  
      *
 380  
      * @param cls get method from this class
 381  
      * @param methodName get method with this name
 382  
      * @param parameterTypes with these parameters types
 383  
      * @return The accessible method
 384  
      */
 385  
     public static Method getAccessibleMethod(final Class<?> cls, final String methodName,
 386  
             final Class<?>... parameterTypes) {
 387  
         try {
 388  30
             return getAccessibleMethod(cls.getMethod(methodName,
 389  
                     parameterTypes));
 390  8
         } catch (final NoSuchMethodException e) {
 391  8
             return null;
 392  
         }
 393  
     }
 394  
 
 395  
     /**
 396  
      * <p>Returns an accessible method (that is, one that can be invoked via
 397  
      * reflection) that implements the specified Method. If no such method
 398  
      * can be found, return {@code null}.</p>
 399  
      *
 400  
      * @param method The method that we wish to call
 401  
      * @return The accessible method
 402  
      */
 403  
     public static Method getAccessibleMethod(Method method) {
 404  88
         if (!MemberUtils.isAccessible(method)) {
 405  1
             return null;
 406  
         }
 407  
         // If the declaring class is public, we are done
 408  87
         final Class<?> cls = method.getDeclaringClass();
 409  87
         if (Modifier.isPublic(cls.getModifiers())) {
 410  82
             return method;
 411  
         }
 412  5
         final String methodName = method.getName();
 413  5
         final Class<?>[] parameterTypes = method.getParameterTypes();
 414  
 
 415  
         // Check the implemented interfaces and subinterfaces
 416  5
         method = getAccessibleMethodFromInterfaceNest(cls, methodName,
 417  
                 parameterTypes);
 418  
 
 419  
         // Check the superclass chain
 420  5
         if (method == null) {
 421  1
             method = getAccessibleMethodFromSuperclass(cls, methodName,
 422  
                     parameterTypes);
 423  
         }
 424  5
         return method;
 425  
     }
 426  
 
 427  
     /**
 428  
      * <p>Returns an accessible method (that is, one that can be invoked via
 429  
      * reflection) by scanning through the superclasses. If no such method
 430  
      * can be found, return {@code null}.</p>
 431  
      *
 432  
      * @param cls Class to be checked
 433  
      * @param methodName Method name of the method we wish to call
 434  
      * @param parameterTypes The parameter type signatures
 435  
      * @return the accessible method or {@code null} if not found
 436  
      */
 437  
     private static Method getAccessibleMethodFromSuperclass(final Class<?> cls,
 438  
             final String methodName, final Class<?>... parameterTypes) {
 439  1
         Class<?> parentClass = cls.getSuperclass();
 440  1
         while (parentClass != null) {
 441  1
             if (Modifier.isPublic(parentClass.getModifiers())) {
 442  
                 try {
 443  1
                     return parentClass.getMethod(methodName, parameterTypes);
 444  1
                 } catch (final NoSuchMethodException e) {
 445  1
                     return null;
 446  
                 }
 447  
             }
 448  0
             parentClass = parentClass.getSuperclass();
 449  
         }
 450  0
         return null;
 451  
     }
 452  
 
 453  
     /**
 454  
      * <p>Returns an accessible method (that is, one that can be invoked via
 455  
      * reflection) that implements the specified method, by scanning through
 456  
      * all implemented interfaces and subinterfaces. If no such method
 457  
      * can be found, return {@code null}.</p>
 458  
      *
 459  
      * <p>There isn't any good reason why this method must be {@code private}.
 460  
      * It is because there doesn't seem any reason why other classes should
 461  
      * call this rather than the higher level methods.</p>
 462  
      *
 463  
      * @param cls Parent class for the interfaces to be checked
 464  
      * @param methodName Method name of the method we wish to call
 465  
      * @param parameterTypes The parameter type signatures
 466  
      * @return the accessible method or {@code null} if not found
 467  
      */
 468  
     private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls,
 469  
             final String methodName, final Class<?>... parameterTypes) {
 470  
         // Search up the superclass chain
 471  9
         for (; cls != null; cls = cls.getSuperclass()) {
 472  
 
 473  
             // Check the implemented interfaces of the parent class
 474  6
             final Class<?>[] interfaces = cls.getInterfaces();
 475  7
             for (int i = 0; i < interfaces.length; i++) {
 476  
                 // Is this interface public?
 477  5
                 if (!Modifier.isPublic(interfaces[i].getModifiers())) {
 478  1
                     continue;
 479  
                 }
 480  
                 // Does the method exist on this interface?
 481  
                 try {
 482  4
                     return interfaces[i].getDeclaredMethod(methodName,
 483  
                             parameterTypes);
 484  0
                 } catch (final NoSuchMethodException e) { // NOPMD
 485  
                     /*
 486  
                      * Swallow, if no method is found after the loop then this
 487  
                      * method returns null.
 488  
                      */
 489  
                 }
 490  
                 // Recursively check our parent interfaces
 491  0
                 final Method method = getAccessibleMethodFromInterfaceNest(interfaces[i],
 492  
                         methodName, parameterTypes);
 493  0
                 if (method != null) {
 494  0
                     return method;
 495  
                 }
 496  
             }
 497  
         }
 498  1
         return null;
 499  
     }
 500  
 
 501  
     /**
 502  
      * <p>Finds an accessible method that matches the given name and has compatible parameters.
 503  
      * Compatible parameters mean that every method parameter is assignable from 
 504  
      * the given parameters.
 505  
      * In other words, it finds a method with the given name 
 506  
      * that will take the parameters given.</p>
 507  
      *
 508  
      * <p>This method is used by 
 509  
      * {@link 
 510  
      * #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
 511  
      * </p>
 512  
      *
 513  
      * <p>This method can match primitive parameter by passing in wrapper classes.
 514  
      * For example, a {@code Boolean} will match a primitive {@code boolean}
 515  
      * parameter.
 516  
      * </p>
 517  
      *
 518  
      * @param cls find method in this class
 519  
      * @param methodName find method with this name
 520  
      * @param parameterTypes find method with most compatible parameters 
 521  
      * @return The accessible method
 522  
      */
 523  
     public static Method getMatchingAccessibleMethod(final Class<?> cls,
 524  
             final String methodName, final Class<?>... parameterTypes) {
 525  
         try {
 526  62
             final Method method = cls.getMethod(methodName, parameterTypes);
 527  25
             MemberUtils.setAccessibleWorkaround(method);
 528  25
             return method;
 529  37
         } catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception
 530  
         }
 531  
         // search through all methods
 532  37
         Method bestMatch = null;
 533  37
         final Method[] methods = cls.getMethods();
 534  707
         for (final Method method : methods) {
 535  
             // compare name and parameters
 536  670
             if (method.getName().equals(methodName) && ClassUtils.isAssignable(parameterTypes, method.getParameterTypes(), true)) {
 537  
                 // get accessible version of method
 538  62
                 final Method accessibleMethod = getAccessibleMethod(method);
 539  62
                 if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareParameterTypes(
 540  
                             accessibleMethod.getParameterTypes(),
 541  
                             bestMatch.getParameterTypes(),
 542  
                             parameterTypes) < 0)) {
 543  56
                         bestMatch = accessibleMethod;
 544  
                  }
 545  
             }
 546  
         }
 547  37
         if (bestMatch != null) {
 548  32
             MemberUtils.setAccessibleWorkaround(bestMatch);
 549  
         }
 550  37
         return bestMatch;
 551  
     }
 552  
 
 553  
     /**
 554  
      * Get the hierarchy of overridden methods down to {@code result} respecting generics.
 555  
      * @param method lowest to consider
 556  
      * @param interfacesBehavior whether to search interfaces, {@code null} {@code implies} false
 557  
      * @return Set&lt;Method&gt; in ascending order from sub- to superclass
 558  
      * @throws NullPointerException if the specified method is {@code null}
 559  
      * @since 3.2
 560  
      */
 561  
     public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
 562  2
         Validate.notNull(method);
 563  2
         final Set<Method> result = new LinkedHashSet<Method>();
 564  2
         result.add(method);
 565  
 
 566  2
         final Class<?>[] parameterTypes = method.getParameterTypes();
 567  
 
 568  2
         final Class<?> declaringClass = method.getDeclaringClass();
 569  
 
 570  2
         final Iterator<Class<?>> hierarchy = ClassUtils.hierarchy(declaringClass, interfacesBehavior).iterator();
 571  
         //skip the declaring class :P
 572  2
         hierarchy.next();
 573  7
         hierarchyTraversal: while (hierarchy.hasNext()) {
 574  5
             final Class<?> c = hierarchy.next();
 575  5
             final Method m = getMatchingAccessibleMethod(c, method.getName(), parameterTypes);
 576  5
             if (m == null) {
 577  2
                 continue;
 578  
             }
 579  3
             if (Arrays.equals(m.getParameterTypes(), parameterTypes)) {
 580  
                 // matches without generics
 581  0
                 result.add(m);
 582  0
                 continue;
 583  
             }
 584  
             // necessary to get arguments every time in the case that we are including interfaces
 585  3
             final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(declaringClass, m.getDeclaringClass());
 586  6
             for (int i = 0; i < parameterTypes.length; i++) {
 587  3
                 final Type childType = TypeUtils.unrollVariables(typeArguments, method.getGenericParameterTypes()[i]);
 588  3
                 final Type parentType = TypeUtils.unrollVariables(typeArguments, m.getGenericParameterTypes()[i]);
 589  3
                 if (!TypeUtils.equals(childType, parentType)) {
 590  0
                     continue hierarchyTraversal;
 591  
                 }
 592  
             }
 593  3
             result.add(m);
 594  3
         }
 595  2
         return result;
 596  
     }
 597  
 
 598  
     /**
 599  
      * Gets all methods of the given class that are annotated with the given annotation.
 600  
      * @param cls
 601  
      *            the {@link Class} to query
 602  
      * @param annotationCls
 603  
      *            the {@link java.lang.annotation.Annotation} that must be present on a method to be matched
 604  
      * @return an array of Methods (possibly empty).
 605  
      * @throws IllegalArgumentException
 606  
      *            if the class or annotation are {@code null}
 607  
      * @since 3.4
 608  
      */
 609  
     public static Method[] getMethodsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
 610  5
         final List<Method> annotatedMethodsList = getMethodsListWithAnnotation(cls, annotationCls);
 611  2
         return annotatedMethodsList.toArray(new Method[annotatedMethodsList.size()]);
 612  
     }
 613  
 
 614  
     /**
 615  
      * Gets all methods of the given class that are annotated with the given annotation.
 616  
      * @param cls
 617  
      *            the {@link Class} to query
 618  
      * @param annotationCls
 619  
      *            the {@link Annotation} that must be present on a method to be matched
 620  
      * @return a list of Methods (possibly empty).
 621  
      * @throws IllegalArgumentException
 622  
      *            if the class or annotation are {@code null}
 623  
      * @since 3.4
 624  
      */
 625  
     public static List<Method> getMethodsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
 626  10
         Validate.isTrue(cls != null, "The class must not be null");
 627  6
         Validate.isTrue(annotationCls != null, "The annotation class must not be null");
 628  4
         final Method[] allMethods = cls.getMethods();
 629  4
         final List<Method> annotatedMethods = new ArrayList<Method>();
 630  88
         for (final Method method : allMethods) {
 631  84
             if (method.getAnnotation(annotationCls) != null) {
 632  4
                 annotatedMethods.add(method);
 633  
             }
 634  
         }
 635  4
         return annotatedMethods;
 636  
     }
 637  
 
 638  
 }