refined generic converter concept

This commit is contained in:
Keith Donald
2009-09-18 19:57:59 +00:00
parent 6420fd303b
commit d3b43ebccb
14 changed files with 290 additions and 173 deletions

View File

@@ -38,26 +38,24 @@ import org.springframework.util.Assert;
*/
public class StandardTypeConverter implements TypeConverter {
private final ConversionService typeConverter;
private final ConversionService conversionService;
public StandardTypeConverter() {
this.typeConverter = new DefaultConversionService();
this.conversionService = new DefaultConversionService();
}
public StandardTypeConverter(ConversionService typeConverter) {
Assert.notNull(typeConverter, "ConversionService must not be null");
this.typeConverter = typeConverter;
this.conversionService = typeConverter;
}
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return this.typeConverter.canConvert(sourceType, targetType);
return this.conversionService.canConvert(sourceType, targetType);
}
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
try {
return this.typeConverter.convert(value, typeDescriptor);
return this.conversionService.convert(value, TypeDescriptor.forObject(value), typeDescriptor);
}
catch (ConverterNotFoundException cenfe) {
throw new SpelEvaluationException(cenfe, SpelMessage.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());

View File

@@ -101,21 +101,11 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
private final DefaultConversionService service = new DefaultConversionService();
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return this.service.canConvert(sourceType, TypeDescriptor.valueOf(targetType));
return this.service.canConvert(sourceType, targetType);
}
public boolean canConvert(Class<?> sourceType, TypeDescriptor typeDescriptor) {
return this.service.canConvert(sourceType, typeDescriptor);
}
@SuppressWarnings("cast")
public <T> T convertValue(Object value, Class<T> targetType) throws EvaluationException {
return (T) this.service.convert(value,TypeDescriptor.valueOf(targetType));
}
@SuppressWarnings("unchecked")
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
return this.service.convert(value, typeDescriptor);
return this.service.convert(value, TypeDescriptor.forObject(value), typeDescriptor);
}
}