/** * Get the default value for a type. * * @param type * The type whose default value to get. * @return The default return type of <code>type</code>. */ public static Object getDefaultValue(Class<?> type) { return getDefaultValue(type.getName()); }
private String getReturnCode(CtClass returnType) { if (returnType.equals(CtClass.voidType)) { return "{}"; } return "{return " + TypeUtils.getDefaultValueAsString(returnType.getName()) + ";}"; }
/** * Get the default value for a type. * * @param type * The type whose default value to get. * @return The default return type of {@code type}. */ public static Object getDefaultValue(Class<?> type) { return getDefaultValue(type.getName()); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return TypeUtils.getDefaultValue(method.getReturnType()); } });
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return TypeUtils.getDefaultValue(method.getReturnType()); } });
@Override public void afterTestMethod(Object testInstance, Method method, Object[] arguments, TestMethodResult testResult) throws Exception { Set<Field> allFields = Whitebox.getAllInstanceFields(testInstance); for (Field field : allFields) { field.set(testInstance, TypeUtils.getDefaultValue(field.getType())); } } }
@SuppressWarnings("UnusedDeclaration") public static Object fieldCall(Object instanceOrClassContainingTheField, Class<?> classDefiningField, String fieldName, Class<?> fieldType) { if (MockRepository.shouldSuppressField(WhiteboxImpl.getField(classDefiningField, fieldName))) { return TypeUtils.getDefaultValue(fieldType); } return PROCEED; }
public static <T> T fillWithDefaultValues(T object) { if (object == null) { throw new IllegalArgumentException("object to fill cannot be null"); } Set<Field> allInstanceFields = Whitebox.getAllInstanceFields(object); for (Field field : allInstanceFields) { final Class<?> fieldType = field.getType(); Object defaultValue = TypeUtils.getDefaultValue(fieldType); if (defaultValue == null && fieldType != object.getClass() && !field.isSynthetic()) { defaultValue = instantiateFieldType(field); if (defaultValue != null) { fillWithDefaultValues(defaultValue); } } try { field.set(object, defaultValue); } catch (Exception e) { throw new RuntimeException("Internal error: Failed to set field.", e); } } return object; }
/** * {@link #noMockito} is wrapped into it's own static class to make sure it is initialized not earlier than * {@link #calledFromMockito()} is called for the first time. */ private static final class NoMockito { static final boolean noMockito = Package.getPackage("org.mockito") == null; private NoMockito() {} } public static final Object PROCEED = new Object();