public static Method findMethodOrThrowException ( Object tested , Class < ? > declaringClass , String methodToExecute , Object [ ] arguments ) { if ( tested == null ) { throw new IllegalArgumentException ( "The object to perform the operation on cannot be null." ) ; } /* * Get methods from the type if it's not mocked or from the super type * if the tested object is mocked. */ Class < ? > testedType = null ; if ( isClass ( tested ) ) { testedType = ( Class < ? > ) tested ; } else { testedType = tested . getClass ( ) ; } Method [ ] methods = null ; if ( declaringClass == null ) { methods = getAllMethods ( testedType ) ; } else { methods = declaringClass . getDeclaredMethods ( ) ; } Method potentialMethodToInvoke = null ; for ( Method method : methods ) { if ( methodToExecute == null || method . getName ( ) . equals ( methodToExecute ) ) { Class < ? > [ ] paramTypes = method . getParameterTypes ( ) ; if ( ( arguments != null && ( paramTypes . length == arguments . length ) ) ) { if ( paramTypes . length == 0 ) { potentialMethodToInvoke = method ; break ; } boolean methodFound = checkArgumentTypesMatchParameterTypes ( method . isVarArgs ( ) , paramTypes , arguments ) ; if ( methodFound ) { if ( potentialMethodToInvoke == null ) { potentialMethodToInvoke = method ; } else if ( potentialMethodToInvoke . getName ( ) . equals ( method . getName ( ) ) ) { if ( areAllArgumentsOfSameType ( arguments ) && potentialMethodToInvoke . getDeclaringClass ( ) != method . getDeclaringClass ( ) ) { // We've already found the method which means that "potentialMethodToInvoke" overrides "method". return potentialMethodToInvoke ; } else { // We've found an overloaded method return getBestMethodCandidate ( getType ( tested ) , method . getName ( ) , getTypes ( arguments ) , false ) ; } } else { // A special case to be backward compatible Method bestCandidateMethod = getMethodWithMostSpecificParameterTypes ( method , potentialMethodToInvoke ) ; if ( bestCandidateMethod != null ) { potentialMethodToInvoke = bestCandidateMethod ; continue ; } /* * We've already found a method match before, this * means that PowerMock cannot determine which * method to expect since there are two methods with * the same name and the same number of arguments * but one is using wrapper types. */ throwExceptionWhenMultipleMethodMatchesFound ( "argument parameter types" , new Method [ ] { potentialMethodToInvoke , method } ) ; } } } else if ( isPotentialVarArgsMethod ( method , arguments ) ) { if ( potentialMethodToInvoke == null ) { potentialMethodToInvoke = method ; } else { /* * We've already found a method match before, this means * that PowerMock cannot determine which method to * expect since there are two methods with the same name * and the same number of arguments but one is using * wrapper types. */ throwExceptionWhenMultipleMethodMatchesFound ( "argument parameter types" , new Method [ ] { potentialMethodToInvoke , method } ) ; } break ; } else if ( arguments != null && ( paramTypes . length != arguments . length ) ) { continue ; } else if ( arguments == null && paramTypes . length == 1 && ! paramTypes [ 0 ] . isPrimitive ( ) ) { potentialMethodToInvoke = method ; } } } WhiteboxImpl . throwExceptionIfMethodWasNotFound ( getType ( tested ) , methodToExecute , potentialMethodToInvoke , arguments ) ; return potentialMethodToInvoke ; }
Finds and returns a certain method . If the method couldn t be found this method delegates to