/** * 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); }
/** * 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</code>). * @return A <code>java.lang.reflect.Constructor</code>. * @throws ConstructorNotFoundException * if the constructor cannot be found. */ public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes) { return WhiteboxImpl.getConstructor(type, parameterTypes); }
@SuppressWarnings("UnusedDeclaration") public static Object constructorCall(Class<?> type, Object[] args, Class<?>[] sig) throws Throwable { final Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (MockRepository.shouldSuppressConstructor(constructor)) { return null; } return PROCEED; }
@Override public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { Object varArgs = args[args.length - 1]; final int varArgsLength = Array.getLength(varArgs); Object[] oldArgs = args; args = new Object[args.length + varArgsLength - 1]; System.arraycopy(oldArgs, 0, args, 0, oldArgs.length - 1); for (int i = oldArgs.length - 1, j=0; i < args.length; i++, j++) { args[i] = Array.get(varArgs, j); } } try { return substitute.performSubstitutionLogic(args); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
@Override public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { Object varArgs = args[args.length - 1]; final int varArgsLength = Array.getLength(varArgs); Object[] oldArgs = args; args = new Object[args.length + varArgsLength - 1]; System.arraycopy(oldArgs, 0, args, 0, oldArgs.length - 1); for (int i = oldArgs.length - 1, j=0; i < args.length; i++, j++) { args[i] = Array.get(varArgs, j); } } try { return substitute.performSubstitutionLogic(args); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
if (MockRepository.shouldSuppressConstructor(WhiteboxImpl.getConstructor(type, sig))) { return WhiteboxImpl.getFirstParentConstructor(type);
@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); }
public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { /* * Get the first argument because this contains the actual varargs * arguments. */ args = (Object[]) args[0]; } try { final MockType mockType = ((EasyMockMethodInvocationControl<?>) MockRepository.getInstanceMethodInvocationControl(substitute)) .getMockType(); Object result = substitute.performSubstitutionLogic(args); if (result == null) { if (mockType == MockType.NICE) { result = EasyMock.createNiceMock(subsitutionType); } else { throw new IllegalStateException("Must replay class " + type.getName() + " to get configured expectation."); } } return result; } catch (AssertionError e) { NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes);
@SuppressWarnings({"unchecked", "rawtypes"}) private <T> 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); } /* * Check if this type has been mocked before */ NewInvocationControl<OngoingStubbing<T>> newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>) MockRepository.getNewInstanceControl(unmockedType); if (newInvocationControl == null) { InvocationSubstitute<T> mock = getMockCreator().createMock(InvocationSubstitute.class, false, false, null, null, (Method[]) null); newInvocationControl = createNewInvocationControl(mock); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getOriginalUnmockedType(type)); } return newInvocationControl.expectSubstitutionLogic(arguments); }