added two way converter
This commit is contained in:
parent
50985d5aa9
commit
796a457d9f
|
|
@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @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.
|
* The value we tried to convert. Transient because we cannot guarantee that the value is Serializable.
|
||||||
|
|
|
||||||
|
|
@ -39,9 +39,7 @@ public interface ConversionService {
|
||||||
* @param targetType context about the target type to convert to
|
* @param targetType context about the target type to convert to
|
||||||
* @return the converted object, an instance of {@link TypeDescriptor#getType()}</code>, or <code>null</code> if a null source
|
* @return the converted object, an instance of {@link TypeDescriptor#getType()}</code>, or <code>null</code> if a null source
|
||||||
* was provided
|
* was provided
|
||||||
* @throws ConverterNotFoundException if no suitable conversion executor could be found to convert the
|
* @throws ConvertException if an exception occurred during the conversion process
|
||||||
* source to an instance of targetType
|
|
||||||
* @throws ConversionException if an exception occurred during the conversion process
|
|
||||||
*/
|
*/
|
||||||
public Object convert(Object source, TypeDescriptor targetType);
|
public Object convert(Object source, TypeDescriptor targetType);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ package org.springframework.core.convert;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public abstract class ConversionException extends RuntimeException {
|
public abstract class ConvertException extends RuntimeException {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new conversion exception.
|
* Creates a new conversion exception.
|
||||||
* @param message the exception message
|
* @param message the exception message
|
||||||
* @param cause the cause
|
* @param cause the cause
|
||||||
*/
|
*/
|
||||||
public ConversionException(String message, Throwable cause) {
|
public ConvertException(String message, Throwable cause) {
|
||||||
super(message, cause);
|
super(message, cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ public abstract class ConversionException extends RuntimeException {
|
||||||
* Creates a new conversion exception.
|
* Creates a new conversion exception.
|
||||||
* @param message the exception message
|
* @param message the exception message
|
||||||
*/
|
*/
|
||||||
public ConversionException(String message) {
|
public ConvertException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -16,11 +16,11 @@
|
||||||
package org.springframework.core.convert;
|
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
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class ConverterNotFoundException extends ConversionException {
|
public class ConverterNotFoundException extends ConvertException {
|
||||||
|
|
||||||
private Class<?> sourceType;
|
private Class<?> sourceType;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.converter;
|
package org.springframework.core.convert.converter;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionException;
|
import org.springframework.core.convert.ConvertException;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -33,19 +33,9 @@ public interface Converter<S, T> {
|
||||||
* @param source the source object to convert, which must be an instance of S
|
* @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
|
* @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
|
* @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
|
* conversion error context
|
||||||
*/
|
*/
|
||||||
public T convert(S source) throws Exception;
|
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +22,7 @@ import java.math.BigDecimal;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToBigDecimal implements Converter<String, BigDecimal> {
|
public class StringToBigDecimal implements TwoWayConverter<String, BigDecimal> {
|
||||||
|
|
||||||
public BigDecimal convert(String source) {
|
public BigDecimal convert(String source) {
|
||||||
return new BigDecimal(source);
|
return new BigDecimal(source);
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import java.math.BigInteger;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToBigInteger implements Converter<String, BigInteger> {
|
public class StringToBigInteger implements TwoWayConverter<String, BigInteger> {
|
||||||
|
|
||||||
public BigInteger convert(String source) {
|
public BigInteger convert(String source) {
|
||||||
return new BigInteger(source);
|
return new BigInteger(source);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import org.springframework.util.Assert;
|
||||||
* @see #StringToBoolean(String, String)
|
* @see #StringToBoolean(String, String)
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToBoolean implements Converter<String, Boolean> {
|
public class StringToBoolean implements TwoWayConverter<String, Boolean> {
|
||||||
|
|
||||||
private String trueString;
|
private String trueString;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToByte implements Converter<String, Byte> {
|
public class StringToByte implements TwoWayConverter<String, Byte> {
|
||||||
|
|
||||||
public Byte convert(String source) {
|
public Byte convert(String source) {
|
||||||
return Byte.valueOf(source);
|
return Byte.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToCharacter implements Converter<String, Character> {
|
public class StringToCharacter implements TwoWayConverter<String, Character> {
|
||||||
|
|
||||||
public Character convert(String source) {
|
public Character convert(String source) {
|
||||||
if (source.length() != 1) {
|
if (source.length() != 1) {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToDouble implements Converter<String, Double> {
|
public class StringToDouble implements TwoWayConverter<String, Double> {
|
||||||
|
|
||||||
public Double convert(String source) {
|
public Double convert(String source) {
|
||||||
return Double.valueOf(source);
|
return Double.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToFloat implements Converter<String, Float> {
|
public class StringToFloat implements TwoWayConverter<String, Float> {
|
||||||
|
|
||||||
public Float convert(String source) {
|
public Float convert(String source) {
|
||||||
return Float.valueOf(source);
|
return Float.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToInteger implements Converter<String, Integer> {
|
public class StringToInteger implements TwoWayConverter<String, Integer> {
|
||||||
|
|
||||||
public Integer convert(String source) {
|
public Integer convert(String source) {
|
||||||
return Integer.valueOf(source);
|
return Integer.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ import org.springframework.util.StringUtils;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToLocale implements Converter<String, Locale> {
|
public class StringToLocale implements TwoWayConverter<String, Locale> {
|
||||||
|
|
||||||
public Locale convert(String source) {
|
public Locale convert(String source) {
|
||||||
return StringUtils.parseLocaleString(source);
|
return StringUtils.parseLocaleString(source);
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToLong implements Converter<String, Long> {
|
public class StringToLong implements TwoWayConverter<String, Long> {
|
||||||
|
|
||||||
public Long convert(String source) {
|
public Long convert(String source) {
|
||||||
return Long.valueOf(source);
|
return Long.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public class StringToShort implements Converter<String, Short> {
|
public class StringToShort implements TwoWayConverter<String, Short> {
|
||||||
|
|
||||||
public Short convert(String source) {
|
public Short convert(String source) {
|
||||||
return Short.valueOf(source);
|
return Short.valueOf(source);
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.converter;
|
package org.springframework.core.convert.converter;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionException;
|
import org.springframework.core.convert.ConvertException;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -34,7 +34,7 @@ public interface SuperConverter<S, T> {
|
||||||
* @param targetClass the requested target class to convert to (RT), which must be equal to T or extend from T
|
* @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
|
* @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
|
* @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
|
* conversion error context
|
||||||
*/
|
*/
|
||||||
public <RT extends T> RT convert(S source, Class<RT> targetClass) throws Exception;
|
public <RT extends T> RT convert(S source, Class<RT> targetClass) throws Exception;
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.converter;
|
package org.springframework.core.convert.converter;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionException;
|
import org.springframework.core.convert.ConvertException;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -34,7 +34,7 @@ public interface SuperTwoWayConverter<S, T> extends SuperConverter<S, T> {
|
||||||
* @param sourceClass the requested source class to convert to, which must be equal to S or extend from S
|
* @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
|
* @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
|
* @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
|
* conversion error context
|
||||||
*/
|
*/
|
||||||
public <RS extends S> RS convertBack(T target, Class<RS> sourceClass) throws Exception;
|
public <RS extends S> RS convertBack(T target, Class<RS> sourceClass) throws Exception;
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
|
* <p>
|
||||||
|
* Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and
|
||||||
|
* accessed through a {@link ConversionService}.
|
||||||
|
* </p>
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
|
public interface TwoWayConverter<S, T> extends Converter<S, T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
@ -33,6 +33,7 @@ import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.convert.converter.ConverterInfo;
|
import org.springframework.core.convert.converter.ConverterInfo;
|
||||||
import org.springframework.core.convert.converter.SuperConverter;
|
import org.springframework.core.convert.converter.SuperConverter;
|
||||||
import org.springframework.core.convert.converter.SuperTwoWayConverter;
|
import org.springframework.core.convert.converter.SuperTwoWayConverter;
|
||||||
|
import org.springframework.core.convert.converter.TwoWayConverter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -89,9 +90,11 @@ public class GenericConversionService implements ConversionService {
|
||||||
// index forward
|
// index forward
|
||||||
Map sourceMap = getSourceMap(sourceType);
|
Map sourceMap = getSourceMap(sourceType);
|
||||||
sourceMap.put(targetType, converter);
|
sourceMap.put(targetType, converter);
|
||||||
// index reverse
|
if (converter instanceof TwoWayConverter) {
|
||||||
sourceMap = getSourceMap(targetType);
|
// index reverse
|
||||||
sourceMap.put(sourceType, new ReverseConverter(converter));
|
sourceMap = getSourceMap(targetType);
|
||||||
|
sourceMap.put(sourceType, new ReverseConverter((TwoWayConverter) converter));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -228,7 +231,7 @@ public class GenericConversionService implements ConversionService {
|
||||||
for (Type genericInterface : genericInterfaces) {
|
for (Type genericInterface : genericInterfaces) {
|
||||||
if (genericInterface instanceof ParameterizedType) {
|
if (genericInterface instanceof ParameterizedType) {
|
||||||
ParameterizedType pInterface = (ParameterizedType) genericInterface;
|
ParameterizedType pInterface = (ParameterizedType) genericInterface;
|
||||||
if (Converter.class.equals(pInterface.getRawType())
|
if (Converter.class.isAssignableFrom((Class) pInterface.getRawType())
|
||||||
|| SuperConverter.class.isAssignableFrom((Class) pInterface.getRawType())) {
|
|| SuperConverter.class.isAssignableFrom((Class) pInterface.getRawType())) {
|
||||||
Class s = getParameterClass(pInterface.getActualTypeArguments()[0], converter.getClass());
|
Class s = getParameterClass(pInterface.getActualTypeArguments()[0], converter.getClass());
|
||||||
Class t = getParameterClass(pInterface.getActualTypeArguments()[1], converter.getClass());
|
Class t = getParameterClass(pInterface.getActualTypeArguments()[1], converter.getClass());
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
package org.springframework.core.convert.service;
|
package org.springframework.core.convert.service;
|
||||||
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
import org.springframework.core.convert.converter.TwoWayConverter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A converter that reverses another converter.
|
* A converter that reverses another converter.
|
||||||
|
|
@ -24,17 +25,14 @@ import org.springframework.core.convert.converter.Converter;
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class ReverseConverter implements Converter {
|
class ReverseConverter implements Converter {
|
||||||
|
|
||||||
private Converter converter;
|
private TwoWayConverter converter;
|
||||||
|
|
||||||
public ReverseConverter(Converter converter) {
|
public ReverseConverter(TwoWayConverter converter) {
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convert(Object source) throws Exception {
|
public Object convert(Object source) throws Exception {
|
||||||
return converter.convertBack(source);
|
return converter.convertBack(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertBack(Object target) throws Exception {
|
|
||||||
throw new IllegalStateException("Should not be called");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.expression.spel.support;
|
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.ConverterNotFoundException;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
@ -55,7 +55,7 @@ public class StandardTypeConverter implements TypeConverter {
|
||||||
return conversionService.convert(value, typeDescriptor);
|
return conversionService.convert(value, typeDescriptor);
|
||||||
} catch (ConverterNotFoundException cenfe) {
|
} catch (ConverterNotFoundException cenfe) {
|
||||||
throw new SpelException(cenfe, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
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());
|
throw new SpelException(ce, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue