提交 796a457d 编写于 作者: K Keith Donald

added two way converter

上级 50985d5a
......@@ -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.
......
......@@ -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()}</code>, or <code>null</code> 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);
......
......@@ -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);
}
}
......@@ -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;
......
......@@ -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<S, T> {
* @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
......@@ -22,7 +22,7 @@ import java.math.BigDecimal;
*
* @author Keith Donald
*/
public class StringToBigDecimal implements Converter<String, BigDecimal> {
public class StringToBigDecimal implements TwoWayConverter<String, BigDecimal> {
public BigDecimal convert(String source) {
return new BigDecimal(source);
......
......@@ -22,7 +22,7 @@ import java.math.BigInteger;
*
* @author Keith Donald
*/
public class StringToBigInteger implements Converter<String, BigInteger> {
public class StringToBigInteger implements TwoWayConverter<String, BigInteger> {
public BigInteger convert(String source) {
return new BigInteger(source);
......
......@@ -23,7 +23,7 @@ import org.springframework.util.Assert;
* @see #StringToBoolean(String, String)
* @author Keith Donald
*/
public class StringToBoolean implements Converter<String, Boolean> {
public class StringToBoolean implements TwoWayConverter<String, Boolean> {
private String trueString;
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToByte implements Converter<String, Byte> {
public class StringToByte implements TwoWayConverter<String, Byte> {
public Byte convert(String source) {
return Byte.valueOf(source);
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToCharacter implements Converter<String, Character> {
public class StringToCharacter implements TwoWayConverter<String, Character> {
public Character convert(String source) {
if (source.length() != 1) {
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToDouble implements Converter<String, Double> {
public class StringToDouble implements TwoWayConverter<String, Double> {
public Double convert(String source) {
return Double.valueOf(source);
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToFloat implements Converter<String, Float> {
public class StringToFloat implements TwoWayConverter<String, Float> {
public Float convert(String source) {
return Float.valueOf(source);
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToInteger implements Converter<String, Integer> {
public class StringToInteger implements TwoWayConverter<String, Integer> {
public Integer convert(String source) {
return Integer.valueOf(source);
......
......@@ -24,7 +24,7 @@ import org.springframework.util.StringUtils;
*
* @author Keith Donald
*/
public class StringToLocale implements Converter<String, Locale> {
public class StringToLocale implements TwoWayConverter<String, Locale> {
public Locale convert(String source) {
return StringUtils.parseLocaleString(source);
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToLong implements Converter<String, Long> {
public class StringToLong implements TwoWayConverter<String, Long> {
public Long convert(String source) {
return Long.valueOf(source);
......
......@@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
*
* @author Keith Donald
*/
public class StringToShort implements Converter<String, Short> {
public class StringToShort implements TwoWayConverter<String, Short> {
public Short convert(String source) {
return Short.valueOf(source);
......
......@@ -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<S, 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
* @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 extends T> RT convert(S source, Class<RT> targetClass) throws Exception;
......
......@@ -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<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
* @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 extends S> RS convertBack(T target, Class<RS> sourceClass) throws Exception;
......
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.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());
......
......@@ -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");
}
}
......@@ -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());
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册