/** * Set the value in the field with the specified name in the specified object. Supports * inheritance. * * @param object The object to set the value in. * @param fieldName The name of the field. * @param value The value to set in the field. */ public static void setFieldValue( final Object object, final String fieldName, final Object value) { Validate.notNull(object, "The object to set the value in can not be null"); Validate.notEmpty(fieldName, "The name of the field can not be empty"); final Field field = getField(object, fieldName); setValue(object, value, field); }
/** * Gets the value of the field with the specified name in the specified object. Supports * inheritance. * * @param object The object to get the value from. * @param fieldClass The class of the field. * @param fieldName The name of the field. * @param <T> The class of the field. * @return The value in the field of the object. */ public static <T> T getFieldValue( final Object object, final Class<T> fieldClass, final String fieldName) { Validate.notNull(object, "The object to get the value from can not be null"); Validate.notNull(fieldClass, "The class of the field can not be null"); Validate.notEmpty(fieldName, "The name of the field can not be empty"); final Field field = getField(object, fieldName); return getValue(object, fieldClass, field); }
/** * Creates a mock from the specified class and sets it as the value in the field with the * specified name in the specified object. Supports inheritance. * * @param object The object to set the value in. * @param fieldName The name of the field. * @param classToMock The class to create a mock from, as the value to set in the field. */ public static <T> T setFieldValueWithMock( final Object object, final String fieldName, final Class<T> classToMock) { Validate.notNull(classToMock, "The class to mock can not be null"); final T mock = mock(classToMock); setFieldValue(object, fieldName, mock); return mock; }