/** * Find method or throw exception. * * @param type the type * @param methodName the method name * @param parameterTypes the parameter types * @return the method */ @SuppressWarnings("all") public static Method findMethodOrThrowException(Class<?> type, String methodName, Class<?>... parameterTypes) { Method methodToMock = findMethod(type, methodName, parameterTypes); throwExceptionIfMethodWasNotFound(type, methodName, methodToMock, (Object[]) parameterTypes); return methodToMock; }
/** * Find method or throw exception. * * @param type * the type * @param methodName * the method name * @param parameterTypes * the parameter types * @return the method */ @SuppressWarnings("all") public static Method findMethodOrThrowException(Class<?> type, String methodName, Class<?>... parameterTypes) { Method methodToMock = findMethod(type, methodName, parameterTypes); throwExceptionIfMethodWasNotFound(type, methodName, methodToMock, parameterTypes); return methodToMock; }
/** * Get a method when it cannot be determined by methodName or parameter * types only. * <p> * The method will first try to look for a declared method in the same * class. If the method is not declared in this class it will look for the * method in the super class. This will continue throughout the whole class * hierarchy. If the method is not found an {@link IllegalArgumentException} * is thrown. * * @param declaringClass * The declaringClass of the class where the method is located. * @param methodName * The method names. * @param parameterTypes * All parameter types of the method (may be <code>null</code>). * @return A <code>java.lang.reflect.Method</code>. * @throws MethodNotFoundException * If a method cannot be found in the hierarchy. */ public static Method method(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) { final Method method = WhiteboxImpl.findMethod(declaringClass, methodName, parameterTypes); WhiteboxImpl.throwExceptionIfMethodWasNotFound(declaringClass, methodName, method, (Object[]) parameterTypes); return method; }
/** * Get a method when it cannot be determined by methodName or parameter * types only. * <p> * The method will first try to look for a declared method in the same * class. If the method is not declared in this class it will look for the * method in the super class. This will continue throughout the whole class * hierarchy. If the method is not found an {@link IllegalArgumentException} * is thrown. * * @param declaringClass * The declaringClass of the class where the method is located. * @param methodName * The method names. * @param parameterTypes * All parameter types of the method (may be {@code null}). * @return A {@code java.lang.reflect.Method}. * @throws MethodNotFoundException * If a method cannot be found in the hierarchy. */ public static Method method(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) { final Method method = WhiteboxImpl.findMethod(declaringClass, methodName, parameterTypes); WhiteboxImpl.throwExceptionIfMethodWasNotFound(declaringClass, methodName, method, (Object[]) parameterTypes); return method; }
/** * Used to specify expectations on private methods. Use this method to * handle overloaded methods. */ @SuppressWarnings("all") public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (arguments == null) { arguments = new Object[0]; } if (instance == null) { throw new IllegalArgumentException("instance cannot be null."); } else if (arguments.length != parameterTypes.length) { throw new IllegalArgumentException("The length of the arguments must be equal to the number of parameter types."); } Method foundMethod = Whitebox.getMethod(instance.getClass(), methodName, parameterTypes); WhiteboxImpl.throwExceptionIfMethodWasNotFound(instance.getClass(), methodName, foundMethod, parameterTypes); return doExpectPrivate(instance, foundMethod, arguments); }
throwExceptionIfMethodWasNotFound(type, methodName, null, new Object[] { parameterTypes }); return null;
throwExceptionIfMethodWasNotFound(type, methodName, null, new Object[]{parameterTypes}); return null;
/** * Invoke a private or inner class method in a subclass (defined by * {@code definedIn}) in cases where power mock cannot automatically * determine the type of the parameters, for example when mixing primitive * types and wrapper types in the same method. For most situations use * * @param <T> the generic type * @param tested the tested * @param methodToExecute the method to execute * @param definedIn the defined in * @param argumentTypes the argument types * @param arguments the arguments * @return the t * @throws Exception Exception that may occur when invoking this method. * {@link #invokeMethod(Class, String, Object...)} instead. */ @SuppressWarnings("unchecked") public static synchronized <T> T invokeMethod(Object tested, String methodToExecute, Class<?> definedIn, Class<?>[] argumentTypes, Object... arguments) throws Exception { Method method = getMethod(definedIn, methodToExecute, argumentTypes); if (method == null) { throwExceptionIfMethodWasNotFound(definedIn, methodToExecute, null, arguments); } return (T) performMethodInvocation(tested, method, arguments); }
Method method = getMethod(definedIn, methodToExecute, argumentTypes); if (method == null) { throwExceptionIfMethodWasNotFound(definedIn, methodToExecute, null, arguments);
/** * Invoke a private or inner class method in cases where power mock cannot * automatically determine the type of the parameters, for example when * mixing primitive types and wrapper types in the same method. For most * situations use {@link #invokeMethod(Class, String, Object...)} instead. * * @param <T> the generic type * @param tested the tested * @param methodToExecute the method to execute * @param argumentTypes the argument types * @param arguments the arguments * @return the t * @throws Exception Exception that may occur when invoking this method. */ @SuppressWarnings("unchecked") public static synchronized <T> T invokeMethod(Object tested, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception { final Class<?> unmockedType = getType(tested); Method method = getMethod(unmockedType, methodToExecute, argumentTypes); if (method == null) { throwExceptionIfMethodWasNotFound(unmockedType, methodToExecute, null, arguments); } return (T) performMethodInvocation(tested, method, arguments); }
/** * Invoke a private or inner class method in cases where power mock cannot * automatically determine the type of the parameters, for example when * mixing primitive types and wrapper types in the same method. For most * situations use {@link #invokeMethod(Class, String, Object...)} instead. * * @param <T> * the generic type * @param tested * the tested * @param methodToExecute * the method to execute * @param argumentTypes * the argument types * @param arguments * the arguments * @return the t * @throws Exception * Exception that may occur when invoking this method. */ @SuppressWarnings("unchecked") public static synchronized <T> T invokeMethod(Object tested, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception { final Class<?> unmockedType = getType(tested); Method method = getMethod(unmockedType, methodToExecute, argumentTypes); if (method == null) { throwExceptionIfMethodWasNotFound(unmockedType, methodToExecute, null, arguments); } return (T) performMethodInvocation(tested, method, arguments); }
WhiteboxImpl.throwExceptionIfMethodWasNotFound(getType(tested), methodToExecute, potentialMethodToInvoke, arguments); return potentialMethodToInvoke;
WhiteboxImpl.throwExceptionIfMethodWasNotFound(getType(tested), methodToExecute, potentialMethodToInvoke, arguments); return potentialMethodToInvoke;