/** * Get all constructors in the supplied class(es). * * @param cls * The class whose constructors to get. * @param additionalClasses * Additional classes whose constructors to get. * @return All constructors declared in this class. */ public static Constructor<?>[] constructorsDeclaredIn(final Class<?> cls, final Class<?>... additionalClasses) { if (cls == null) { throw new IllegalArgumentException("You need to supply at least one class."); } Set<Constructor<?>> constructors = new HashSet<Constructor<?>>(); constructors.addAll(asList(WhiteboxImpl.getAllConstructors(cls))); for (Class<?> klass : additionalClasses) { constructors.addAll(asList(WhiteboxImpl.getAllConstructors(klass))); } return constructors.toArray(new Constructor[constructors.size()]); }
/** * Get all constructors in the supplied class(es). * * @param cls * The class whose constructors to get. * @param additionalClasses * Additional classes whose constructors to get. * @return All constructors declared in this class. */ public static Constructor<?>[] constructorsDeclaredIn(final Class<?> cls, final Class<?>... additionalClasses) { if (cls == null) { throw new IllegalArgumentException("You need to supply at least one class."); } Set<Constructor<?>> constructors = new HashSet<Constructor<?>>(); constructors.addAll(asList(WhiteboxImpl.getAllConstructors(cls))); for (Class<?> klass : additionalClasses) { constructors.addAll(asList(WhiteboxImpl.getAllConstructors(klass))); } return constructors.toArray(new Constructor[constructors.size()]); }
@Override public OngoingStubbing<T> withAnyArguments() throws Exception { if (mockType == null) { throw new IllegalArgumentException("Class to expected cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(mockType); final Constructor<?>[] allConstructors = WhiteboxImpl.getAllConstructors(unmockedType); final Constructor<?> constructor = allConstructors[0]; final Class<?>[] parameterTypes = constructor.getParameterTypes(); Object[] paramArgs = new Object[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { Class<?> paramType = parameterTypes[i]; paramArgs[i] = createParamArgMatcher(paramType); } Constructor<?>[] otherCtors = new Constructor<?>[allConstructors.length - 1]; System.arraycopy(allConstructors, 1, otherCtors, 0, allConstructors.length - 1); final OngoingStubbing<T> ongoingStubbing = createNewSubstituteMock(mockType, parameterTypes, paramArgs); return new DelegatingToConstructorsOngoingStubbing<T>(otherCtors, ongoingStubbing); }
@Override public OngoingStubbing<T> withAnyArguments() throws Exception { if (mockType == null) { throw new IllegalArgumentException("Class to expected cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(mockType); final Constructor<?>[] allConstructors = WhiteboxImpl.getAllConstructors(unmockedType); final Constructor<?> constructor = allConstructors[0]; final Class<?>[] parameterTypes = constructor.getParameterTypes(); Object[] paramArgs = new Object[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { Class<?> paramType = parameterTypes[i]; paramArgs[i] = createParamArgMatcher(paramType); } final OngoingStubbing<T> ongoingStubbing = createNewSubstituteMock(mockType, parameterTypes, paramArgs); Constructor<?>[] otherCtors = new Constructor<?>[allConstructors.length - 1]; System.arraycopy(allConstructors, 1, otherCtors, 0, allConstructors.length - 1); return new DelegatingToConstructorsOngoingStubbing<T>(otherCtors, ongoingStubbing); }