introduced TypeDescriptor.OBJECT and TypeDescriptor.STRING

This commit is contained in:
Juergen Hoeller 2009-12-15 10:21:16 +00:00
parent 1b0c6b848d
commit f52986ea15
16 changed files with 109 additions and 116 deletions

View File

@ -39,7 +39,7 @@ public class MapAccessor implements PropertyAccessor {
} }
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.valueOf(Object.class)); return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
} }
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {

View File

@ -32,8 +32,8 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory;
import org.springframework.format.datetime.joda.DateTimeParser; import org.springframework.format.datetime.joda.DateTimeParser;
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory;
import org.springframework.format.datetime.joda.ReadablePartialPrinter; import org.springframework.format.datetime.joda.ReadablePartialPrinter;
import org.springframework.format.number.NumberFormatter; import org.springframework.format.number.NumberFormatter;
@ -95,9 +95,9 @@ public class FormattingConversionServiceTests {
}); });
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime() String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class)); .toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.STRING);
assertEquals("10/31/09", formatted); assertEquals("10/31/09", formatted);
LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.valueOf(String.class), LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.STRING,
new TypeDescriptor(Model.class.getField("date")))); new TypeDescriptor(Model.class.getField("date"))));
assertEquals(new LocalDate(2009, 10, 31), date); assertEquals(new LocalDate(2009, 10, 31), date);
} }
@ -105,34 +105,34 @@ public class FormattingConversionServiceTests {
@Test @Test
public void testPrintNull() throws ParseException { public void testPrintNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter()); formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))); assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
} }
@Test @Test
public void testParseNull() throws ParseException { public void testParseNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter()); formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
} }
@Test @Test
public void testParseEmptyString() throws ParseException { public void testParseEmptyString() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter()); formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
} }
@Test @Test
public void testPrintNullDefault() throws ParseException { public void testPrintNullDefault() throws ParseException {
assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))); assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
} }
@Test @Test
public void testParseNullDefault() throws ParseException { public void testParseNullDefault() throws ParseException {
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
} }
@Test @Test
public void testParseEmptyStringDefault() throws ParseException { public void testParseEmptyStringDefault() throws ParseException {
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
} }
private static class Model { private static class Model {

View File

@ -37,11 +37,15 @@ import org.springframework.util.ClassUtils;
*/ */
public class TypeDescriptor { public class TypeDescriptor {
/** /** Constant defining an 'unknown' TypeDescriptor */
* Constant defining an 'unknown' TypeDescriptor.
*/
public static final TypeDescriptor NULL = new TypeDescriptor(); public static final TypeDescriptor NULL = new TypeDescriptor();
/** Constant defining a TypeDescriptor for <code>java.lang.Object</code> */
public static final TypeDescriptor OBJECT = TypeDescriptor.valueOf(Object.class);
/** Constant defining a TypeDescriptor for <code>java.lang.String</code> */
public static final TypeDescriptor STRING = TypeDescriptor.valueOf(String.class);
private Class<?> type; private Class<?> type;

View File

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

View File

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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.expression; package org.springframework.expression;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
@ -34,7 +35,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public Object getValue() throws EvaluationException; Object getValue() throws EvaluationException;
/** /**
* Evaluate this expression against the specified root object * Evaluate this expression against the specified root object
@ -43,7 +44,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public Object getValue(Object rootObject) throws EvaluationException; Object getValue(Object rootObject) throws EvaluationException;
/** /**
* Evaluate the expression in the default context. If the result of the evaluation does not match (and * Evaluate the expression in the default context. If the result of the evaluation does not match (and
@ -53,7 +54,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public <T> T getValue(Class<T> desiredResultType) throws EvaluationException; <T> T getValue(Class<T> desiredResultType) throws EvaluationException;
/** /**
* Evaluate the expression in the default context against the specified root object. If the * Evaluate the expression in the default context against the specified root object. If the
@ -65,7 +66,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public <T> T getValue(Object rootObject, Class<T> desiredResultType) throws EvaluationException; <T> T getValue(Object rootObject, Class<T> desiredResultType) throws EvaluationException;
/** /**
* Evaluate this expression in the provided context and return the result of evaluation. * Evaluate this expression in the provided context and return the result of evaluation.
@ -74,7 +75,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public Object getValue(EvaluationContext context) throws EvaluationException; Object getValue(EvaluationContext context) throws EvaluationException;
/** /**
* Evaluate this expression in the provided context and return the result of evaluation, but use * Evaluate this expression in the provided context and return the result of evaluation, but use
@ -85,7 +86,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException; Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException;
/** /**
* Evaluate the expression in a specified context which can resolve references to properties, methods, types, etc - * Evaluate the expression in a specified context which can resolve references to properties, methods, types, etc -
@ -97,7 +98,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public <T> T getValue(EvaluationContext context, Class<T> desiredResultType) throws EvaluationException; <T> T getValue(EvaluationContext context, Class<T> desiredResultType) throws EvaluationException;
/** /**
* Evaluate the expression in a specified context which can resolve references to properties, methods, types, etc - * Evaluate the expression in a specified context which can resolve references to properties, methods, types, etc -
@ -111,7 +112,7 @@ public interface Expression {
* @return the evaluation result * @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) throws EvaluationException; <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using
@ -120,107 +121,97 @@ public interface Expression {
* @return the most general type of value that can be set on this context * @return the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public Class getValueType() throws EvaluationException; Class getValueType() throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the default context. * method using the default context.
*
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return the most general type of value that can be set on this context * @return the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public Class getValueType(Object rootObject) throws EvaluationException; Class getValueType(Object rootObject) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the given context. * method for the given context.
*
* @param context the context in which to evaluate the expression * @param context the context in which to evaluate the expression
* @return the most general type of value that can be set on this context * @return the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public Class getValueType(EvaluationContext context) throws EvaluationException; Class getValueType(EvaluationContext context) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the given context. The supplied root object overrides any specified in the context. * method for the given context. The supplied root object overrides any specified in the context.
*
* @param context the context in which to evaluate the expression * @param context the context in which to evaluate the expression
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return the most general type of value that can be set on this context * @return the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public Class getValueType(EvaluationContext context, Object rootObject) throws EvaluationException; Class getValueType(EvaluationContext context, Object rootObject) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the default context. * method using the default context.
*
* @return a type descriptor for the most general type of value that can be set on this context * @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException; TypeDescriptor getValueTypeDescriptor() throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the default context. * method using the default context.
*
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return a type descriptor for the most general type of value that can be set on this context * @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException; TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)}
* the given context. * method for the given context.
*
* @param context the context in which to evaluate the expression * @param context the context in which to evaluate the expression
* @return a type descriptor for the most general type of value that can be set on this context * @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException; TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException;
/** /**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for * Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for
* the given context. The supplied root object overrides any specified in the context. * the given context. The supplied root object overrides any specified in the context.
*
* @param context the context in which to evaluate the expression * @param context the context in which to evaluate the expression
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return a type descriptor for the most general type of value that can be set on this context * @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type * @throws EvaluationException if there is a problem determining the type
*/ */
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException; TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException;
/** /**
* Determine if an expression can be written to, i.e. setValue() can be called. * Determine if an expression can be written to, i.e. setValue() can be called.
*
* @param context the context in which the expression should be checked * @param context the context in which the expression should be checked
* @return true if the expression is writable * @return true if the expression is writable
* @throws EvaluationException if there is a problem determining if it is writable * @throws EvaluationException if there is a problem determining if it is writable
*/ */
public boolean isWritable(EvaluationContext context) throws EvaluationException; boolean isWritable(EvaluationContext context) throws EvaluationException;
/** /**
* Determine if an expression can be written to, i.e. setValue() can be called. * Determine if an expression can be written to, i.e. setValue() can be called.
* The supplied root object overrides any specified in the context. * The supplied root object overrides any specified in the context.
*
* @param context the context in which the expression should be checked * @param context the context in which the expression should be checked
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return true if the expression is writable * @return true if the expression is writable
* @throws EvaluationException if there is a problem determining if it is writable * @throws EvaluationException if there is a problem determining if it is writable
*/ */
public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException; boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException;
/** /**
* Determine if an expression can be written to, i.e. setValue() can be called. * Determine if an expression can be written to, i.e. setValue() can be called.
*
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @return true if the expression is writable * @return true if the expression is writable
* @throws EvaluationException if there is a problem determining if it is writable * @throws EvaluationException if there is a problem determining if it is writable
*/ */
public boolean isWritable(Object rootObject) throws EvaluationException; boolean isWritable(Object rootObject) throws EvaluationException;
/** /**
* Set this expression in the provided context to the value provided. * Set this expression in the provided context to the value provided.
@ -229,33 +220,30 @@ public interface Expression {
* @param value the new value * @param value the new value
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public void setValue(EvaluationContext context, Object value) throws EvaluationException; void setValue(EvaluationContext context, Object value) throws EvaluationException;
/** /**
* Set this expression in the provided context to the value provided. * Set this expression in the provided context to the value provided.
*
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @param value the new value * @param value the new value
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public void setValue(Object rootObject, Object value) throws EvaluationException; void setValue(Object rootObject, Object value) throws EvaluationException;
/** /**
* Set this expression in the provided context to the value provided. * Set this expression in the provided context to the value provided.
* The supplied root object overrides any specified in the context. * The supplied root object overrides any specified in the context.
*
* @param context the context in which to set the value of the expression * @param context the context in which to set the value of the expression
* @param rootObject the root object against which to evaluate the expression * @param rootObject the root object against which to evaluate the expression
* @param value the new value * @param value the new value
* @throws EvaluationException if there is a problem during evaluation * @throws EvaluationException if there is a problem during evaluation
*/ */
public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException; void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException;
/** /**
* Returns the original string used to create this expression, unmodified. * Returns the original string used to create this expression, unmodified.
*
* @return the original expression string * @return the original expression string
*/ */
public String getExpressionString(); String getExpressionString();
} }

View File

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

View File

@ -22,9 +22,10 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
/** /**
* A very simple hardcoded implementation of the Expression interface that represents a string literal. It is used with * A very simple hardcoded implementation of the Expression interface that represents a string literal.
* CompositeStringExpression when representing a template expression which is made up of pieces - some being real * It is used with CompositeStringExpression when representing a template expression which is made up
* expressions to be handled by an EL implementation like Spel, and some being just textual elements. * of pieces - some being real expressions to be handled by an EL implementation like Spel, and some
* being just textual elements.
* *
* @author Andy Clement * @author Andy Clement
* @since 3.0 * @since 3.0
@ -61,11 +62,11 @@ public class LiteralExpression implements Expression {
} }
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) { public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.valueOf(String.class); return TypeDescriptor.STRING;
} }
public TypeDescriptor getValueTypeDescriptor() { public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.valueOf(String.class); return TypeDescriptor.STRING;
} }
public void setValue(EvaluationContext context, Object value) throws EvaluationException { public void setValue(EvaluationContext context, Object value) throws EvaluationException {
@ -102,8 +103,7 @@ public class LiteralExpression implements Expression {
} }
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) throws EvaluationException {
throws EvaluationException {
Object value = getValue(context, rootObject); Object value = getValue(context, rootObject);
return ExpressionUtils.convert(null, value, desiredResultType); return ExpressionUtils.convert(null, value, desiredResultType);
} }
@ -120,13 +120,12 @@ public class LiteralExpression implements Expression {
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException { public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.valueOf(String.class); return TypeDescriptor.STRING;
} }
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
throws EvaluationException { return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
} }

View File

@ -55,7 +55,7 @@ public class Indexer extends SpelNodeImpl {
if (targetObject instanceof Map && (children[0] instanceof PropertyOrFieldReference)) { if (targetObject instanceof Map && (children[0] instanceof PropertyOrFieldReference)) {
PropertyOrFieldReference reference = (PropertyOrFieldReference)children[0]; PropertyOrFieldReference reference = (PropertyOrFieldReference)children[0];
index = reference.getName(); index = reference.getName();
indexValue = new TypedValue(index, TypeDescriptor.valueOf(String.class)); indexValue = new TypedValue(index, TypeDescriptor.STRING);
} }
else { else {
// In case the map key is unqualified, we want it evaluated against the root object so // In case the map key is unqualified, we want it evaluated against the root object so
@ -86,8 +86,8 @@ public class Indexer extends SpelNodeImpl {
possiblyConvertedKey = state.convertValue(index,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType())); possiblyConvertedKey = state.convertValue(index,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType()));
} }
Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey); Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey);
TypeDescriptor resultDescriptor = targetObjectTypeDescriptor.isMapEntryTypeKnown()? TypeDescriptor resultDescriptor = (targetObjectTypeDescriptor.isMapEntryTypeKnown() ?
TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()): TypeDescriptor.valueOf(Object.class); TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()) : TypeDescriptor.OBJECT);
return new TypedValue(o,resultDescriptor); return new TypedValue(o,resultDescriptor);
} }

View File

@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast; package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.common.ExpressionUtils; import org.springframework.expression.common.ExpressionUtils;
@ -24,7 +25,7 @@ import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage; import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelNode; import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.util.Assert;
/** /**
* The common supertype of all AST nodes in a parsed Spring Expression Language format expression. * The common supertype of all AST nodes in a parsed Spring Expression Language format expression.
@ -34,17 +35,17 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
public abstract class SpelNodeImpl implements SpelNode { 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 BOOLEAN_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Boolean.class);
static TypeDescriptor INTEGER_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Integer.class);
static TypeDescriptor CHARACTER_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Character.class); static TypeDescriptor CHARACTER_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Character.class);
static TypeDescriptor LONG_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Long.class);
static TypeDescriptor SHORT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Short.class);
static TypeDescriptor BYTE_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Byte.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 FLOAT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Float.class);
static TypeDescriptor DOUBLE_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Double.class); static TypeDescriptor DOUBLE_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Double.class);
static TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
static TypeDescriptor CLASS_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Class.class);
static TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Object.class);
private static SpelNodeImpl[] NO_CHILDREN = new SpelNodeImpl[0]; private static SpelNodeImpl[] NO_CHILDREN = new SpelNodeImpl[0];
@ -55,10 +56,10 @@ public abstract class SpelNodeImpl implements SpelNode {
public SpelNodeImpl(int pos, SpelNodeImpl... operands) { public SpelNodeImpl(int pos, SpelNodeImpl... operands) {
this.pos = pos; this.pos = pos;
// pos combines start and end so can never be zero because tokens cannot be zero length // pos combines start and end so can never be zero because tokens cannot be zero length
assert pos!=0; Assert.isTrue(pos != 0);
if (operands!=null && operands.length>0) { if (operands != null && operands.length > 0) {
this.children = operands; this.children = operands;
for (SpelNodeImpl childnode: operands) { for (SpelNodeImpl childnode : operands) {
childnode.parent = this; childnode.parent = this;
} }
} }
@ -66,8 +67,8 @@ public abstract class SpelNodeImpl implements SpelNode {
protected SpelNodeImpl getPreviousChild() { protected SpelNodeImpl getPreviousChild() {
SpelNodeImpl result = null; SpelNodeImpl result = null;
if (parent!=null) { if (parent != null) {
for (SpelNodeImpl child: parent.children) { for (SpelNodeImpl child : parent.children) {
if (this==child) break; if (this==child) break;
result = child; result = child;
} }

View File

@ -64,6 +64,7 @@ import org.springframework.expression.spel.ast.StringLiteral;
import org.springframework.expression.spel.ast.Ternary; import org.springframework.expression.spel.ast.Ternary;
import org.springframework.expression.spel.ast.TypeReference; import org.springframework.expression.spel.ast.TypeReference;
import org.springframework.expression.spel.ast.VariableReference; import org.springframework.expression.spel.ast.VariableReference;
import org.springframework.util.Assert;
/** /**
* Hand written SpEL parser. Instances are reusable but are not thread safe. * Hand written SpEL parser. Instances are reusable but are not thread safe.
@ -114,7 +115,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
if (moreTokens()) { if (moreTokens()) {
throw new SpelParseException(peekToken().startpos,SpelMessage.MORE_INPUT,toString(nextToken())); throw new SpelParseException(peekToken().startpos,SpelMessage.MORE_INPUT,toString(nextToken()));
} }
assert constructedNodes.isEmpty(); Assert.isTrue(constructedNodes.isEmpty());
return new SpelExpression(expressionString, ast, configuration); return new SpelExpression(expressionString, ast, configuration);
} }
catch (InternalParseException ipe) { catch (InternalParseException ipe) {
@ -179,7 +180,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
private SpelNodeImpl eatRelationalExpression() { private SpelNodeImpl eatRelationalExpression() {
SpelNodeImpl expr = eatSumExpression(); SpelNodeImpl expr = eatSumExpression();
Token relationalOperatorToken = maybeEatRelationalOperator(); Token relationalOperatorToken = maybeEatRelationalOperator();
if (relationalOperatorToken!=null) { if (relationalOperatorToken != null) {
Token t = nextToken(); //consume relational operator token Token t = nextToken(); //consume relational operator token
SpelNodeImpl rhExpr = eatSumExpression(); SpelNodeImpl rhExpr = eatSumExpression();
checkRightOperand(t,rhExpr); checkRightOperand(t,rhExpr);
@ -194,10 +195,10 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
return new OpLE(pos,expr,rhExpr); return new OpLE(pos,expr,rhExpr);
} else if (tk==TokenKind.GE) { } else if (tk==TokenKind.GE) {
return new OpGE(pos,expr,rhExpr); return new OpGE(pos,expr,rhExpr);
} else if (tk==TokenKind.EQ) { } else if (tk == TokenKind.EQ) {
return new OpEQ(pos,expr,rhExpr); return new OpEQ(pos,expr,rhExpr);
} else { } else {
assert tk==TokenKind.NE; Assert.isTrue(tk == TokenKind.NE);
return new OpNE(pos,expr,rhExpr); return new OpNE(pos,expr,rhExpr);
} }
} }
@ -206,7 +207,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
} else if (tk==TokenKind.MATCHES) { } else if (tk==TokenKind.MATCHES) {
return new OperatorMatches(toPos(t),expr,rhExpr); return new OperatorMatches(toPos(t),expr,rhExpr);
} else { } else {
assert tk==TokenKind.BETWEEN; Assert.isTrue(tk==TokenKind.BETWEEN);
return new org.springframework.expression.spel.ast.OperatorBetween(toPos(t),expr,rhExpr); return new org.springframework.expression.spel.ast.OperatorBetween(toPos(t),expr,rhExpr);
} }
} }
@ -223,7 +224,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
if (t.kind==TokenKind.PLUS) { if (t.kind==TokenKind.PLUS) {
expr = new OpPlus(toPos(t),expr,rhExpr); expr = new OpPlus(toPos(t),expr,rhExpr);
} else { } else {
assert t.kind==TokenKind.MINUS; Assert.isTrue(t.kind==TokenKind.MINUS);
expr = new OpMinus(toPos(t),expr,rhExpr); expr = new OpMinus(toPos(t),expr,rhExpr);
} }
} }
@ -242,7 +243,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
} else if (t.kind==TokenKind.DIV) { } else if (t.kind==TokenKind.DIV) {
expr = new OpDivide(toPos(t),expr,rhExpr); expr = new OpDivide(toPos(t),expr,rhExpr);
} else { } else {
assert t.kind==TokenKind.MOD; Assert.isTrue(t.kind==TokenKind.MOD);
expr = new OpModulus(toPos(t),expr,rhExpr); expr = new OpModulus(toPos(t),expr,rhExpr);
} }
} }
@ -271,7 +272,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
} else if (t.kind==TokenKind.PLUS) { } else if (t.kind==TokenKind.PLUS) {
return new OpPlus(toPos(t),expr); return new OpPlus(toPos(t),expr);
} else { } else {
assert t.kind==TokenKind.MINUS; Assert.isTrue(t.kind==TokenKind.MINUS);
return new OpMinus(toPos(t),expr); return new OpMinus(toPos(t),expr);
} }
} else { } else {
@ -653,7 +654,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
} }
private Token eatToken(TokenKind expectedKind) { private Token eatToken(TokenKind expectedKind) {
assert moreTokens(); Assert.isTrue(moreTokens());
Token t = nextToken(); Token t = nextToken();
if (t==null) { if (t==null) {
raiseInternalException( expressionString.length(), SpelMessage.OOD); raiseInternalException( expressionString.length(), SpelMessage.OOD);

View File

@ -23,6 +23,7 @@ import java.util.List;
import org.springframework.expression.spel.InternalParseException; import org.springframework.expression.spel.InternalParseException;
import org.springframework.expression.spel.SpelMessage; import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelParseException; import org.springframework.expression.spel.SpelParseException;
import org.springframework.util.Assert;
/** /**
* Lex some input data into a stream of tokens that can then be parsed. * Lex some input data into a stream of tokens that can then be parsed.
@ -395,8 +396,8 @@ class Tokenizer {
* Check if this might be a two character token. * Check if this might be a two character token.
*/ */
private boolean isTwoCharToken(TokenKind kind) { private boolean isTwoCharToken(TokenKind kind) {
assert kind.tokenChars.length==2; Assert.isTrue(kind.tokenChars.length == 2);
assert toProcess[pos] == kind.tokenChars[0]; Assert.isTrue(toProcess[pos] == kind.tokenChars[0]);
return toProcess[pos+1] == kind.tokenChars[1]; return toProcess[pos+1] == kind.tokenChars[1];
} }

View File

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

View File

@ -78,7 +78,7 @@ public class MapAccessTests extends ExpressionTestCase {
} }
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.valueOf(Object.class)); return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
} }
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {

View File

@ -155,7 +155,7 @@ public class PropertyAccessTests extends ExpressionTestCase {
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
if (!name.equals("flibbles")) if (!name.equals("flibbles"))
throw new RuntimeException("Assertion Failed! name should be flibbles"); throw new RuntimeException("Assertion Failed! name should be flibbles");
return new TypedValue(flibbles, TypeDescriptor.valueOf(String.class)); return new TypedValue(flibbles, TypeDescriptor.STRING);
} }
public void write(EvaluationContext context, Object target, String name, Object newValue) public void write(EvaluationContext context, Object target, String name, Object newValue)

View File

@ -219,7 +219,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
} }
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.valueOf(Object.class)); return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
} }
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {