activated DefaultConversionService in EL, linking convert and EL
This commit is contained in:
@@ -16,14 +16,14 @@
|
||||
|
||||
package org.springframework.expression.spel.support;
|
||||
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.core.convert.ConversionExecutorNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.service.DefaultConversionService;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.TypeConverter;
|
||||
import org.springframework.expression.spel.SpelException;
|
||||
import org.springframework.expression.spel.SpelMessages;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.NumberUtils;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
@@ -40,84 +40,25 @@ public class StandardTypeConverter implements TypeConverter {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T convertValue(Object value, Class<T> targetType) throws EvaluationException {
|
||||
// For activation when conversion service available - this replaces the rest of the method (probably...)
|
||||
// return (T)convertValue(value,TypeDescriptor.valueOf(targetType));
|
||||
if (ClassUtils.isAssignableValue(targetType, value)) {
|
||||
return (T) value;
|
||||
}
|
||||
if (String.class.equals(targetType)) {
|
||||
return (T) (value != null ? value.toString() : null);
|
||||
}
|
||||
Class actualTargetType = ClassUtils.resolvePrimitiveIfNecessary(targetType);
|
||||
if (Number.class.isAssignableFrom(actualTargetType)) {
|
||||
try {
|
||||
if (value instanceof String) {
|
||||
return (T) NumberUtils.parseNumber(value.toString(), (Class<Number>) actualTargetType);
|
||||
}
|
||||
else if (value instanceof Number) {
|
||||
return (T) NumberUtils.convertNumberToTargetClass((Number) value, (Class<Number>) actualTargetType);
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new SpelException(SpelMessages.PROBLEM_DURING_TYPE_CONVERSION, ex.getMessage());
|
||||
}
|
||||
}
|
||||
if (Character.class.equals(actualTargetType)) {
|
||||
if (value instanceof String) {
|
||||
String str = (String) value;
|
||||
if (str.length() == 1) {
|
||||
return (T) new Character(str.charAt(0));
|
||||
}
|
||||
}
|
||||
else if (value instanceof Number) {
|
||||
return (T) new Character((char) ((Number) value).shortValue());
|
||||
}
|
||||
}
|
||||
if (Boolean.class.equals(actualTargetType) && value instanceof String) {
|
||||
String str = (String) value;
|
||||
if ("true".equalsIgnoreCase(str)) {
|
||||
return (T) Boolean.TRUE;
|
||||
}
|
||||
else if ("false".equalsIgnoreCase(str)) {
|
||||
return (T) Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
throw new SpelException(SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), targetType);
|
||||
return (T) convertValue(value,TypeDescriptor.valueOf(targetType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
|
||||
// For activation when conversion service available - this replaces the rest of the method (probably...)
|
||||
// try {
|
||||
// return conversionService.executeConversion(value, typeDescriptor);
|
||||
// } catch (ConversionExecutorNotFoundException cenfe) {
|
||||
// throw new SpelException(cenfe, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
||||
// } catch (ConversionException ce) {
|
||||
// throw new SpelException(ce, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
||||
// }
|
||||
return convertValue(value,typeDescriptor.getType());
|
||||
try {
|
||||
return conversionService.executeConversion(value, typeDescriptor);
|
||||
} catch (ConversionExecutorNotFoundException cenfe) {
|
||||
throw new SpelException(cenfe, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
||||
} catch (ConversionException ce) {
|
||||
throw new SpelException(ce, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
||||
// For activation when conversion service available - this replaces the rest of the method (probably...)
|
||||
// return canConvert(sourceType,TypeDescriptor.valueOf(targetType));
|
||||
if (ClassUtils.isAssignable(targetType, sourceType) || String.class.equals(targetType)) {
|
||||
return true;
|
||||
}
|
||||
Class actualTargetType = ClassUtils.resolvePrimitiveIfNecessary(targetType);
|
||||
return (((Number.class.isAssignableFrom(actualTargetType) || Character.class.equals(actualTargetType)) &&
|
||||
(String.class.equals(sourceType) || Number.class.isAssignableFrom(sourceType))) ||
|
||||
(Boolean.class.equals(actualTargetType) && String.class.equals(sourceType)));
|
||||
return canConvert(sourceType, TypeDescriptor.valueOf(targetType));
|
||||
}
|
||||
|
||||
public boolean canConvert(Class<?> sourceType, TypeDescriptor typeDescriptor) {
|
||||
// For activation when conversion service available - this replaces the rest of the method (probably...)
|
||||
// try {
|
||||
// return conversionService.getConversionExecutor(sourceType, typeDescriptor)!=null;
|
||||
// } catch (ConversionExecutorNotFoundException cenfe) {
|
||||
// return false;
|
||||
// }
|
||||
return canConvert(sourceType,typeDescriptor.getType());
|
||||
public boolean canConvert(Class<?> sourceType, TypeDescriptor targetType) {
|
||||
return conversionService.canConvert(sourceType, targetType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class LiteralTests extends ExpressionTestCase {
|
||||
// ask for the result to be made into an Integer
|
||||
evaluateAndAskForReturnType("0x20 * 2L", 64, Integer.class);
|
||||
// ask for the result to be made into an Integer knowing that it will not fit
|
||||
evaluateAndCheckError("0x1220 * 0xffffffffL", Integer.class, SpelMessages.PROBLEM_DURING_TYPE_CONVERSION, -1);
|
||||
evaluateAndCheckError("0x1220 * 0xffffffffL", Integer.class, SpelMessages.TYPE_CONVERSION_ERROR, -1);
|
||||
}
|
||||
|
||||
public void testSignedIntLiterals() {
|
||||
|
||||
Reference in New Issue
Block a user