TypeDescriptor.valueOf usage in favor of constants; TypedValue usage simplification

This commit is contained in:
Keith Donald 2009-12-15 19:41:52 +00:00
parent f37d7082a2
commit 2fef141a00
31 changed files with 120 additions and 138 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.context.expression;
import java.util.Map;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
@ -39,7 +38,7 @@ public class MapAccessor implements PropertyAccessor {
}
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
return new TypedValue(((Map) target).get(name));
}
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
@ -52,7 +51,7 @@ public class MapAccessor implements PropertyAccessor {
}
public Class[] getSpecificTargetClasses() {
return new Class[] {Map.class};
return new Class[] { Map.class };
}
}

View File

@ -115,10 +115,11 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
}
}
if (this.conversionService != null) {
// Try custom formatter...
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, TypeDescriptor.STRING)) {
return this.conversionService.convert(value, fieldDesc, TypeDescriptor.STRING);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
@ -152,7 +153,7 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
TypeDescriptor td = (field != null ?
getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) :
TypeDescriptor.valueOf(valueType));
if (this.conversionService.canConvert(TypeDescriptor.STRING, td)) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
}
}

View File

@ -95,9 +95,9 @@ public class FormattingConversionServiceTests {
});
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.STRING);
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class));
assertEquals("10/31/09", formatted);
LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.STRING,
LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(Model.class.getField("date"))));
assertEquals(new LocalDate(2009, 10, 31), date);
}
@ -105,34 +105,34 @@ public class FormattingConversionServiceTests {
@Test
public void testPrintNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class)));
}
@Test
public void testParseNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
@Test
public void testParseEmptyString() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
@Test
public void testPrintNullDefault() throws ParseException {
assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class)));
}
@Test
public void testParseNullDefault() throws ParseException {
assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
@Test
public void testParseEmptyStringDefault() throws ParseException {
assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
private static class Model {

View File

@ -19,6 +19,7 @@ package org.springframework.core.convert;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.GenericCollectionTypeResolver;
@ -39,13 +40,20 @@ public class TypeDescriptor {
/** Constant defining an 'unknown' TypeDescriptor */
public static final TypeDescriptor NULL = new TypeDescriptor();
/** Constant defining a TypeDescriptor for <code>java.lang.Object</code> */
public static final TypeDescriptor OBJECT = new TypeDescriptor(Object.class);
/** Constant defining a TypeDescriptor for <code>java.lang.String</code> */
public static final TypeDescriptor STRING = new TypeDescriptor(String.class);
private static final Map<Class<?>, TypeDescriptor> typeDescriptorCache = new HashMap<Class<?>, TypeDescriptor>();
static {
typeDescriptorCache.put(String.class, new TypeDescriptor(String.class));
typeDescriptorCache.put(Byte.class, new TypeDescriptor(Byte.class));
typeDescriptorCache.put(Character.class, new TypeDescriptor(Character.class));
typeDescriptorCache.put(Boolean.class, new TypeDescriptor(Boolean.class));
typeDescriptorCache.put(Short.class, new TypeDescriptor(Short.class));
typeDescriptorCache.put(Integer.class, new TypeDescriptor(Integer.class));
typeDescriptorCache.put(Long.class, new TypeDescriptor(Long.class));
typeDescriptorCache.put(Float.class, new TypeDescriptor(Float.class));
typeDescriptorCache.put(Double.class, new TypeDescriptor(Double.class));
}
private Object value;
private Class<?> type;
@ -413,11 +421,9 @@ public class TypeDescriptor {
public static TypeDescriptor valueOf(Class<?> type) {
if (type == null) {
return TypeDescriptor.NULL;
} else if (type.equals(String.class)) {
return TypeDescriptor.STRING;
} else {
return new TypeDescriptor(type);
}
TypeDescriptor desc = typeDescriptorCache.get(type);
return desc != null ? desc : new TypeDescriptor(type);
}
/**

View File

@ -50,19 +50,19 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
this.conversionService = conversionService;
this.targetDescriptor = targetDescriptor;
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.STRING);
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(this.conversionService.convert(text, TypeDescriptor.STRING, this.targetDescriptor));
setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
}
@Override
public String getAsText() {
if (this.canConvertToString) {
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.STRING);
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
else {
return null;

View File

@ -414,7 +414,7 @@ public class DefaultConversionTests {
public void convertCollectionToStringWithElementConversion() throws Exception {
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 });
String result = (String) conversionService.convert(list,
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.STRING);
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.valueOf(String.class));
assertEquals("3,5", result);
}
@ -429,7 +429,7 @@ public class DefaultConversionTests {
@Test
public void convertStringToCollectionWithElementConversion() throws Exception {
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.STRING,
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(getClass().getField("genericList")));
assertEquals(3, result.size());
assertEquals(new Integer(1), result.get(0));
@ -679,7 +679,7 @@ public class DefaultConversionTests {
@Test
public void convertObjectToObjectFinderMethodWithNull() {
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(TestEntity.class));
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(TestEntity.class));
assertNull(e);
}

View File

@ -88,7 +88,7 @@ public class GenericConversionServiceTests {
@Test
public void convertNullTypeDescriptor() {
assertNull(conversionService.convert("3", TypeDescriptor.STRING, TypeDescriptor.NULL));
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
}
@Test
@ -121,9 +121,9 @@ public class GenericConversionServiceTests {
assertTrue(conversionService.canConvert(String.class, boolean.class));
Boolean b = conversionService.convert("true", boolean.class);
assertEquals(Boolean.TRUE, b);
assertTrue(conversionService.canConvert(TypeDescriptor.STRING, TypeDescriptor
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor
.valueOf(boolean.class)));
b = (Boolean) conversionService.convert("true", TypeDescriptor.STRING, TypeDescriptor
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor
.valueOf(boolean.class));
assertEquals(Boolean.TRUE, b);
}

View File

@ -107,11 +107,11 @@ public class CompositeStringExpression implements Expression {
}
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
@ -157,11 +157,11 @@ public class CompositeStringExpression implements Expression {
}
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException {

View File

@ -62,11 +62,11 @@ public class LiteralExpression implements Expression {
}
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
@ -91,49 +91,40 @@ public class LiteralExpression implements Expression {
return String.class;
}
public <T> T getValue(Object rootObject, Class<T> desiredResultType) throws EvaluationException {
Object value = getValue(rootObject);
return ExpressionUtils.convert(null, value, desiredResultType);
}
public String getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
return this.literalValue;
}
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) throws EvaluationException {
Object value = getValue(context, rootObject);
return ExpressionUtils.convert(null, value, desiredResultType);
}
public Class getValueType(Object rootObject) throws EvaluationException {
return String.class;
}
public Class getValueType(EvaluationContext context, Object rootObject) throws EvaluationException {
return String.class;
}
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}
public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException {
return false;
}
public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException {
throw new EvaluationException(literalValue, "Cannot call setValue() on a LiteralExpression");
}

View File

@ -29,7 +29,7 @@ public class Identifier extends SpelNodeImpl {
public Identifier(String payload,int pos) {
super(pos);
this.id = new TypedValue(payload, STRING_TYPE_DESCRIPTOR);
this.id = new TypedValue(payload);
}
@Override

View File

@ -55,7 +55,7 @@ public class Indexer extends SpelNodeImpl {
if (targetObject instanceof Map && (children[0] instanceof PropertyOrFieldReference)) {
PropertyOrFieldReference reference = (PropertyOrFieldReference)children[0];
index = reference.getName();
indexValue = new TypedValue(index, TypeDescriptor.STRING);
indexValue = new TypedValue(index);
}
else {
// In case the map key is unqualified, we want it evaluated against the root object so
@ -75,23 +75,20 @@ public class Indexer extends SpelNodeImpl {
if (targetObject == null) {
// Current decision: attempt to index into null map == exception and does not just return null
throw new SpelEvaluationException(getStartPosition(),SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
// if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
// return new TypedValue(null,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()));
// } else {
// return new TypedValue(null,TypeDescriptor.NULL);
// }
}
Object possiblyConvertedKey = index;
if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
possiblyConvertedKey = state.convertValue(index,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType()));
}
Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey);
TypeDescriptor resultDescriptor = (targetObjectTypeDescriptor.isMapEntryTypeKnown() ?
TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()) : TypeDescriptor.OBJECT);
return new TypedValue(o,resultDescriptor);
if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
return new TypedValue(o, targetObjectTypeDescriptor.getMapValueTypeDescriptor());
} else {
return new TypedValue(o);
}
}
int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR);
int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
if (targetObject == null) {
throw new SpelEvaluationException(getStartPosition(),SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
@ -139,7 +136,7 @@ public class Indexer extends SpelNodeImpl {
if (idx >= ctxString.length()) {
throw new SpelEvaluationException(getStartPosition(),SpelMessage.STRING_INDEX_OUT_OF_BOUNDS, ctxString.length(), idx);
}
return new TypedValue(String.valueOf(ctxString.charAt(idx)),STRING_TYPE_DESCRIPTOR);
return new TypedValue(String.valueOf(ctxString.charAt(idx)));
}
throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.asString());
}
@ -175,11 +172,11 @@ public class Indexer extends SpelNodeImpl {
}
if (targetObjectTypeDescriptor.isArray()) {
int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR);
int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
setArrayElement(state, contextObject.getValue(), idx, newValue, targetObjectTypeDescriptor.getElementType());
}
else if (targetObjectTypeDescriptor.isCollection()) {
int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR);
int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
Collection c = (Collection) targetObject;
if (idx >= c.size()) {
throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx);
@ -215,35 +212,35 @@ public class Indexer extends SpelNodeImpl {
if (arrayComponentType == Integer.TYPE) {
int[] array = (int[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Integer)state.convertValue(newValue, INTEGER_TYPE_DESCRIPTOR);
array[idx] = (Integer)state.convertValue(newValue, TypeDescriptor.valueOf(Integer.class));
} else if (arrayComponentType == Boolean.TYPE) {
boolean[] array = (boolean[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Boolean)state.convertValue(newValue, BOOLEAN_TYPE_DESCRIPTOR);
array[idx] = (Boolean)state.convertValue(newValue, TypeDescriptor.valueOf(Boolean.class));
} else if (arrayComponentType == Character.TYPE) {
char[] array = (char[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Character)state.convertValue(newValue, CHARACTER_TYPE_DESCRIPTOR);
array[idx] = (Character)state.convertValue(newValue, TypeDescriptor.valueOf(Character.class));
} else if (arrayComponentType == Long.TYPE) {
long[] array = (long[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Long)state.convertValue(newValue, LONG_TYPE_DESCRIPTOR);
array[idx] = (Long)state.convertValue(newValue, TypeDescriptor.valueOf(Long.class));
} else if (arrayComponentType == Short.TYPE) {
short[] array = (short[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Short)state.convertValue(newValue, SHORT_TYPE_DESCRIPTOR);
array[idx] = (Short)state.convertValue(newValue, TypeDescriptor.valueOf(Short.class));
} else if (arrayComponentType == Double.TYPE) {
double[] array = (double[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Double)state.convertValue(newValue, DOUBLE_TYPE_DESCRIPTOR);
array[idx] = (Double)state.convertValue(newValue, TypeDescriptor.valueOf(Double.class));
} else if (arrayComponentType == Float.TYPE) {
float[] array = (float[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Float)state.convertValue(newValue, FLOAT_TYPE_DESCRIPTOR);
array[idx] = (Float)state.convertValue(newValue, TypeDescriptor.valueOf(Float.class));
} else if (arrayComponentType == Byte.TYPE) {
byte[] array = (byte[]) ctx;
checkAccess(array.length, idx);
array[idx] = (Byte)state.convertValue(newValue, BYTE_TYPE_DESCRIPTOR);
array[idx] = (Byte)state.convertValue(newValue, TypeDescriptor.valueOf(Byte.class));
} else {
Object[] array = (Object[]) ctx;
checkAccess(array.length, idx);

View File

@ -14,7 +14,7 @@ public class IntLiteral extends Literal {
IntLiteral(String payload, int pos, int value) {
super(payload, pos);
this.value = new TypedValue(value, INTEGER_TYPE_DESCRIPTOR);
this.value = new TypedValue(value);
}
@Override

View File

@ -30,7 +30,7 @@ public class LongLiteral extends Literal {
LongLiteral(String payload, int pos, long value) {
super(payload, pos);
this.value = new TypedValue(value, LONG_TYPE_DESCRIPTOR);
this.value = new TypedValue(value);
}
@Override

View File

@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
@ -44,7 +45,7 @@ public class OpAnd extends Operator {
try {
TypedValue typedValue = getLeftOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
leftValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException ee) {
ee.setPosition(getLeftOperand().getStartPosition());
@ -58,7 +59,7 @@ public class OpAnd extends Operator {
try {
TypedValue typedValue = getRightOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
rightValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException ee) {
ee.setPosition(getRightOperand().getStartPosition());

View File

@ -42,11 +42,11 @@ public class OpDivide extends Operator {
Number op1 = (Number) operandOne;
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() / op2.doubleValue(), DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(op1.doubleValue() / op2.doubleValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() / op2.longValue(), LONG_TYPE_DESCRIPTOR);
return new TypedValue(op1.longValue() / op2.longValue());
} else { // TODO what about non-int result of the division?
return new TypedValue(op1.intValue() / op2.intValue(), INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(op1.intValue() / op2.intValue());
}
}
Object result = state.operate(Operation.DIVIDE, operandOne, operandTwo);

View File

@ -51,11 +51,11 @@ public class OpMinus extends Operator {
if (operand instanceof Number) {
Number n = (Number) operand;
if (operand instanceof Double) {
return new TypedValue(0 - n.doubleValue(),DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(0 - n.doubleValue());
} else if (operand instanceof Long) {
return new TypedValue(0 - n.longValue(),LONG_TYPE_DESCRIPTOR);
return new TypedValue(0 - n.longValue());
} else {
return new TypedValue(0 - n.intValue(),INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(0 - n.intValue());
}
}
return state.operate(Operation.SUBTRACT, operand, null);
@ -66,17 +66,17 @@ public class OpMinus extends Operator {
Number op1 = (Number) left;
Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() - op2.doubleValue(),DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(op1.doubleValue() - op2.doubleValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() - op2.longValue(),LONG_TYPE_DESCRIPTOR);
return new TypedValue(op1.longValue() - op2.longValue());
} else {
return new TypedValue(op1.intValue() - op2.intValue(),INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(op1.intValue() - op2.intValue());
}
} else if (left instanceof String && right instanceof Integer && ((String)left).length()==1) {
String theString = (String) left;
Integer theInteger = (Integer) right;
// implements character - int (ie. b - 1 = a)
return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger)),STRING_TYPE_DESCRIPTOR);
return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger)));
}
return state.operate(Operation.SUBTRACT, left, right);
}

View File

@ -41,11 +41,11 @@ public class OpModulus extends Operator {
Number op1 = (Number) operandOne;
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() % op2.doubleValue(),DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(op1.doubleValue() % op2.doubleValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() % op2.longValue(),LONG_TYPE_DESCRIPTOR);
return new TypedValue(op1.longValue() % op2.longValue());
} else {
return new TypedValue(op1.intValue() % op2.intValue(),INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(op1.intValue() % op2.intValue());
}
}
return state.operate(Operation.MODULUS, operandOne, operandTwo);

View File

@ -60,11 +60,11 @@ public class OpMultiply extends Operator {
Number leftNumber = (Number) operandOne;
Number rightNumber = (Number) operandTwo;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue(), DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
} else if (leftNumber instanceof Long || rightNumber instanceof Long) {
return new TypedValue(leftNumber.longValue() * rightNumber.longValue(), LONG_TYPE_DESCRIPTOR);
return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
} else {
return new TypedValue(leftNumber.intValue() * rightNumber.intValue(), INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(leftNumber.intValue() * rightNumber.intValue());
}
} else if (operandOne instanceof String && operandTwo instanceof Integer) {
int repeats = (Integer) operandTwo;
@ -72,7 +72,7 @@ public class OpMultiply extends Operator {
for (int i = 0; i < repeats; i++) {
result.append(operandOne);
}
return new TypedValue(result.toString(), STRING_TYPE_DESCRIPTOR);
return new TypedValue(result.toString());
}
return state.operate(Operation.MULTIPLY, operandOne, operandTwo);
}

View File

@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
@ -43,7 +44,7 @@ public class OpOr extends Operator {
try {
TypedValue typedValue = getLeftOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
leftValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException see) {
see.setPosition(getLeftOperand().getStartPosition());
@ -57,7 +58,7 @@ public class OpOr extends Operator {
try {
TypedValue typedValue = getRightOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
rightValue = (Boolean)state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException see) {
see.setPosition(getRightOperand().getStartPosition()); // TODO end positions here and in similar situations

View File

@ -49,11 +49,11 @@ public class OpPlus extends Operator {
Object operandOne = leftOp.getValueInternal(state).getValue();
if (operandOne instanceof Number) {
if (operandOne instanceof Double) {
return new TypedValue(((Double) operandOne).doubleValue(), DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(((Double) operandOne).doubleValue());
} else if (operandOne instanceof Long) {
return new TypedValue(((Long) operandOne).longValue(), LONG_TYPE_DESCRIPTOR);
return new TypedValue(((Long) operandOne).longValue());
} else {
return new TypedValue(((Integer) operandOne).intValue(), INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(((Integer) operandOne).intValue());
}
}
return state.operate(Operation.ADD, operandOne, null);
@ -65,22 +65,22 @@ public class OpPlus extends Operator {
Number op1 = (Number) operandOne;
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() + op2.doubleValue(),DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(op1.doubleValue() + op2.doubleValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() + op2.longValue(),LONG_TYPE_DESCRIPTOR);
return new TypedValue(op1.longValue() + op2.longValue());
} else { // TODO what about overflow?
return new TypedValue(op1.intValue() + op2.intValue(),INTEGER_TYPE_DESCRIPTOR);
return new TypedValue(op1.intValue() + op2.intValue());
}
} else if (operandOne instanceof String && operandTwo instanceof String) {
return new TypedValue(new StringBuilder((String) operandOne).append((String) operandTwo).toString(),STRING_TYPE_DESCRIPTOR);
return new TypedValue(new StringBuilder((String) operandOne).append((String) operandTwo).toString());
} else if (operandOne instanceof String) {
StringBuilder result = new StringBuilder((String)operandOne);
result.append((operandTwo==null?"null":operandTwo.toString()));
return new TypedValue(result.toString(),STRING_TYPE_DESCRIPTOR);
return new TypedValue(result.toString());
} else if (operandTwo instanceof String) {
StringBuilder result = new StringBuilder((operandOne==null?"null":operandOne.toString()));
result.append((String)operandTwo);
return new TypedValue(result.toString(),STRING_TYPE_DESCRIPTOR);
return new TypedValue(result.toString());
}
return state.operate(Operation.ADD, operandOne, operandTwo);
}

View File

@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
@ -43,7 +44,7 @@ public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so do
if (TypedValue.NULL_TYPED_VALUE.equals(typedValue)) {
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
}
boolean value = (Boolean) state.convertValue(typedValue, BOOLEAN_TYPE_DESCRIPTOR);
boolean value = (Boolean) state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
return BooleanTypedValue.forValue(!value);
}
catch (SpelEvaluationException see) {

View File

@ -44,16 +44,16 @@ public class OperatorPower extends Operator {
Number op1 = (Number) operandOne;
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(Math.pow(op1.doubleValue(),op2.doubleValue()),DOUBLE_TYPE_DESCRIPTOR);
return new TypedValue(Math.pow(op1.doubleValue(),op2.doubleValue()));
} else if (op1 instanceof Long || op2 instanceof Long) {
double d= Math.pow(op1.longValue(), op2.longValue());
return new TypedValue((long)d, LONG_TYPE_DESCRIPTOR);
return new TypedValue((long)d);
} else {
double d= Math.pow(op1.longValue(), op2.longValue());
if (d > Integer.MAX_VALUE) {
return new TypedValue((long)d,LONG_TYPE_DESCRIPTOR);
return new TypedValue((long)d);
} else {
return new TypedValue((int)d,INTEGER_TYPE_DESCRIPTOR);
return new TypedValue((int)d);
}
}
}

View File

@ -49,7 +49,7 @@ public class QualifiedIdentifier extends SpelNodeImpl {
}
sb.append(value);
}
this.value = new TypedValue(sb.toString(),STRING_TYPE_DESCRIPTOR);
this.value = new TypedValue(sb.toString());
}
return this.value;
}

View File

@ -28,7 +28,7 @@ public class RealLiteral extends Literal {
public RealLiteral(String payload, int pos, double value) {
super(payload, pos);
this.value = new TypedValue(value,DOUBLE_TYPE_DESCRIPTOR);
this.value = new TypedValue(value);
}
@Override

View File

@ -16,7 +16,6 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.common.ExpressionUtils;
@ -35,18 +34,6 @@ import org.springframework.util.Assert;
*/
public abstract class SpelNodeImpl implements SpelNode {
static TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.OBJECT;
static TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.STRING;
static TypeDescriptor CLASS_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Class.class);
static TypeDescriptor BOOLEAN_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Boolean.class);
static TypeDescriptor CHARACTER_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Character.class);
static TypeDescriptor BYTE_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Byte.class);
static TypeDescriptor SHORT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Short.class);
static TypeDescriptor INTEGER_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Integer.class);
static TypeDescriptor LONG_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Long.class);
static TypeDescriptor FLOAT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Float.class);
static TypeDescriptor DOUBLE_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Double.class);
private static SpelNodeImpl[] NO_CHILDREN = new SpelNodeImpl[0];
protected int pos; // start = top 16bits, end = bottom 16bits

View File

@ -31,7 +31,7 @@ public class StringLiteral extends Literal {
super(payload,pos);
// TODO should these have been skipped being created by the parser rules? or not?
value = value.substring(1, value.length() - 1);
this.value = new TypedValue(value.replaceAll("''", "'"),STRING_TYPE_DESCRIPTOR);
this.value = new TypedValue(value.replaceAll("''", "'"));
}
@Override

View File

@ -39,10 +39,10 @@ public class TypeReference extends SpelNodeImpl {
TypeCode tc = TypeCode.valueOf(typename.toUpperCase());
if (tc != TypeCode.OBJECT) {
// it is a primitive type
return new TypedValue(tc.getType(),CLASS_TYPE_DESCRIPTOR);
return new TypedValue(tc.getType());
}
}
return new TypedValue(state.findType(typename),CLASS_TYPE_DESCRIPTOR);
return new TypedValue(state.findType(typename));
}
@Override

View File

@ -257,10 +257,10 @@ public class ExpressionStateTests extends ExpressionTestCase {
@Test
public void testTypeConversion() throws EvaluationException {
ExpressionState state = getState();
String s = (String)state.convertValue(34,TypeDescriptor.STRING);
String s = (String)state.convertValue(34, TypeDescriptor.valueOf(String.class));
Assert.assertEquals("34",s);
s = (String)state.convertValue(new TypedValue(34),TypeDescriptor.STRING);
s = (String)state.convertValue(new TypedValue(34), TypeDescriptor.valueOf(String.class));
Assert.assertEquals("34",s);
}

View File

@ -29,7 +29,6 @@ import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* Testing variations on map access.
@ -78,7 +77,7 @@ public class MapAccessTests extends ExpressionTestCase {
}
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
return new TypedValue(((Map) target).get(name));
}
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {

View File

@ -17,8 +17,8 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
@ -26,8 +26,8 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
///CLOVER:OFF
@ -155,7 +155,7 @@ public class PropertyAccessTests extends ExpressionTestCase {
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
if (!name.equals("flibbles"))
throw new RuntimeException("Assertion Failed! name should be flibbles");
return new TypedValue(flibbles, TypeDescriptor.STRING);
return new TypedValue(flibbles, TypeDescriptor.valueOf(String.class));
}
public void write(EvaluationContext context, Object target, String name, Object newValue)

View File

@ -21,9 +21,8 @@ import java.util.Map;
import java.util.Properties;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
@ -219,7 +218,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
return new TypedValue(((Map) target).get(name));
}
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {