/** * Throw exception if field was not found. * * @param type * the type * @param fieldName * the field name * @param field * the field */ public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field) { if (field == null) { throw new FieldNotFoundException("No field was found with name '" + fieldName + "' in class " + getUnmockedType(type).getName() + "."); } }
/** * Get the first parent constructor defined in a super class of * <code>klass</code>. * * @param klass * The class where the constructor is located. <code>null</code> * ). * @return A . */ public static Constructor<?> getFirstParentConstructor(Class<?> klass) { try { return getUnmockedType(klass).getSuperclass().getDeclaredConstructors()[0]; } catch (Exception e) { throw new ConstructorNotFoundException("Failed to lookup constructor.", e); } }
/** * Gets the type. * * @param object * the object * @return The type of the of an object. */ public static Class<?> getType(Object object) { Class<?> type = null; if (isClass(object)) { type = (Class<?>) object; } else if (object != null) { type = object.getClass(); } return getUnmockedType(type); }
/** * Throw exception if constructor was not found. * * @param type * the type * @param potentialConstructor * the potential constructor * @param arguments * the arguments */ static void throwExceptionIfConstructorWasNotFound(Class<?> type, Constructor<?> potentialConstructor, Object... arguments) { if (potentialConstructor == null) { String message = "No constructor found in class '" + getUnmockedType(type).getName() + "' with " + "parameter types: [ " + getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } }
if (args != null && args.length == argumentTypes.length) { for (int i = 0; i < args.length; i++) { if (args[i].isAssignableFrom(getUnmockedType(argumentTypes[i]))) {
/** * 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 . */ public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes) { Class<?> unmockedType = WhiteboxImpl.getUnmockedType(type); try { final Constructor<?> constructor = unmockedType.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (RuntimeException e) { throw (RuntimeException) e; } catch (Error e) { throw (Error) e; } catch (Throwable e) { throw new ConstructorNotFoundException(String.format( "Failed to lookup constructor with parameter types [ %s ] in class %s.", getArgumentTypesAsString((Object[]) parameterTypes), unmockedType.getName()), e); } }
/** * Throw exception if method was not found. * * @param type * the type * @param methodName * the method name * @param methodToMock * the method to mock * @param arguments * the arguments */ public static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock, Object... arguments) { if (methodToMock == null) { String methodNameData = ""; if (methodName != null) { methodNameData = "with name '" + methodName + "' "; } throw new MethodNotFoundException("No method found " + methodNameData + "with parameter types: [ " + getArgumentTypesAsString(arguments) + " ] in class " + getUnmockedType(type).getName() + "."); } }
throw new MethodNotFoundException("No method was found with parameter types: [ " + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getUnmockedType(type).getName() + "."); } else { throwExceptionWhenMultipleMethodMatchesFound("method name",
final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getUnmockedType(type); if (!isNiceMock) { if (parameterTypes == null) { newInvocationControl = new NewInvocationControlImpl<T>(mock, type); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));
Class<?> unmockedType = getUnmockedType(type); if ((unmockedType.isLocalClass() || unmockedType.isAnonymousClass() || unmockedType.isMemberClass()) && !Modifier.isStatic(unmockedType.getModifiers()) && arguments != null) {