@SuppressWarnings({"unchecked", "rawtypes"}) private OngoingStubbing<T> createNewSubstituteMock(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type); if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); } NewInvocationControl<OngoingStubbing<T>> newInvocationControl = createNewInvocationControl(type, unmockedType); return newInvocationControl.expectSubstitutionLogic(arguments); }
/** * Do invoke method. * * @param <T> the generic type * @param tested the tested * @param declaringClass the declaring class * @param methodToExecute the method to execute * @param arguments the arguments * @return the t * @throws Exception the exception */ @SuppressWarnings("unchecked") private static <T> T doInvokeMethod(Object tested, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception { Method methodToInvoke = findMethodOrThrowException(tested, declaringClass, methodToExecute, arguments); // Invoke test return (T) performMethodInvocation(tested, methodToInvoke, 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); }
/** * 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; }
if (isClass(tested)) { testedType = (Class<?>) tested; } else { methods = getAllMethods(testedType); } else { methods = declaringClass.getDeclaredMethods(); if (!checkArgumentTypesMatchParameterTypes(method.isVarArgs(), paramTypes, arguments)) { wrappedMethodFound = false; if (!checkIfParameterTypesAreSame(method.isVarArgs(), convertArgumentTypesToPrimitive(paramTypes, arguments), paramTypes)) { primitiveMethodFound = false; return getBestMethodCandidate(getType(tested), method.getName(), getTypes(arguments), false); } else { throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", new Method[] { potentialMethodToInvoke, method }); } else if (isPotentialVarArgsMethod(method, arguments)) { if (potentialMethodToInvoke == null) { potentialMethodToInvoke = method; throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", new Method[] { potentialMethodToInvoke, method }); WhiteboxImpl.throwExceptionIfMethodWasNotFound(getType(tested), methodToExecute, potentialMethodToInvoke,
if (isClass(tested)) { testedType = (Class<?>) tested; } else { methods = getAllMethods(testedType); } else { methods = declaringClass.getDeclaredMethods(); 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()) { return getBestMethodCandidate(getType(tested), method.getName(), getTypes(arguments), false); Method bestCandidateMethod = getMethodWithMostSpecificParameterTypes(method, potentialMethodToInvoke); if (bestCandidateMethod != null) { potentialMethodToInvoke = bestCandidateMethod; throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", new Method[]{ potentialMethodToInvoke, method}); } else if (isPotentialVarArgsMethod(method, arguments)) { if (potentialMethodToInvoke == null) { potentialMethodToInvoke = method; throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", new Method[]{
Class<?> unmockedType = getUnmockedType(type); if ((unmockedType.isLocalClass() || unmockedType.isAnonymousClass() || unmockedType.isMemberClass()) && !Modifier.isStatic(unmockedType.getModifiers()) && arguments != null) { Constructor<?>[] constructors = filterPowerMockConstructor(unmockedType.getDeclaredConstructors()); Constructor<?> potentialConstructor = null; for (Constructor<?> constructor : constructors) { if (!checkArgumentTypesMatchParameterTypes(constructor.isVarArgs(), paramTypes, arguments)) { wrappedConstructorFound = false; if (!checkIfParameterTypesAreSame(constructor.isVarArgs(), convertArgumentTypesToPrimitive(paramTypes, arguments), paramTypes)) { primitiveConstructorFound = false; throwExceptionWhenMultipleConstructorMatchesFound(new Constructor<?>[] { potentialConstructor, constructor }); } else if (isPotentialVarArgsConstructor(constructor, arguments)) { if (potentialConstructor == null) { potentialConstructor = constructor; throwExceptionWhenMultipleConstructorMatchesFound(new Constructor<?>[] { potentialConstructor, constructor }); WhiteboxImpl.throwExceptionIfConstructorWasNotFound(type, potentialConstructor, arguments); return potentialConstructor;
final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getUnmockedType(type); if (!isNiceMock) { if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); newInvocationControl = new NewInvocationControlImpl<T>(mock, type); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));
/** * Returns a constructor specified in declaringClass. * * @param declaringClass * The declaringClass of the class where the constructor is * located. * @param parameterTypes * All parameter types of the constructor (may be * {@code null}). * @return A {@code java.lang.reflect.Constructor}. * @throws ConstructorNotFoundException * if the constructor cannot be found. */ @SuppressWarnings("unchecked") public static <T> Constructor<T> constructor(Class<T> declaringClass, Class<?>... parameterTypes) { return (Constructor<T>) WhiteboxImpl.findUniqueConstructorOrThrowException(declaringClass, (Object[]) parameterTypes); }
methodsToTraverse = getAllPublicMethods(thisType); } else { methodsToTraverse = thisType.getDeclaredMethods(); if (checkIfParameterTypesAreSame(method.isVarArgs(), parameterTypes, method.getParameterTypes())) { foundMethods.add(method); if (foundMethods.size() == 1) { + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getOriginalUnmockedType(type).getName() + "."); } else { throwExceptionWhenMultipleMethodMatchesFound("method name", foundMethods.toArray(new Method[foundMethods.size()]));
/** * 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); }
boolean exactParameterTypeMatch) { List<Method> matchingArgumentTypes = new LinkedList<Method>(); Method[] methods = getMethods(clazz, methodName); for (Method method : methods) { final Class<?>[] parameterTypes = method.getParameterTypes(); if (checkIfParameterTypesAreSame(method.isVarArgs(), expectedTypes, parameterTypes) || (!exactParameterTypeMatch && checkIfParameterTypesAreSame(method.isVarArgs(), convertParameterTypesToPrimitive(expectedTypes), parameterTypes))) { matchingArgumentTypes.add(method); throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodName), getType(clazz)));
methodsToTraverse = getAllPublicMethods(thisType); } else { methodsToTraverse = thisType.getDeclaredMethods(); if (checkIfParameterTypesAreSame(method.isVarArgs(), parameterTypes, method.getParameterTypes())) { foundMethods.add(method); if (foundMethods.size() == 1) { + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getUnmockedType(type).getName() + "."); } else { throwExceptionWhenMultipleMethodMatchesFound("method name", foundMethods.toArray(new Method[foundMethods.size()]));
/** * Convenience method to get a (declared) constructor from a class type * without having to catch the checked exceptions otherwise required. These * exceptions are wrapped as runtime exceptions. The constructor is also set * to accessible. * * @param type * The type of the class where the constructor is located. * @param parameterTypes * All parameter types of the constructor (may be * {@code null}). * @return A {@code java.lang.reflect.Constructor}. * @throws ConstructorNotFoundException * if the constructor cannot be found. */ public static <T> Constructor<T> getConstructor(Class<?> type, Class<?>... parameterTypes) { return (Constructor<T>) WhiteboxImpl.getConstructor(type, parameterTypes); }
/** * Get the value of a field using reflection based on the fields type. This * method will traverse the super class hierarchy until the first field of * type <tt>fieldType</tt> is found. The value of this field will be * returned. * * @param object * the object to modify * @param fieldType * the type of the field */ public static <T> T getInternalState(Object object, Class<T> fieldType) { return WhiteboxImpl.getInternalState(object, fieldType); }
Method[] allMethods = null; if (clazz.isInterface()) { allMethods = getAllPublicMethods(clazz); } else { allMethods = getAllMethods(clazz); throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodNames), getType(clazz)));
methodsToTraverse = getAllPublicMethods(thisType); } else { methodsToTraverse = thisType.getDeclaredMethods(); && checkIfParameterTypesAreSame(method.isVarArgs(), parameterTypes, method.getParameterTypes())) { method.setAccessible(true); return method; throwExceptionIfMethodWasNotFound(type, methodName, null, new Object[]{parameterTypes}); return null;
for (Method method : getAllMethods(type)) { if (methodName == null || method.getName().equals(methodName)) { if (parameterTypes != null && parameterTypes.length > 0) { if (!checkIfParameterTypesAreSame(method.isVarArgs(), parameterTypes, paramTypes)) { continue; WhiteboxImpl.throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", matchingMethodsList.toArray(new Method[matchingMethodsList.size()])); WhiteboxImpl.throwExceptionWhenMultipleMethodMatchesFound("argument parameter types", matchingMethodsList.toArray(new Method[matchingMethodsList.size()]));
if (MockRepository.shouldSuppressConstructor(WhiteboxImpl.getConstructor(type, sig))) { return WhiteboxImpl.getFirstParentConstructor(type);
/** * Get an array of {@link Field}'s that matches the supplied list of field * names. Both instance and static fields are taken into account. * * @param clazz * The class that should contain the fields. * @param fieldNames * Names of the fields that will be returned. * @return An array of Field's. May be of length 0 but not . */ public static Field[] getFields(Class<?> clazz, String... fieldNames) { final List<Field> fields = new LinkedList<Field>(); for (Field field : getAllFields(clazz)) { for (String fieldName : fieldNames) { if (field.getName().equals(fieldName)) { fields.add(field); } } } final Field[] fieldArray = fields.toArray(new Field[fields.size()]); if (fieldArray.length == 0) { throw new FieldNotFoundException(String.format( "No fields matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(fieldNames), getType(clazz))); } return fieldArray; }