From 796a457d9fdf396c6fe242dc3cc7e88841b2d6a6 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 11 May 2009 21:40:55 +0000 Subject: [PATCH] added two way converter --- .../convert/ConversionExecutionException.java | 2 +- .../core/convert/ConversionService.java | 4 +-- ...onException.java => ConvertException.java} | 6 ++--- .../convert/ConverterNotFoundException.java | 4 +-- .../core/convert/converter/Converter.java | 14 ++--------- .../convert/converter/StringToBigDecimal.java | 2 +- .../convert/converter/StringToBigInteger.java | 2 +- .../convert/converter/StringToBoolean.java | 2 +- .../core/convert/converter/StringToByte.java | 2 +- .../convert/converter/StringToCharacter.java | 2 +- .../convert/converter/StringToDouble.java | 2 +- .../core/convert/converter/StringToFloat.java | 2 +- .../convert/converter/StringToInteger.java | 2 +- .../convert/converter/StringToLocale.java | 2 +- .../core/convert/converter/StringToLong.java | 2 +- .../core/convert/converter/StringToShort.java | 2 +- .../convert/converter/SuperConverter.java | 4 +-- .../converter/SuperTwoWayConverter.java | 4 +-- .../convert/converter/TwoWayConverter.java | 25 +++++++++++++++++++ .../service/GenericConversionService.java | 11 +++++--- .../convert/service/ReverseConverter.java | 10 +++----- .../spel/support/StandardTypeConverter.java | 4 +-- 22 files changed, 62 insertions(+), 48 deletions(-) rename org.springframework.core/src/main/java/org/springframework/core/convert/{ConversionException.java => ConvertException.java} (85%) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/TwoWayConverter.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java index addc27e7ad..466b53d700 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java @@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils; * * @author Keith Donald */ -public class ConversionExecutionException extends ConversionException { +public class ConversionExecutionException extends ConvertException { /** * The value we tried to convert. Transient because we cannot guarantee that the value is Serializable. diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java index 16a3c58c6c..6567c163bd 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java @@ -39,9 +39,7 @@ public interface ConversionService { * @param targetType context about the target type to convert to * @return the converted object, an instance of {@link TypeDescriptor#getType()}, or null if a null source * was provided - * @throws ConverterNotFoundException if no suitable conversion executor could be found to convert the - * source to an instance of targetType - * @throws ConversionException if an exception occurred during the conversion process + * @throws ConvertException if an exception occurred during the conversion process */ public Object convert(Object source, TypeDescriptor targetType); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConvertException.java similarity index 85% rename from org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/ConvertException.java index 5baa0079da..f1bd218814 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConvertException.java @@ -20,14 +20,14 @@ package org.springframework.core.convert; * * @author Keith Donald */ -public abstract class ConversionException extends RuntimeException { +public abstract class ConvertException extends RuntimeException { /** * Creates a new conversion exception. * @param message the exception message * @param cause the cause */ - public ConversionException(String message, Throwable cause) { + public ConvertException(String message, Throwable cause) { super(message, cause); } @@ -35,7 +35,7 @@ public abstract class ConversionException extends RuntimeException { * Creates a new conversion exception. * @param message the exception message */ - public ConversionException(String message) { + public ConvertException(String message) { super(message); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java index 8856a84101..a7a21e549c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java @@ -16,11 +16,11 @@ package org.springframework.core.convert; /** - * Thrown when a conversion executor could not be found in a conversion service. + * Thrown when a suitable converter could not be found in a conversion service. * * @author Keith Donald */ -public class ConverterNotFoundException extends ConversionException { +public class ConverterNotFoundException extends ConvertException { private Class sourceType; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java index 0ace319957..b751b750be 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java @@ -15,7 +15,7 @@ */ package org.springframework.core.convert.converter; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConvertException; import org.springframework.core.convert.ConversionService; /** @@ -33,19 +33,9 @@ public interface Converter { * @param source the source object to convert, which must be an instance of S * @return the converted object, which must be an instance of T * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion - * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type * conversion error context */ public T convert(S source) throws Exception; - /** - * Convert the target of type T back to source type S. - * @param target the target object to convert, which must be an instance of T - * @return the converted object, which must be an instance of S - * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion - * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type - * conversion error context - */ - public S convertBack(T target) throws Exception; - } \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java index ffeeef0f65..68d81aad77 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java @@ -22,7 +22,7 @@ import java.math.BigDecimal; * * @author Keith Donald */ -public class StringToBigDecimal implements Converter { +public class StringToBigDecimal implements TwoWayConverter { public BigDecimal convert(String source) { return new BigDecimal(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java index 6bf4b464a9..fabb6d0648 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java @@ -22,7 +22,7 @@ import java.math.BigInteger; * * @author Keith Donald */ -public class StringToBigInteger implements Converter { +public class StringToBigInteger implements TwoWayConverter { public BigInteger convert(String source) { return new BigInteger(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java index 4a6955377b..44109cd0a7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java @@ -23,7 +23,7 @@ import org.springframework.util.Assert; * @see #StringToBoolean(String, String) * @author Keith Donald */ -public class StringToBoolean implements Converter { +public class StringToBoolean implements TwoWayConverter { private String trueString; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java index 043b24bb08..ca91c077b3 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToByte implements Converter { +public class StringToByte implements TwoWayConverter { public Byte convert(String source) { return Byte.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java index 18ffae6de2..d15aa1e02e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToCharacter implements Converter { +public class StringToCharacter implements TwoWayConverter { public Character convert(String source) { if (source.length() != 1) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java index 819ee88458..7e816f4149 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToDouble implements Converter { +public class StringToDouble implements TwoWayConverter { public Double convert(String source) { return Double.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java index 0972b4ef1f..a513ff4320 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToFloat implements Converter { +public class StringToFloat implements TwoWayConverter { public Float convert(String source) { return Float.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java index 55f08959b3..db6c44aa79 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToInteger implements Converter { +public class StringToInteger implements TwoWayConverter { public Integer convert(String source) { return Integer.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java index 982f21dbc4..a9bf186818 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java @@ -24,7 +24,7 @@ import org.springframework.util.StringUtils; * * @author Keith Donald */ -public class StringToLocale implements Converter { +public class StringToLocale implements TwoWayConverter { public Locale convert(String source) { return StringUtils.parseLocaleString(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java index fb43dfc98c..9a31064289 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToLong implements Converter { +public class StringToLong implements TwoWayConverter { public Long convert(String source) { return Long.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java index f6a669157c..949fa553a1 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java @@ -20,7 +20,7 @@ package org.springframework.core.convert.converter; * * @author Keith Donald */ -public class StringToShort implements Converter { +public class StringToShort implements TwoWayConverter { public Short convert(String source) { return Short.valueOf(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java index 4ee7275c2b..dc649979d5 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java @@ -15,7 +15,7 @@ */ package org.springframework.core.convert.converter; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConvertException; import org.springframework.core.convert.ConversionService; /** @@ -34,7 +34,7 @@ public interface SuperConverter { * @param targetClass the requested target class to convert to (RT), which must be equal to T or extend from T * @return the converted object, which must be an instance of RT * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion - * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type * conversion error context */ public RT convert(S source, Class targetClass) throws Exception; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperTwoWayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperTwoWayConverter.java index c7f2f6ab7d..a2254b98e6 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperTwoWayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperTwoWayConverter.java @@ -15,7 +15,7 @@ */ package org.springframework.core.convert.converter; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConvertException; import org.springframework.core.convert.ConversionService; /** @@ -34,7 +34,7 @@ public interface SuperTwoWayConverter extends SuperConverter { * @param sourceClass the requested source class to convert to, which must be equal to S or extend from S * @return the converted object, which must be an instance of RS * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion - * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type * conversion error context */ public RS convertBack(T target, Class sourceClass) throws Exception; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/TwoWayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/TwoWayConverter.java new file mode 100644 index 0000000000..f31ba5e1d2 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/TwoWayConverter.java @@ -0,0 +1,25 @@ +package org.springframework.core.convert.converter; + +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.ConvertException; + +/** + * A converter that can also convert a target object of type T to a source of class S. + *

+ * Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and + * accessed through a {@link ConversionService}. + *

+ * @author Keith Donald + */ +public interface TwoWayConverter extends Converter { + + /** + * Convert the target of type T back to source type S. + * @param target the target object to convert, which must be an instance of T + * @return the converted object, which must be an instance of S + * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion + * system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type + * conversion error context + */ + public S convertBack(T target) throws Exception; +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java index 4adf86d348..d9f35da6d3 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java @@ -33,6 +33,7 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.ConverterInfo; import org.springframework.core.convert.converter.SuperConverter; import org.springframework.core.convert.converter.SuperTwoWayConverter; +import org.springframework.core.convert.converter.TwoWayConverter; import org.springframework.util.Assert; /** @@ -89,9 +90,11 @@ public class GenericConversionService implements ConversionService { // index forward Map sourceMap = getSourceMap(sourceType); sourceMap.put(targetType, converter); - // index reverse - sourceMap = getSourceMap(targetType); - sourceMap.put(sourceType, new ReverseConverter(converter)); + if (converter instanceof TwoWayConverter) { + // index reverse + sourceMap = getSourceMap(targetType); + sourceMap.put(sourceType, new ReverseConverter((TwoWayConverter) converter)); + } } /** @@ -228,7 +231,7 @@ public class GenericConversionService implements ConversionService { for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType pInterface = (ParameterizedType) genericInterface; - if (Converter.class.equals(pInterface.getRawType()) + if (Converter.class.isAssignableFrom((Class) pInterface.getRawType()) || SuperConverter.class.isAssignableFrom((Class) pInterface.getRawType())) { Class s = getParameterClass(pInterface.getActualTypeArguments()[0], converter.getClass()); Class t = getParameterClass(pInterface.getActualTypeArguments()[1], converter.getClass()); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/service/ReverseConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/service/ReverseConverter.java index eb85ac1aed..d35559cd91 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/service/ReverseConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/service/ReverseConverter.java @@ -16,6 +16,7 @@ package org.springframework.core.convert.service; import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.TwoWayConverter; /** * A converter that reverses another converter. @@ -24,17 +25,14 @@ import org.springframework.core.convert.converter.Converter; @SuppressWarnings("unchecked") class ReverseConverter implements Converter { - private Converter converter; + private TwoWayConverter converter; - public ReverseConverter(Converter converter) { + public ReverseConverter(TwoWayConverter converter) { this.converter = converter; } public Object convert(Object source) throws Exception { return converter.convertBack(source); } - - public Object convertBack(Object target) throws Exception { - throw new IllegalStateException("Should not be called"); - } + } diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java index 0c9f36dfa9..50cf3c432a 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java @@ -16,7 +16,7 @@ package org.springframework.expression.spel.support; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConvertException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; @@ -55,7 +55,7 @@ public class StandardTypeConverter implements TypeConverter { return conversionService.convert(value, typeDescriptor); } catch (ConverterNotFoundException cenfe) { throw new SpelException(cenfe, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString()); - } catch (ConversionException ce) { + } catch (ConvertException ce) { throw new SpelException(ce, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString()); } }