Added entity conveter null checking; updated from string converters to simply test str.length() as pre-processing check
This commit is contained in:
parent
48e1ed0a24
commit
064b6db666
|
|
@ -18,7 +18,6 @@ package org.springframework.core.convert.support;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
|
@ -34,9 +33,9 @@ import org.springframework.util.ReflectionUtils;
|
|||
*/
|
||||
final class EntityConverter implements ConditionalGenericConverter {
|
||||
|
||||
private ConversionService conversionService;
|
||||
private GenericConversionService conversionService;
|
||||
|
||||
public EntityConverter(ConversionService conversionService) {
|
||||
public EntityConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -56,6 +55,9 @@ final class EntityConverter implements ConditionalGenericConverter {
|
|||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
if (String.class.equals(targetType.getType())) {
|
||||
Method idAccessor = getIdAccessor(sourceType.getType());
|
||||
Object id = ReflectionUtils.invokeMethod(idAccessor, source);
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Converts String to a Boolean.
|
||||
|
|
@ -50,8 +49,8 @@ final class StringToBooleanConverter implements Converter<String, Boolean> {
|
|||
}
|
||||
|
||||
public Boolean convert(String source) {
|
||||
String value = (source != null ? source.trim() : null);
|
||||
if (!StringUtils.hasLength(value)) {
|
||||
String value = source.trim();
|
||||
if (value.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
else if (trueValues.contains(value)) {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import org.springframework.core.convert.converter.Converter;
|
|||
final class StringToCharacterConverter implements Converter<String, Character> {
|
||||
|
||||
public Character convert(String source) {
|
||||
if ("".equals(source)) {
|
||||
if (source.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (source.length() > 1) {
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ final class StringToEnumConverterFactory implements ConverterFactory<String, Enu
|
|||
}
|
||||
|
||||
public T convert(String source) {
|
||||
if ("".equals(source)) {
|
||||
if (source.length() == 0) {
|
||||
// It's an empty enum identifier: reset the enum value to null.
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ final class StringToNumberConverterFactory implements ConverterFactory<String, N
|
|||
}
|
||||
|
||||
public T convert(String source) {
|
||||
if ("".equals(source)) {
|
||||
if (source.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
return NumberUtils.parseNumber(source, this.targetType);
|
||||
|
|
|
|||
|
|
@ -193,6 +193,9 @@ public abstract class ReflectionUtils {
|
|||
* @return the invocation result, if any
|
||||
*/
|
||||
public static Object invokeMethod(Method method, Object target, Object... args) {
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException("target cannot be null");
|
||||
}
|
||||
try {
|
||||
return method.invoke(target, args);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -251,6 +251,12 @@ public class DefaultConversionTests {
|
|||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithNull() {
|
||||
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(TestEntity.class));
|
||||
assertNull(e);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithIdConversion() {
|
||||
TestEntity e = conversionService.convert("1", TestEntity.class);
|
||||
|
|
@ -263,6 +269,12 @@ public class DefaultConversionTests {
|
|||
assertEquals("1", id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToObjectToStringIdPropertyWithNull() {
|
||||
String id = (String) conversionService.convert(null, TypeDescriptor.valueOf(TestEntity.class), TypeDescriptor.valueOf(String.class));
|
||||
assertNull(id);
|
||||
}
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
|
|
|||
Loading…
Reference in New Issue