@Test public void whenAttemptingToSetANullValue_ThenShouldNotCallTheSetter() throws Exception { ObjectMapper<Person> mapper = new ObjectMapper<>(Person.class); Map<String, String> values = new HashMap<>(); values.put("age", null); Person person = mapper.mapObject(values); assertThat(person).isNotNull(); assertThat(person.getAge()).isEqualTo(0); }
public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; }
ObjectMapper { public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; } }
ObjectMapper { public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; } ObjectMapper(final Class<T> objectType); }
ObjectMapper { public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; } ObjectMapper(final Class<T> objectType); T mapObject(final Map<String, String> values); void registerTypeConverter(final TypeConverter<String, ?> typeConverter); }
ObjectMapper { public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; } ObjectMapper(final Class<T> objectType); T mapObject(final Map<String, String> values); void registerTypeConverter(final TypeConverter<String, ?> typeConverter);  }