From 5bbb38479dd91598491a0ce31f28df4ef0a09674 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sun, 22 Nov 2009 15:40:41 +0000 Subject: [PATCH] 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 --- .../core/convert/support/EntityConverter.java | 8 +++++--- .../convert/support/StringToBooleanConverter.java | 5 ++--- .../convert/support/StringToCharacterConverter.java | 2 +- .../support/StringToEnumConverterFactory.java | 2 +- .../support/StringToNumberConverterFactory.java | 2 +- .../org/springframework/util/ReflectionUtils.java | 3 +++ .../core/convert/support/DefaultConversionTests.java | 12 ++++++++++++ 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java index 4b96d0cd7d0..c085eb3844b 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java @@ -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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java index ef91679d18b..36e3090f884 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java @@ -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 { } 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)) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java index 7e3525eab7b..3771d83f505 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java @@ -27,7 +27,7 @@ import org.springframework.core.convert.converter.Converter; final class StringToCharacterConverter implements Converter { public Character convert(String source) { - if ("".equals(source)) { + if (source.length() == 0) { return null; } if (source.length() > 1) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java index 181ba2430cf..b3d47d6a511 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java @@ -41,7 +41,7 @@ final class StringToEnumConverterFactory implements ConverterFactory