Browse Source

Added entity conveter null checking; updated from string converters to simply test str.length() as pre-processing check

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2494 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Keith Donald 16 years ago
parent
commit
5bbb38479d
  1. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java
  2. 5
      org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java
  3. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java
  4. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java
  5. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java
  6. 3
      org.springframework.core/src/main/java/org/springframework/util/ReflectionUtils.java
  7. 12
      org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java

@ -18,7 +18,6 @@ package org.springframework.core.convert.support; @@ -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; @@ -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 { @@ -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);

5
org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java

@ -20,7 +20,6 @@ import java.util.HashSet; @@ -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> { @@ -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)) {

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java

@ -27,7 +27,7 @@ import org.springframework.core.convert.converter.Converter; @@ -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) {

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java

@ -41,7 +41,7 @@ final class StringToEnumConverterFactory implements ConverterFactory<String, Enu @@ -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;
}

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java

@ -53,7 +53,7 @@ final class StringToNumberConverterFactory implements ConverterFactory<String, N @@ -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);

3
org.springframework.core/src/main/java/org/springframework/util/ReflectionUtils.java

@ -193,6 +193,9 @@ public abstract class ReflectionUtils { @@ -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);
}

12
org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java

@ -251,6 +251,12 @@ public class DefaultConversionTests { @@ -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 { @@ -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…
Cancel
Save