public MockGetawayCall(final Class mockGetawayClass) { getawayMethod = WhiteboxImpl.getMethod( mockGetawayClass, "suppressConstructorCall", Class.class, Object[].class, Class[].class ); }
/** * Convenience method to get a method from a class type without having to * catch the checked exceptions otherwise required. These exceptions are * wrapped as runtime exceptions. * <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 MethodNotFoundException} * is thrown. Since the method name is not specified an * {@link TooManyMethodsFoundException} is thrown if two or more methods * matches the same parameter types in the same class. * * @param type * The type of the class where the method is located. * @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. * @throws TooManyMethodsFoundException * If several methods were found. */ public static Method getMethod(Class<?> type, Class<?>... parameterTypes) { return WhiteboxImpl.getMethod(type, parameterTypes); }
/** * Convenience method to get a method from a class type without having to * catch the checked exceptions otherwise required. These exceptions are * wrapped as runtime exceptions. * <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 MethodNotFoundException} * is thrown. Since the method name is not specified an * {@link TooManyMethodsFoundException} is thrown if two or more methods * matches the same parameter types in the same class. * * @param type * The type of the class where the method is located. * @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. * @throws TooManyMethodsFoundException * If several methods were found. */ public static Method getMethod(Class<?> type, Class<?>... parameterTypes) { return WhiteboxImpl.getMethod(type, parameterTypes); }
/** * Convenience method to get a method from a class type without having to * catch the checked exceptions otherwise required. These exceptions are * wrapped as runtime exceptions. * <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 type * The type 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 getMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { return WhiteboxImpl.getMethod(type, methodName, parameterTypes); }
/** * Convenience method to get a method from a class type without having to * catch the checked exceptions otherwise required. These exceptions are * wrapped as runtime exceptions. * <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 type * The type 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 getMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { return WhiteboxImpl.getMethod(type, methodName, parameterTypes); }
/** * Invoke a private method in that is located in a subclass of an instance. * This might be useful to test overloaded private methods. * * Use this for overloaded methods only, if possible use * * @param <T> the generic type * @param object the object * @param declaringClass the declaring class * @param methodToExecute the method to execute * @param parameterTypes the parameter types * @param arguments the arguments * @return the t * @throws Exception Exception that may occur when invoking this method. * {@link #invokeMethod(Object, Object...)} or * {@link #invokeMethod(Object, String, Object...)} instead. */ @SuppressWarnings("unchecked") public static synchronized <T> T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (object == null) { throw new IllegalArgumentException("object cannot be null"); } final Method methodToInvoke = getMethod(declaringClass, methodToExecute, parameterTypes); // Invoke method return (T) performMethodInvocation(object, methodToInvoke, arguments); }
private static Method findMethodToInvoke(String methodName, Class<?>[] sig, Class<?> objectType) { /* * if invocationControl is null or the method is not mocked, invoke * original method or suppress the method code otherwise invoke the * invocation handler. */ Method method; try { method = WhiteboxImpl.getBestMethodCandidate(objectType, methodName, sig, true); } catch (MethodNotFoundException e) { /* * Dirty hack to get around issue 110 * (http://code.google.com/p/powermock/issues/detail?id=110). Review * this! What we do here is to try to find a reflective method on * class. This has begun to fail since version 1.2 when we supported * mocking static methods in system classes. */ try { method = WhiteboxImpl.getMethod(Class.class, methodName, sig); } catch (MethodNotFoundException e2) { throw e; } } return method; } }
final Method methodToInvoke = getMethod(declaringClass, methodToExecute, parameterTypes);
/** * 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); }
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);
/** * 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); }