rearranged spel subpackages in order to avoid package dependency cycle; introduced SpelParserConfiguration object to replace bit flags

This commit is contained in:
Juergen Hoeller 2009-12-15 02:03:16 +00:00
parent b5b1962530
commit 086aeb0aac
36 changed files with 295 additions and 307 deletions

View File

@ -29,7 +29,6 @@ import org.springframework.expression.OperatorOverloader;
import org.springframework.expression.PropertyAccessor; import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypeComparator; import org.springframework.expression.TypeComparator;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpressionParserConfiguration;
/** /**
* An ExpressionState is for maintaining per-expression-evaluation state, any changes to it are not seen by other * An ExpressionState is for maintaining per-expression-evaluation state, any changes to it are not seen by other
@ -52,14 +51,15 @@ public class ExpressionState {
private final TypedValue rootObject; private final TypedValue rootObject;
private int configuration = 0; private SpelParserConfiguration configuration;
public ExpressionState(EvaluationContext context) { public ExpressionState(EvaluationContext context) {
this.relatedContext = context; this.relatedContext = context;
this.rootObject = context.getRootObject(); this.rootObject = context.getRootObject();
} }
public ExpressionState(EvaluationContext context, int configuration) { public ExpressionState(EvaluationContext context, SpelParserConfiguration configuration) {
this.relatedContext = context; this.relatedContext = context;
this.configuration = configuration; this.configuration = configuration;
this.rootObject = context.getRootObject(); this.rootObject = context.getRootObject();
@ -70,14 +70,15 @@ public class ExpressionState {
this.rootObject = rootObject; this.rootObject = rootObject;
} }
public ExpressionState(EvaluationContext context, TypedValue rootObject, int configuration) { public ExpressionState(EvaluationContext context, TypedValue rootObject, SpelParserConfiguration configuration) {
this.relatedContext = context; this.relatedContext = context;
this.configuration = configuration; this.configuration = configuration;
this.rootObject = rootObject; this.rootObject = rootObject;
} }
private void ensureVariableScopesInitialized() { private void ensureVariableScopesInitialized() {
if (variableScopes == null) { if (this.variableScopes == null) {
this.variableScopes = new Stack<VariableScope>(); this.variableScopes = new Stack<VariableScope>();
// top level empty variable scope // top level empty variable scope
this.variableScopes.add(new VariableScope()); this.variableScopes.add(new VariableScope());
@ -198,7 +199,11 @@ public class ExpressionState {
public EvaluationContext getEvaluationContext() { public EvaluationContext getEvaluationContext() {
return this.relatedContext; return this.relatedContext;
} }
public SpelParserConfiguration getConfiguration() {
return this.configuration;
}
/** /**
* A new scope is entered when a function is called and it is used to hold the parameters to the function call. If the names * A new scope is entered when a function is called and it is used to hold the parameters to the function call. If the names
* of the parameters clash with those in a higher level scope, those in the higher level scope will not be accessible whilst * of the parameters clash with those in a higher level scope, those in the higher level scope will not be accessible whilst
@ -233,12 +238,4 @@ public class ExpressionState {
} }
} }
public boolean configuredToGrowCollection() {
return (configuration & SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize)!=0;
}
public boolean configuredToDynamicallyCreateNullObjects() {
return (configuration & SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull)!=0;
}
} }

View File

@ -13,12 +13,13 @@
* 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.spel.standard;
package org.springframework.expression.spel;
import org.springframework.expression.spel.SpelParseException; import org.springframework.expression.spel.SpelParseException;
/** /**
* Wraps a real parse exception. This exception flows to the top parse method and then * Wraps a real parse exception. This exception flows to the top parse method and then
* the wrapped exception is thrown as the real problem. * the wrapped exception is thrown as the real problem.
* *
* @author Andy Clement * @author Andy Clement
@ -26,12 +27,12 @@ import org.springframework.expression.spel.SpelParseException;
*/ */
public class InternalParseException extends RuntimeException { public class InternalParseException extends RuntimeException {
public InternalParseException(SpelParseException t) { public InternalParseException(SpelParseException cause) {
super(t); super(cause);
} }
public SpelParseException getCause() { public SpelParseException getCause() {
return (SpelParseException)super.getCause(); return (SpelParseException) super.getCause();
} }
} }

View File

@ -1,39 +0,0 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
* @author Andy Clement
* @since 3.0
*/
public class SpelExpressionParserFactory {
public static ExpressionParser getParser() {
return new SpelExpressionParser();
}
/**
* @param configuration configuration bit flags @see SpelExpressionParserConfiguration
* @return an expression parser instance configured appropriately
*/
public static ExpressionParser getParser(int configuration) {
return new SpelExpressionParser(configuration);
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
/**
* Configuration object for the SpEL expression parser.
*
* @author Juergen Hoeller
* @since 3.0
* @see org.springframework.expression.spel.standard.SpelExpressionParser#SpelExpressionParser(SpelParserConfiguration)
*/
public class SpelParserConfiguration {
private final boolean autoGrowNullReferences;
private final boolean autoGrowCollections;
public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections) {
this.autoGrowNullReferences = autoGrowNullReferences;
this.autoGrowCollections = autoGrowCollections;
}
public boolean isAutoGrowNullReferences() {
return this.autoGrowNullReferences;
}
public boolean isAutoGrowCollections() {
return this.autoGrowCollections;
}
}

View File

@ -27,8 +27,6 @@ import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException; import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage; import org.springframework.expression.spel.SpelMessage;
// TODO support multidimensional arrays
// TODO support correct syntax for multidimensional [][][] and not [,,,]
/** /**
* An Indexer can index into some proceeding structure to access a particular piece of it. Supported structures are: * An Indexer can index into some proceeding structure to access a particular piece of it. Supported structures are:
* strings/collections (lists/sets)/arrays * strings/collections (lists/sets)/arrays
@ -36,10 +34,12 @@ import org.springframework.expression.spel.SpelMessage;
* @author Andy Clement * @author Andy Clement
* @since 3.0 * @since 3.0
*/ */
// TODO support multidimensional arrays
// TODO support correct syntax for multidimensional [][][] and not [,,,]
public class Indexer extends SpelNodeImpl { public class Indexer extends SpelNodeImpl {
public Indexer(int pos,SpelNodeImpl expr) { public Indexer(int pos, SpelNodeImpl expr) {
super(pos,expr); super(pos, expr);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -48,7 +48,7 @@ public class Indexer extends SpelNodeImpl {
TypedValue context = state.getActiveContextObject(); TypedValue context = state.getActiveContextObject();
Object targetObject = context.getValue(); Object targetObject = context.getValue();
TypeDescriptor targetObjectTypeDescriptor = context.getTypeDescriptor(); TypeDescriptor targetObjectTypeDescriptor = context.getTypeDescriptor();
TypedValue indexValue = null; TypedValue indexValue = null;
Object index = null; Object index = null;
// This first part of the if clause prevents a 'double dereference' of the property (SPR-5847) // This first part of the if clause prevents a 'double dereference' of the property (SPR-5847)
@ -56,14 +56,16 @@ public class Indexer extends SpelNodeImpl {
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.valueOf(String.class));
} 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
// temporarily push that on whilst evaluating the key // temporarily push that on whilst evaluating the key
try { try {
state.pushActiveContextObject(state.getRootContextObject()); state.pushActiveContextObject(state.getRootContextObject());
indexValue = children[0].getValueInternal(state); indexValue = children[0].getValueInternal(state);
index = indexValue.getValue(); index = indexValue.getValue();
} finally { }
finally {
state.popActiveContextObject(); state.popActiveContextObject();
} }
} }
@ -100,7 +102,7 @@ public class Indexer extends SpelNodeImpl {
} else if (targetObject instanceof Collection) { } else if (targetObject instanceof Collection) {
Collection c = (Collection) targetObject; Collection c = (Collection) targetObject;
if (idx >= c.size()) { if (idx >= c.size()) {
if (state.configuredToGrowCollection()) { if (state.getConfiguration().isAutoGrowCollections()) {
// Grow the collection // Grow the collection
Object newCollectionElement = null; Object newCollectionElement = null;
try { try {
@ -114,14 +116,14 @@ public class Indexer extends SpelNodeImpl {
newElements--; newElements--;
} }
newCollectionElement = targetObjectTypeDescriptor.getElementType().newInstance(); newCollectionElement = targetObjectTypeDescriptor.getElementType().newInstance();
} catch (InstantiationException e) { }
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_GROW_COLLECTION); catch (Exception ex) {
} catch (IllegalAccessException e) { throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION);
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_GROW_COLLECTION);
} }
c.add(newCollectionElement); c.add(newCollectionElement);
return new TypedValue(newCollectionElement,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType())); return new TypedValue(newCollectionElement,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
} else { }
else {
throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx); throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx);
} }
} }
@ -175,7 +177,8 @@ public class Indexer extends SpelNodeImpl {
if (targetObjectTypeDescriptor.isArray()) { if (targetObjectTypeDescriptor.isArray()) {
int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR); int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR);
setArrayElement(state, contextObject.getValue(), idx, newValue, targetObjectTypeDescriptor.getElementType()); setArrayElement(state, contextObject.getValue(), idx, newValue, targetObjectTypeDescriptor.getElementType());
} else if (targetObjectTypeDescriptor.isCollection()) { }
else if (targetObjectTypeDescriptor.isCollection()) {
int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR); int idx = (Integer)state.convertValue(index, INTEGER_TYPE_DESCRIPTOR);
Collection c = (Collection) targetObject; Collection c = (Collection) targetObject;
if (idx >= c.size()) { if (idx >= c.size()) {
@ -185,7 +188,8 @@ public class Indexer extends SpelNodeImpl {
List list = (List)targetObject; List list = (List)targetObject;
Object possiblyConvertedValue = state.convertValue(newValue,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType())); Object possiblyConvertedValue = state.convertValue(newValue,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
list.set(idx,possiblyConvertedValue); list.set(idx,possiblyConvertedValue);
} else { }
else {
throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, contextObject.getClass().getName()); throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, contextObject.getClass().getName());
} }
} else { } else {
@ -247,7 +251,6 @@ public class Indexer extends SpelNodeImpl {
} }
} }
private Object accessArrayElement(Object ctx, int idx) throws SpelEvaluationException { private Object accessArrayElement(Object ctx, int idx) throws SpelEvaluationException {
Class<?> arrayComponentType = ctx.getClass().getComponentType(); Class<?> arrayComponentType = ctx.getClass().getComponentType();
if (arrayComponentType == Integer.TYPE) { if (arrayComponentType == Integer.TYPE) {
@ -289,7 +292,6 @@ public class Indexer extends SpelNodeImpl {
} }
} }
private void checkAccess(int arrayLength, int index) throws SpelEvaluationException { private void checkAccess(int arrayLength, int index) throws SpelEvaluationException {
if (index > arrayLength) { if (index > arrayLength) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS, arrayLength, index); throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS, arrayLength, index);

View File

@ -21,7 +21,7 @@ import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException; import org.springframework.expression.spel.SpelEvaluationException;
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.expression.spel.standard.InternalParseException; import org.springframework.expression.spel.InternalParseException;
/** /**
* Common superclass for nodes representing literals (boolean, string, number, etc). * Common superclass for nodes representing literals (boolean, string, number, etc).

View File

@ -51,7 +51,7 @@ public class OpOr extends Operator {
} }
if (leftValue == true) { if (leftValue == true) {
return BooleanTypedValue.True; // no need to evaluate right operand return BooleanTypedValue.TRUE; // no need to evaluate right operand
} }
try { try {

View File

@ -50,7 +50,7 @@ public class OperatorInstanceof extends Operator {
Object leftValue = left.getValue(); Object leftValue = left.getValue();
Object rightValue = right.getValue(); Object rightValue = right.getValue();
if (leftValue == null) { if (leftValue == null) {
return BooleanTypedValue.False; // null is not an instanceof anything return BooleanTypedValue.FALSE; // null is not an instanceof anything
} }
if (rightValue == null || !(rightValue instanceof Class<?>)) { if (rightValue == null || !(rightValue instanceof Class<?>)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(), throw new SpelEvaluationException(getRightOperand().getStartPosition(),

View File

@ -30,7 +30,7 @@ import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState; import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException; import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage; import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.support.ReflectivePropertyResolver; import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
/** /**
* Represents a simple property or field reference. * Represents a simple property or field reference.
@ -60,7 +60,8 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
TypedValue result = readProperty(state, this.name); TypedValue result = readProperty(state, this.name);
// Dynamically create the objects if the user has requested that optional behaviour // Dynamically create the objects if the user has requested that optional behaviour
if (result.getValue()==null && state.configuredToDynamicallyCreateNullObjects() && nextChildIs(Indexer.class,PropertyOrFieldReference.class)) { if (result.getValue() == null && state.getConfiguration().isAutoGrowNullReferences() &&
nextChildIs(Indexer.class, PropertyOrFieldReference.class)) {
TypeDescriptor resultDescriptor = result.getTypeDescriptor(); TypeDescriptor resultDescriptor = result.getTypeDescriptor();
// Creating lists and maps // Creating lists and maps
if ((resultDescriptor.getType().equals(List.class) || resultDescriptor.getType().equals(Map.class))) { if ((resultDescriptor.getType().equals(List.class) || resultDescriptor.getType().equals(Map.class))) {
@ -161,8 +162,8 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
try { try {
for (PropertyAccessor accessor : accessorsToTry) { for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canRead(eContext, contextObject.getValue(), name)) { if (accessor.canRead(eContext, contextObject.getValue(), name)) {
if (accessor instanceof ReflectivePropertyResolver) { if (accessor instanceof ReflectivePropertyAccessor) {
accessor = ((ReflectivePropertyResolver)accessor).createOptimalAccessor(eContext, contextObject.getValue(), name); accessor = ((ReflectivePropertyAccessor)accessor).createOptimalAccessor(eContext, contextObject.getValue(), name);
} }
this.cachedReadAccessor = accessor; this.cachedReadAccessor = accessor;
return accessor.read(eContext, contextObject.getValue(), name); return accessor.read(eContext, contextObject.getValue(), name);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2008-2009 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,8 @@
* 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.spel.standard.internal;
package org.springframework.expression.spel.standard;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,9 +23,10 @@ import java.util.Stack;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.ParserContext; import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateAwareExpressionParser; import org.springframework.expression.common.TemplateAwareExpressionParser;
import org.springframework.expression.spel.SpelExpression; 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.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.ast.Assign; import org.springframework.expression.spel.ast.Assign;
import org.springframework.expression.spel.ast.BooleanLiteral; import org.springframework.expression.spel.ast.BooleanLiteral;
import org.springframework.expression.spel.ast.CompoundExpression; import org.springframework.expression.spel.ast.CompoundExpression;
@ -62,16 +64,14 @@ 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.expression.spel.standard.InternalParseException;
import org.springframework.expression.spel.standard.SpelExpressionParserConfiguration;
/** /**
* Hand written SpEL parser. Instances are reusable but are not thread safe. * Hand written SpEL parser. Instances are reusable but are not thread safe.
* *
* @author Andy Clement * @author Andy Clement
* @since 3.0 * @since 3.0
*/ */
public class InternalSpelExpressionParser extends TemplateAwareExpressionParser { class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
// The expression being parsed // The expression being parsed
private String expressionString; private String expressionString;
@ -88,30 +88,20 @@ public class InternalSpelExpressionParser extends TemplateAwareExpressionParser
// For rules that build nodes, they are stacked here for return // For rules that build nodes, they are stacked here for return
private Stack<SpelNodeImpl> constructedNodes = new Stack<SpelNodeImpl>(); private Stack<SpelNodeImpl> constructedNodes = new Stack<SpelNodeImpl>();
private int configuration; private SpelParserConfiguration configuration;
/** /**
* Create a parser. * Create a parser with some configured behavior.
* @param configuration custom configuration options
*/ */
public InternalSpelExpressionParser() { public InternalSpelExpressionParser(SpelParserConfiguration configuration) {
this(0);
}
/**
* Create a parser with some configured behaviour. Supported configuration
* bit flags can be seen in {@link SpelExpressionParserConfiguration}
* @param configuration bitflags for configuration options
*/
public InternalSpelExpressionParser(int configuration) {
this.configuration = configuration; this.configuration = configuration;
} }
public SpelExpression parse(String expressionString) throws ParseException {
return doParseExpression(expressionString, null);
}
@Override @Override
public SpelExpression doParseExpression(String expressionString, ParserContext context) throws ParseException { protected SpelExpression doParseExpression(String expressionString, ParserContext context) throws ParseException {
try { try {
this.expressionString = expressionString; this.expressionString = expressionString;
Tokenizer tokenizer = new Tokenizer(expressionString); Tokenizer tokenizer = new Tokenizer(expressionString);
@ -126,7 +116,8 @@ public class InternalSpelExpressionParser extends TemplateAwareExpressionParser
} }
assert constructedNodes.isEmpty(); assert constructedNodes.isEmpty();
return new SpelExpression(expressionString, ast, configuration); return new SpelExpression(expressionString, ast, configuration);
} catch (InternalParseException ipe) { }
catch (InternalParseException ipe) {
throw ipe.getCause(); throw ipe.getCause();
} }
} }

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.expression.spel; package org.springframework.expression.spel.standard;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
@ -22,6 +22,9 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.common.ExpressionUtils; import org.springframework.expression.common.ExpressionUtils;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.ast.SpelNodeImpl; import org.springframework.expression.spel.ast.SpelNodeImpl;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -38,22 +41,24 @@ public class SpelExpression implements Expression {
private final String expression; private final String expression;
private final SpelNodeImpl ast;
private final SpelParserConfiguration configuration;
// the default context is used if no override is supplied by the user // the default context is used if no override is supplied by the user
private transient EvaluationContext defaultContext; private EvaluationContext defaultContext;
public final SpelNodeImpl ast;
public final int configuration;
/** /**
* Construct an expression, only used by the parser. * Construct an expression, only used by the parser.
*/ */
public SpelExpression(String expression, SpelNodeImpl ast, int configuration) { public SpelExpression(String expression, SpelNodeImpl ast, SpelParserConfiguration configuration) {
this.expression = expression; this.expression = expression;
this.ast = ast; this.ast = ast;
this.configuration = configuration; this.configuration = configuration;
} }
// implementing Expression // implementing Expression
public Object getValue() throws EvaluationException { public Object getValue() throws EvaluationException {
@ -203,7 +208,6 @@ public class SpelExpression implements Expression {
* Produce a string representation of the Abstract Syntax Tree for the expression, this should ideally look like the * Produce a string representation of the Abstract Syntax Tree for the expression, this should ideally look like the
* input expression, but properly formatted since any unnecessary whitespace will have been discarded during the * input expression, but properly formatted since any unnecessary whitespace will have been discarded during the
* parse of the expression. * parse of the expression.
*
* @return the string representation of the AST * @return the string representation of the AST
*/ */
public String toStringAST() { public String toStringAST() {
@ -215,7 +219,7 @@ public class SpelExpression implements Expression {
* @return the default evaluation context * @return the default evaluation context
*/ */
public EvaluationContext getEvaluationContext() { public EvaluationContext getEvaluationContext() {
if (defaultContext==null) { if (defaultContext == null) {
defaultContext = new StandardEvaluationContext(); defaultContext = new StandardEvaluationContext();
} }
return defaultContext; return defaultContext;
@ -223,7 +227,6 @@ public class SpelExpression implements Expression {
/** /**
* Set the evaluation context that will be used if none is specified on an evaluation call. * Set the evaluation context that will be used if none is specified on an evaluation call.
*
* @param context an evaluation context * @param context an evaluation context
*/ */
public void setEvaluationContext(EvaluationContext context) { public void setEvaluationContext(EvaluationContext context) {
@ -231,9 +234,10 @@ public class SpelExpression implements Expression {
} }
private TypedValue toTypedValue(Object object) { private TypedValue toTypedValue(Object object) {
if (object==null) { if (object == null) {
return TypedValue.NULL_TYPED_VALUE; return TypedValue.NULL_TYPED_VALUE;
} else { }
else {
return new TypedValue(object); return new TypedValue(object);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2008-2009 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,48 +13,51 @@
* 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.spel.standard; package org.springframework.expression.spel.standard;
import org.springframework.expression.Expression;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.ParserContext; import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateAwareExpressionParser; import org.springframework.expression.common.TemplateAwareExpressionParser;
import org.springframework.expression.spel.SpelExpression; import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.internal.InternalSpelExpressionParser; import org.springframework.util.Assert;
/** /**
* SpEL parser. Instances are reusable and thread safe. * SpEL parser. Instances are reusable and thread-safe.
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public class SpelExpressionParser extends TemplateAwareExpressionParser { public class SpelExpressionParser extends TemplateAwareExpressionParser {
private int configuration; private final SpelParserConfiguration configuration;
/** /**
* Create a parser with some configured behaviour. Supported configuration * Create a parser with standard configuration.
* bit flags can be seen in {@link SpelExpressionParserConfiguration}
* @param configuration bitflags for configuration options
*/ */
public SpelExpressionParser(int configuration) { public SpelExpressionParser() {
this.configuration = new SpelParserConfiguration(false, false);
}
/**
* Create a parser with some configured behavior.
* @param configuration custom configuration options
*/
public SpelExpressionParser(SpelParserConfiguration configuration) {
Assert.notNull(configuration, "SpelParserConfiguration must not be null");
this.configuration = configuration; this.configuration = configuration;
} }
/**
* Create a parser with default behaviour.
*/
public SpelExpressionParser() {
this(0);
}
@Override @Override
protected Expression doParseExpression(String expressionString, ParserContext context) throws ParseException { protected SpelExpression doParseExpression(String expressionString, ParserContext context) throws ParseException {
return new InternalSpelExpressionParser(configuration).doParseExpression(expressionString, context); return new InternalSpelExpressionParser(this.configuration).doParseExpression(expressionString, context);
} }
public SpelExpression parse(String expressionString) throws ParseException { public SpelExpression parseRaw(String expressionString) throws ParseException {
return new InternalSpelExpressionParser(configuration).parse(expressionString); return doParseExpression(expressionString, null);
} }
} }

View File

@ -1,40 +0,0 @@
/*
* Copyright 2008-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.standard;
/**
* Bit flags that configure optional behaviour in the parser. Pass the necessary
* bits when calling the expression parser constructor.
*
* @author Andy Clement
* @since 3.0
*/
public interface SpelExpressionParserConfiguration {
/**
* This option applies to maps/collections and regular objects. If the initial part of an expression evaluates to null and then an
* attempt is made to resolve an index '[]' or property against it, and this option is set, then the relevant object will be constructed so that
* the index/property resolution can proceed.
*/
static final int CreateObjectIfAttemptToReferenceNull = 0x0001;
/**
* This option allows collections to grow if an attempt is made to index an element beyond the current size. Rather than fail the
* collection is populated with elements up to the specified index.
*/
static final int GrowListsOnIndexBeyondSize = 0x0002;
}

View File

@ -13,7 +13,8 @@
* 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.spel.standard.internal;
package org.springframework.expression.spel.standard;
/** /**
* Holder for a kind of token, the associated data and its position in the input data stream (start/end). * Holder for a kind of token, the associated data and its position in the input data stream (start/end).

View File

@ -13,7 +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.spel.standard.internal; package org.springframework.expression.spel.standard;
/** /**
* @author Andy Clement * @author Andy Clement

View File

@ -13,15 +13,16 @@
* 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.spel.standard.internal;
package org.springframework.expression.spel.standard;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
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.expression.spel.standard.InternalParseException;
/** /**
* 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.
@ -29,7 +30,7 @@ import org.springframework.expression.spel.standard.InternalParseException;
* @author Andy Clement * @author Andy Clement
* @since 3.0 * @since 3.0
*/ */
public class Tokenizer { class Tokenizer {
String expressionString; String expressionString;
char[] toProcess; char[] toProcess;

View File

@ -16,8 +16,8 @@
package org.springframework.expression.spel.support; package org.springframework.expression.spel.support;
import org.springframework.expression.TypedValue;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.TypedValue;
/** /**
* @author Andy Clement * @author Andy Clement
@ -25,18 +25,23 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
public class BooleanTypedValue extends TypedValue { public class BooleanTypedValue extends TypedValue {
public static final BooleanTypedValue True = new BooleanTypedValue(true); public static final BooleanTypedValue TRUE = new BooleanTypedValue(true);
public static final BooleanTypedValue False = new BooleanTypedValue(false);
public static final BooleanTypedValue FALSE = new BooleanTypedValue(false);
private BooleanTypedValue(boolean b) { private BooleanTypedValue(boolean b) {
super(b, TypeDescriptor.valueOf(Boolean.class)); super(b, TypeDescriptor.valueOf(Boolean.class));
} }
public static BooleanTypedValue forValue(boolean b) { public static BooleanTypedValue forValue(boolean b) {
if (b) { if (b) {
return True; return TRUE;
} else { }
return False; else {
return FALSE;
} }
} }
} }

View File

@ -35,14 +35,14 @@ import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Simple PropertyResolver that uses reflection to access properties for reading and writing. A property can be accessed * Simple PropertyAccessor that uses reflection to access properties for reading and writing. A property can be accessed
* if it is accessible as a field on the object or through a getter (if being read) or a setter (if being written). * if it is accessible as a field on the object or through a getter (if being read) or a setter (if being written).
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public class ReflectivePropertyResolver implements PropertyAccessor { public class ReflectivePropertyAccessor implements PropertyAccessor {
protected Map<CacheKey, InvokerPair> readerCache; protected Map<CacheKey, InvokerPair> readerCache;

View File

@ -137,7 +137,7 @@ public class StandardEvaluationContext implements EvaluationContext {
private void ensurePropertyAccessorsInitialized() { private void ensurePropertyAccessorsInitialized() {
if (this.propertyAccessors == null) { if (this.propertyAccessors == null) {
this.propertyAccessors = new ArrayList<PropertyAccessor>(); this.propertyAccessors = new ArrayList<PropertyAccessor>();
this.propertyAccessors.add(new ReflectivePropertyResolver()); this.propertyAccessors.add(new ReflectivePropertyAccessor());
} }
} }

View File

@ -20,15 +20,15 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParserConfiguration;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator; import org.springframework.expression.spel.support.StandardTypeLocator;
@ -43,7 +43,7 @@ public class EvaluationTests extends ExpressionTestCase {
@Test @Test
public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException { public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
ExpressionParser parser = new SpelExpressionParser(SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull | SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("list[0]"); Expression expression = parser.parseExpression("list[0]");
TestClass testClass = new TestClass(); TestClass testClass = new TestClass();
Object o = null; Object o = null;
@ -67,7 +67,7 @@ public class EvaluationTests extends ExpressionTestCase {
public void testCreateMapsOnAttemptToIndexNull01() throws EvaluationException, ParseException { public void testCreateMapsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
TestClass testClass = new TestClass(); TestClass testClass = new TestClass();
StandardEvaluationContext ctx = new StandardEvaluationContext(testClass); StandardEvaluationContext ctx = new StandardEvaluationContext(testClass);
ExpressionParser parser = new SpelExpressionParser(SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull | SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Object o = null; Object o = null;
o = parser.parseExpression("map['a']").getValue(ctx); o = parser.parseExpression("map['a']").getValue(ctx);
Assert.assertNull(o); Assert.assertNull(o);
@ -87,7 +87,7 @@ public class EvaluationTests extends ExpressionTestCase {
public void testCreateObjectsOnAttemptToReferenceNull() throws EvaluationException, ParseException { public void testCreateObjectsOnAttemptToReferenceNull() throws EvaluationException, ParseException {
TestClass testClass = new TestClass(); TestClass testClass = new TestClass();
StandardEvaluationContext ctx = new StandardEvaluationContext(testClass); StandardEvaluationContext ctx = new StandardEvaluationContext(testClass);
ExpressionParser parser = new SpelExpressionParser(SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull | SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Object o = null; Object o = null;
o = parser.parseExpression("wibble.bar").getValue(ctx); o = parser.parseExpression("wibble.bar").getValue(ctx);
Assert.assertEquals("hello",o); Assert.assertEquals("hello",o);
@ -236,7 +236,7 @@ public class EvaluationTests extends ExpressionTestCase {
@Test @Test
public void testPropertiesNested03() throws ParseException { public void testPropertiesNested03() throws ParseException {
try { try {
new SpelExpressionParser().parse("placeOfBirth.23"); new SpelExpressionParser().parseRaw("placeOfBirth.23");
Assert.fail(); Assert.fail();
} catch (SpelParseException spe) { } catch (SpelParseException spe) {
Assert.assertEquals(spe.getMessageCode(), SpelMessage.UNEXPECTED_DATA_AFTER_DOT); Assert.assertEquals(spe.getMessageCode(), SpelMessage.UNEXPECTED_DATA_AFTER_DOT);
@ -507,4 +507,5 @@ public class EvaluationTests extends ExpressionTestCase {
Class stringClass = parser.parseExpression("T(String)").getValue(Class.class); Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);
Assert.assertEquals(String.class,stringClass); Assert.assertEquals(String.class,stringClass);
} }
} }

View File

@ -71,7 +71,7 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
// Create a parser // Create a parser
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
// Parse an expression // Parse an expression
Expression expr = parser.parseExpression("new String('hello world')"); Expression expr = parser.parseRaw("new String('hello world')");
// Evaluate it using a 'standard' context // Evaluate it using a 'standard' context
Object value = expr.getValue(); Object value = expr.getValue();
// They are reusable // They are reusable
@ -102,16 +102,16 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
primes.addAll(Arrays.asList(2,3,5,7,11,13,17)); primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
ctx.setVariable("primes",primes); ctx.setVariable("primes",primes);
Expression expr = parser.parseExpression("#favouriteColour"); Expression expr = parser.parseRaw("#favouriteColour");
Object value = expr.getValue(ctx); Object value = expr.getValue(ctx);
Assert.assertEquals("blue", value); Assert.assertEquals("blue", value);
expr = parser.parseExpression("#primes.get(1)"); expr = parser.parseRaw("#primes.get(1)");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals(3, value); Assert.assertEquals(3, value);
// all prime numbers > 10 from the list (using selection ?{...}) // all prime numbers > 10 from the list (using selection ?{...})
expr = parser.parseExpression("#primes.?[#this>10]"); expr = parser.parseRaw("#primes.?[#this>10]");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals("[11, 13, 17]", value.toString()); Assert.assertEquals("[11, 13, 17]", value.toString());
} }
@ -140,30 +140,30 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
ctx.setRootObject(tc); ctx.setRootObject(tc);
// read it, set it, read it again // read it, set it, read it again
Expression expr = parser.parseExpression("str"); Expression expr = parser.parseRaw("str");
Object value = expr.getValue(ctx); Object value = expr.getValue(ctx);
Assert.assertEquals("wibble", value); Assert.assertEquals("wibble", value);
expr = parser.parseExpression("str"); expr = parser.parseRaw("str");
expr.setValue(ctx, "wobble"); expr.setValue(ctx, "wobble");
expr = parser.parseExpression("str"); expr = parser.parseRaw("str");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals("wobble", value); Assert.assertEquals("wobble", value);
// or using assignment within the expression // or using assignment within the expression
expr = parser.parseExpression("str='wabble'"); expr = parser.parseRaw("str='wabble'");
value = expr.getValue(ctx); value = expr.getValue(ctx);
expr = parser.parseExpression("str"); expr = parser.parseRaw("str");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals("wabble", value); Assert.assertEquals("wabble", value);
// private property will be accessed through getter() // private property will be accessed through getter()
expr = parser.parseExpression("property"); expr = parser.parseRaw("property");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals(42, value); Assert.assertEquals(42, value);
// ... and set through setter // ... and set through setter
expr = parser.parseExpression("property=4"); expr = parser.parseRaw("property=4");
value = expr.getValue(ctx); value = expr.getValue(ctx);
expr = parser.parseExpression("property"); expr = parser.parseRaw("property");
value = expr.getValue(ctx); value = expr.getValue(ctx);
Assert.assertEquals(4,value); Assert.assertEquals(4,value);
} }
@ -182,7 +182,7 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
StandardEvaluationContext ctx = new StandardEvaluationContext(); StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class)); ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class));
Expression expr = parser.parseExpression("#repeat('hello')"); Expression expr = parser.parseRaw("#repeat('hello')");
Object value = expr.getValue(ctx); Object value = expr.getValue(ctx);
Assert.assertEquals("hellohello", value); Assert.assertEquals("hellohello", value);
@ -206,7 +206,7 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
StandardEvaluationContext ctx = new StandardEvaluationContext(); StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new FruitColourAccessor()); ctx.addPropertyAccessor(new FruitColourAccessor());
Expression expr = parser.parseExpression("orange"); Expression expr = parser.parseRaw("orange");
Object value = expr.getValue(ctx); Object value = expr.getValue(ctx);
Assert.assertEquals(Color.orange, value); Assert.assertEquals(Color.orange, value);
@ -226,7 +226,7 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
StandardEvaluationContext ctx = new StandardEvaluationContext(); StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new VegetableColourAccessor()); ctx.addPropertyAccessor(new VegetableColourAccessor());
Expression expr = parser.parseExpression("pea"); Expression expr = parser.parseRaw("pea");
Object value = expr.getValue(ctx); Object value = expr.getValue(ctx);
Assert.assertEquals(Color.green, value); Assert.assertEquals(Color.green, value);

View File

@ -25,6 +25,7 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
/** /**
@ -39,7 +40,7 @@ public abstract class ExpressionTestCase {
protected final static boolean SHOULD_BE_WRITABLE = true; protected final static boolean SHOULD_BE_WRITABLE = true;
protected final static boolean SHOULD_NOT_BE_WRITABLE = false; protected final static boolean SHOULD_NOT_BE_WRITABLE = false;
protected final static ExpressionParser parser = SpelExpressionParserFactory.getParser(); protected final static ExpressionParser parser = new SpelExpressionParser();
protected final static StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); protected final static StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
/** /**

View File

@ -30,10 +30,11 @@ import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ast.FormatHelper; import org.springframework.expression.spel.ast.FormatHelper;
import org.springframework.expression.spel.support.ReflectionHelper; import org.springframework.expression.spel.support.ReflectionHelper;
import org.springframework.expression.spel.support.ReflectivePropertyResolver; import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeConverter; import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.expression.spel.support.ReflectionHelper.ArgsMatchKind; import org.springframework.expression.spel.support.ReflectionHelper.ArgsMatchKind;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Tests for any helper code. * Tests for any helper code.
@ -274,7 +275,7 @@ public class HelperTests extends ExpressionTestCase {
@Test @Test
public void testReflectivePropertyResolver() throws Exception { public void testReflectivePropertyResolver() throws Exception {
ReflectivePropertyResolver rpr = new ReflectivePropertyResolver(); ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
Tester t = new Tester(); Tester t = new Tester();
t.setProperty("hello"); t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t); EvaluationContext ctx = new StandardEvaluationContext(t);
@ -313,7 +314,7 @@ public class HelperTests extends ExpressionTestCase {
@Test @Test
public void testOptimalReflectivePropertyResolver() throws Exception { public void testOptimalReflectivePropertyResolver() throws Exception {
ReflectivePropertyResolver rpr = new ReflectivePropertyResolver(); ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
Tester t = new Tester(); Tester t = new Tester();
t.setProperty("hello"); t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t); EvaluationContext ctx = new StandardEvaluationContext(t);

View File

@ -21,6 +21,7 @@ import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* These are tests for language features that are not yet considered 'live'. Either missing implementation or documentation. * These are tests for language features that are not yet considered 'live'. Either missing implementation or documentation.

View File

@ -20,6 +20,7 @@ import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Tests the evaluation of basic literals: boolean, integer, hex integer, long, real, null, date * Tests the evaluation of basic literals: boolean, integer, hex integer, long, real, null, date

View File

@ -23,6 +23,7 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.Operation; import org.springframework.expression.Operation;
import org.springframework.expression.OperatorOverloader; import org.springframework.expression.OperatorOverloader;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Test providing operator support * Test providing operator support

View File

@ -20,6 +20,7 @@ import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.spel.ast.Operator; import org.springframework.expression.spel.ast.Operator;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Tests the evaluation of expressions using relational operators. * Tests the evaluation of expressions using relational operators.

View File

@ -21,6 +21,7 @@ import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Parse some expressions and check we get the AST we expect. Rather than inspecting each node in the AST, we ask it to * Parse some expressions and check we get the AST we expect. Rather than inspecting each node in the AST, we ask it to
@ -451,7 +452,7 @@ public class ParsingTests {
*/ */
public void parseCheck(String expression, String expectedStringFormOfAST) { public void parseCheck(String expression, String expectedStringFormOfAST) {
try { try {
SpelExpression e = (SpelExpression) parser.parseExpression(expression); SpelExpression e = (SpelExpression) parser.parseRaw(expression);
if (e != null && !e.toStringAST().equals(expectedStringFormOfAST)) { if (e != null && !e.toStringAST().equals(expectedStringFormOfAST)) {
SpelUtilities.printAbstractSyntaxTree(System.err, e); SpelUtilities.printAbstractSyntaxTree(System.err, e);
} }

View File

@ -17,11 +17,12 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
///CLOVER:OFF ///CLOVER:OFF
@ -35,7 +36,7 @@ public class PerformanceTests {
public static final int ITERATIONS = 10000; public static final int ITERATIONS = 10000;
public static final boolean report = true; public static final boolean report = true;
private static ExpressionParser parser = SpelExpressionParserFactory.getParser(); private static ExpressionParser parser = new SpelExpressionParser();
private static EvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); private static EvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
private static final boolean DEBUG = false; private static final boolean DEBUG = false;

View File

@ -27,6 +27,7 @@ import org.springframework.expression.Expression;
import org.springframework.expression.PropertyAccessor; import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
///CLOVER:OFF ///CLOVER:OFF
@ -104,16 +105,16 @@ public class PropertyAccessTests extends ExpressionTestCase {
// names the String class as the type it is interested in so is chosen in preference to // names the String class as the type it is interested in so is chosen in preference to
// any 'default' ones // any 'default' ones
ctx.addPropertyAccessor(new StringyPropertyAccessor()); ctx.addPropertyAccessor(new StringyPropertyAccessor());
Expression expr = parser.parseExpression("new String('hello').flibbles"); Expression expr = parser.parseRaw("new String('hello').flibbles");
Integer i = expr.getValue(ctx, Integer.class); Integer i = expr.getValue(ctx, Integer.class);
Assert.assertEquals((int) i, 7); Assert.assertEquals((int) i, 7);
// The reflection one will be used for other properties... // The reflection one will be used for other properties...
expr = parser.parseExpression("new String('hello').CASE_INSENSITIVE_ORDER"); expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
Object o = expr.getValue(ctx); Object o = expr.getValue(ctx);
Assert.assertNotNull(o); Assert.assertNotNull(o);
expr = parser.parseExpression("new String('hello').flibbles"); expr = parser.parseRaw("new String('hello').flibbles");
expr.setValue(ctx, 99); expr.setValue(ctx, 99);
i = expr.getValue(ctx, Integer.class); i = expr.getValue(ctx, Integer.class);
Assert.assertEquals((int) i, 99); Assert.assertEquals((int) i, 99);

View File

@ -49,7 +49,7 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext(); StandardEvaluationContext ctx = new StandardEvaluationContext();
Expression expr = parser.parseExpression("hasAnyRole('MANAGER','TELLER')"); Expression expr = parser.parseRaw("hasAnyRole('MANAGER','TELLER')");
ctx.setRootObject(new Person("Ben")); ctx.setRootObject(new Person("Ben"));
Boolean value = expr.getValue(ctx,Boolean.class); Boolean value = expr.getValue(ctx,Boolean.class);
@ -74,7 +74,7 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase {
// Multiple options for supporting this expression: "p.name == principal.name" // Multiple options for supporting this expression: "p.name == principal.name"
// (1) If the right person is the root context object then "name==principal.name" is good enough // (1) If the right person is the root context object then "name==principal.name" is good enough
Expression expr = parser.parseExpression("name == principal.name"); Expression expr = parser.parseRaw("name == principal.name");
ctx.setRootObject(new Person("Andy")); ctx.setRootObject(new Person("Andy"));
Boolean value = expr.getValue(ctx,Boolean.class); Boolean value = expr.getValue(ctx,Boolean.class);
@ -85,7 +85,7 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase {
Assert.assertFalse(value); Assert.assertFalse(value);
// (2) Or register an accessor that can understand 'p' and return the right person // (2) Or register an accessor that can understand 'p' and return the right person
expr = parser.parseExpression("p.name == principal.name"); expr = parser.parseRaw("p.name == principal.name");
PersonAccessor pAccessor = new PersonAccessor(); PersonAccessor pAccessor = new PersonAccessor();
ctx.addPropertyAccessor(pAccessor); ctx.addPropertyAccessor(pAccessor);
@ -107,7 +107,7 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase {
// Might be better with a as a variable although it would work as a property too... // Might be better with a as a variable although it would work as a property too...
// Variable references using a '#' // Variable references using a '#'
Expression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Expression expr = parser.parseRaw("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Boolean value = null; Boolean value = null;
@ -134,7 +134,7 @@ public class ScenariosForSpringSecurity extends ExpressionTestCase {
// Might be better with a as a variable although it would work as a property too... // Might be better with a as a variable although it would work as a property too...
// Variable references using a '#' // Variable references using a '#'
// SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); // SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Expression expr = parser.parseExpression("(hasRole(3) or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Expression expr = parser.parseRaw("(hasRole(3) or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Boolean value = null; Boolean value = null;

View File

@ -38,7 +38,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectionWithList() throws Exception { public void selectionWithList() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.?[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ListTestBean()); EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof List); assertTrue(value instanceof List);
@ -53,7 +53,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectFirstItemInList() throws Exception { public void selectFirstItemInList() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.^[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ListTestBean()); EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -62,7 +62,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectLastItemInList() throws Exception { public void selectLastItemInList() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.$[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ListTestBean()); EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -71,7 +71,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectionWithArray() throws Exception { public void selectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.?[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value.getClass().isArray()); assertTrue(value.getClass().isArray());
@ -88,7 +88,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectFirstItemInArray() throws Exception { public void selectFirstItemInArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.^[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -97,7 +97,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectLastItemInArray() throws Exception { public void selectLastItemInArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("integers.$[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -106,7 +106,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectionWithPrimitiveArray() throws Exception { public void selectionWithPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("ints.?[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("ints.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value.getClass().isArray()); assertTrue(value.getClass().isArray());
@ -123,7 +123,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectFirstItemInPrimitiveArray() throws Exception { public void selectFirstItemInPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("ints.^[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("ints.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -132,7 +132,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void selectLastItemInPrimitiveArray() throws Exception { public void selectLastItemInPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("ints.$[#this<5]"); Expression expression = new SpelExpressionParser().parseRaw("ints.$[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context); Object value = expression.getValue(context);
assertTrue(value instanceof Integer); assertTrue(value instanceof Integer);
@ -141,7 +141,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void projectionWithList() throws Exception { public void projectionWithList() throws Exception {
Expression expression = new SpelExpressionParser().parse("#testList.![wrapper.value]"); Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext(); EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testList", IntegerTestBean.createList()); context.setVariable("testList", IntegerTestBean.createList());
Object value = expression.getValue(context); Object value = expression.getValue(context);
@ -155,7 +155,7 @@ public class SelectionAndProjectionTests {
@Test @Test
public void projectionWithArray() throws Exception { public void projectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parse("#testArray.![wrapper.value]"); Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext(); EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testArray", IntegerTestBean.createArray()); context.setVariable("testArray", IntegerTestBean.createArray());
Object value = expression.getValue(context); Object value = expression.getValue(context);

View File

@ -19,6 +19,7 @@ package org.springframework.expression.spel;
import java.io.PrintStream; import java.io.PrintStream;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpression;
/** /**
* Utilities for working with Spring Expressions. * Utilities for working with Spring Expressions.

View File

@ -32,7 +32,7 @@ import org.springframework.expression.ParserContext;
import org.springframework.expression.PropertyAccessor; import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectivePropertyResolver; import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator; import org.springframework.expression.spel.support.StandardTypeLocator;
@ -56,12 +56,12 @@ public class SpringEL300Tests extends ExpressionTestCase {
@Test @Test
public void testSPR5899() throws Exception { public void testSPR5899() throws Exception {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class()); StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
Expression expr = new SpelExpressionParser().parse("tryToInvokeWithNull(12)"); Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
Assert.assertEquals(12,expr.getValue(eContext)); Assert.assertEquals(12,expr.getValue(eContext));
expr = new SpelExpressionParser().parse("tryToInvokeWithNull(null)"); expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
Assert.assertEquals(null,expr.getValue(eContext)); Assert.assertEquals(null,expr.getValue(eContext));
try { try {
expr = new SpelExpressionParser().parse("tryToInvokeWithNull2(null)"); expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
expr.getValue(); expr.getValue();
Assert.fail("Should have failed to find a method to which it could pass null"); Assert.fail("Should have failed to find a method to which it could pass null");
} catch (EvaluationException see) { } catch (EvaluationException see) {
@ -70,26 +70,26 @@ public class SpringEL300Tests extends ExpressionTestCase {
eContext.setTypeLocator(new MyTypeLocator()); eContext.setTypeLocator(new MyTypeLocator());
// varargs // varargs
expr = new SpelExpressionParser().parse("tryToInvokeWithNull3(null,'a','b')"); expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
Assert.assertEquals("ab",expr.getValue(eContext)); Assert.assertEquals("ab",expr.getValue(eContext));
// varargs 2 - null is packed into the varargs // varargs 2 - null is packed into the varargs
expr = new SpelExpressionParser().parse("tryToInvokeWithNull3(12,'a',null,'c')"); expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
Assert.assertEquals("anullc",expr.getValue(eContext)); Assert.assertEquals("anullc",expr.getValue(eContext));
// check we can find the ctor ok // check we can find the ctor ok
expr = new SpelExpressionParser().parse("new Spr5899Class().toString()"); expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
Assert.assertEquals("instance",expr.getValue(eContext)); Assert.assertEquals("instance",expr.getValue(eContext));
expr = new SpelExpressionParser().parse("new Spr5899Class(null).toString()"); expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
Assert.assertEquals("instance",expr.getValue(eContext)); Assert.assertEquals("instance",expr.getValue(eContext));
// ctor varargs // ctor varargs
expr = new SpelExpressionParser().parse("new Spr5899Class(null,'a','b').toString()"); expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
Assert.assertEquals("instance",expr.getValue(eContext)); Assert.assertEquals("instance",expr.getValue(eContext));
// ctor varargs 2 // ctor varargs 2
expr = new SpelExpressionParser().parse("new Spr5899Class(null,'a', null, 'b').toString()"); expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
Assert.assertEquals("instance",expr.getValue(eContext)); Assert.assertEquals("instance",expr.getValue(eContext));
} }
@ -133,13 +133,13 @@ public class SpringEL300Tests extends ExpressionTestCase {
@Test @Test
public void testSPR5905_InnerTypeReferences() throws Exception { public void testSPR5905_InnerTypeReferences() throws Exception {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class()); StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
Expression expr = new SpelExpressionParser().parse("T(java.util.Map$Entry)"); Expression expr = new SpelExpressionParser().parseRaw("T(java.util.Map$Entry)");
Assert.assertEquals(Map.Entry.class,expr.getValue(eContext)); Assert.assertEquals(Map.Entry.class,expr.getValue(eContext));
expr = new SpelExpressionParser().parse("T(org.springframework.expression.spel.SpringEL300Tests$Outer$Inner).run()"); expr = new SpelExpressionParser().parseRaw("T(org.springframework.expression.spel.SpringEL300Tests$Outer$Inner).run()");
Assert.assertEquals(12,expr.getValue(eContext)); Assert.assertEquals(12,expr.getValue(eContext));
expr = new SpelExpressionParser().parse("new org.springframework.expression.spel.SpringEL300Tests$Outer$Inner().run2()"); expr = new SpelExpressionParser().parseRaw("new org.springframework.expression.spel.SpringEL300Tests$Outer$Inner().run2()");
Assert.assertEquals(13,expr.getValue(eContext)); Assert.assertEquals(13,expr.getValue(eContext));
} }
@ -162,7 +162,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
m.put("foo", "bar"); m.put("foo", "bar");
StandardEvaluationContext eContext = new StandardEvaluationContext(m); // root is a map instance StandardEvaluationContext eContext = new StandardEvaluationContext(m); // root is a map instance
eContext.addPropertyAccessor(new MapAccessor()); eContext.addPropertyAccessor(new MapAccessor());
Expression expr = new SpelExpressionParser().parseExpression("['foo']"); Expression expr = new SpelExpressionParser().parseRaw("['foo']");
Assert.assertEquals("bar", expr.getValue(eContext)); Assert.assertEquals("bar", expr.getValue(eContext));
} }
@ -172,16 +172,16 @@ public class SpringEL300Tests extends ExpressionTestCase {
String name = null; String name = null;
Expression expr = null; Expression expr = null;
expr = new SpelExpressionParser().parse("jdbcProperties['username']"); expr = new SpelExpressionParser().parseRaw("jdbcProperties['username']");
name = expr.getValue(eContext,String.class); name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave",name); Assert.assertEquals("Dave",name);
expr = new SpelExpressionParser().parse("jdbcProperties[username]"); expr = new SpelExpressionParser().parseRaw("jdbcProperties[username]");
name = expr.getValue(eContext,String.class); name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave",name); Assert.assertEquals("Dave",name);
// MapAccessor required for this to work // MapAccessor required for this to work
expr = new SpelExpressionParser().parse("jdbcProperties.username"); expr = new SpelExpressionParser().parseRaw("jdbcProperties.username");
eContext.addPropertyAccessor(new MapAccessor()); eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class); name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave",name); Assert.assertEquals("Dave",name);
@ -189,13 +189,13 @@ public class SpringEL300Tests extends ExpressionTestCase {
// --- dotted property names // --- dotted property names
// lookup foo on the root, then bar on that, then use that as the key into jdbcProperties // lookup foo on the root, then bar on that, then use that as the key into jdbcProperties
expr = new SpelExpressionParser().parse("jdbcProperties[foo.bar]"); expr = new SpelExpressionParser().parseRaw("jdbcProperties[foo.bar]");
eContext.addPropertyAccessor(new MapAccessor()); eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class); name = expr.getValue(eContext,String.class);
Assert.assertEquals("Dave2",name); Assert.assertEquals("Dave2",name);
// key is foo.bar // key is foo.bar
expr = new SpelExpressionParser().parse("jdbcProperties['foo.bar']"); expr = new SpelExpressionParser().parseRaw("jdbcProperties['foo.bar']");
eContext.addPropertyAccessor(new MapAccessor()); eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext,String.class); name = expr.getValue(eContext,String.class);
Assert.assertEquals("Elephant",name); Assert.assertEquals("Elephant",name);
@ -274,7 +274,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
@Test @Test
public void testAccessingNullPropertyViaReflection_SPR5663() throws AccessException { public void testAccessingNullPropertyViaReflection_SPR5663() throws AccessException {
PropertyAccessor propertyAccessor = new ReflectivePropertyResolver(); PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
Assert.assertFalse(propertyAccessor.canRead(context, null, "abc")); Assert.assertFalse(propertyAccessor.canRead(context, null, "abc"));
Assert.assertFalse(propertyAccessor.canWrite(context, null, "abc")); Assert.assertFalse(propertyAccessor.canWrite(context, null, "abc"));

View File

@ -76,7 +76,7 @@ public class VariableAndFunctionTests extends ExpressionTestCase {
ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic")); ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic"));
try { try {
@SuppressWarnings("unused") @SuppressWarnings("unused")
Object v = parser.parseExpression("#notStatic()").getValue(ctx); Object v = parser.parseRaw("#notStatic()").getValue(ctx);
Assert.fail("Should have failed with exception - cannot call non static method that way"); Assert.fail("Should have failed with exception - cannot call non static method that way");
} catch (SpelEvaluationException se) { } catch (SpelEvaluationException se) {
if (se.getMessageCode() != SpelMessage.FUNCTION_MUST_BE_STATIC) { if (se.getMessageCode() != SpelMessage.FUNCTION_MUST_BE_STATIC) {

View File

@ -13,30 +13,33 @@
* 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.spel.standard.internal;
import junit.framework.Assert;
package org.springframework.expression.spel.standard;
import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionException; import org.springframework.expression.ExpressionException;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.SpelExpression; import org.springframework.expression.spel.standard.SpelExpression;
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.SpelParseException; import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.ast.OpAnd; import org.springframework.expression.spel.ast.OpAnd;
import org.springframework.expression.spel.ast.OpOr; import org.springframework.expression.spel.ast.OpOr;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* @author Andy Clement
*/
public class SpelParserTests { public class SpelParserTests {
@Test @Test
public void theMostBasic() throws EvaluationException,ParseException { public void theMostBasic() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2"); SpelExpression expr = parser.parseRaw("2");
Assert.assertNotNull(expr); Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST()); Assert.assertNotNull(expr.getAST());
Assert.assertEquals(2,expr.getValue()); Assert.assertEquals(2,expr.getValue());
@ -48,35 +51,35 @@ public class SpelParserTests {
public void valueType() throws Exception { public void valueType() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext(); EvaluationContext ctx = new StandardEvaluationContext();
Class c = parser.parse("2").getValueType(); Class c = parser.parseRaw("2").getValueType();
Assert.assertEquals(Integer.class,c); Assert.assertEquals(Integer.class,c);
c = parser.parse("12").getValueType(ctx); c = parser.parseRaw("12").getValueType(ctx);
Assert.assertEquals(Integer.class,c); Assert.assertEquals(Integer.class,c);
c = parser.parse("null").getValueType(); c = parser.parseRaw("null").getValueType();
Assert.assertNull(c); Assert.assertNull(c);
c = parser.parse("null").getValueType(ctx); c = parser.parseRaw("null").getValueType(ctx);
Assert.assertNull(c); Assert.assertNull(c);
Object o = parser.parse("null").getValue(ctx,Integer.class); Object o = parser.parseRaw("null").getValue(ctx,Integer.class);
Assert.assertNull(o); Assert.assertNull(o);
} }
@Test @Test
public void whitespace() throws EvaluationException,ParseException { public void whitespace() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2 + 3"); SpelExpression expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue()); Assert.assertEquals(5,expr.getValue());
expr = parser.parse("2 + 3"); expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue()); Assert.assertEquals(5,expr.getValue());
expr = parser.parse("2\n+ 3"); expr = parser.parseRaw("2\n+ 3");
Assert.assertEquals(5,expr.getValue()); Assert.assertEquals(5,expr.getValue());
expr = parser.parse("2\r\n+\t3"); expr = parser.parseRaw("2\r\n+\t3");
Assert.assertEquals(5,expr.getValue()); Assert.assertEquals(5,expr.getValue());
} }
@Test @Test
public void arithmeticPlus1() throws EvaluationException,ParseException { public void arithmeticPlus1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2+2"); SpelExpression expr = parser.parseRaw("2+2");
Assert.assertNotNull(expr); Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST()); Assert.assertNotNull(expr.getAST());
Assert.assertEquals(4,expr.getValue()); Assert.assertEquals(4,expr.getValue());
@ -85,14 +88,14 @@ public class SpelParserTests {
@Test @Test
public void arithmeticPlus2() throws EvaluationException,ParseException { public void arithmeticPlus2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("37+41"); SpelExpression expr = parser.parseRaw("37+41");
Assert.assertEquals(78,expr.getValue()); Assert.assertEquals(78,expr.getValue());
} }
@Test @Test
public void arithmeticMultiply1() throws EvaluationException,ParseException { public void arithmeticMultiply1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2*3"); SpelExpression expr = parser.parseRaw("2*3");
Assert.assertNotNull(expr); Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST()); Assert.assertNotNull(expr.getAST());
// printAst(expr.getAST(),0); // printAst(expr.getAST(),0);
@ -102,7 +105,7 @@ public class SpelParserTests {
@Test @Test
public void arithmeticPrecedence1() throws EvaluationException,ParseException { public void arithmeticPrecedence1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2*3+5"); SpelExpression expr = parser.parseRaw("2*3+5");
Assert.assertEquals(11,expr.getValue()); Assert.assertEquals(11,expr.getValue());
} }
@ -110,7 +113,7 @@ public class SpelParserTests {
public void generalExpressions() throws Exception { public void generalExpressions() throws Exception {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("new String[3]"); parser.parseRaw("new String[3]");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -120,7 +123,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("new String"); parser.parseRaw("new String");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -130,7 +133,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("new String(3,"); parser.parseRaw("new String(3,");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -140,7 +143,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("new String(3"); parser.parseRaw("new String(3");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -150,7 +153,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("new String("); parser.parseRaw("new String(");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -160,7 +163,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("\"abc"); parser.parseRaw("\"abc");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -170,7 +173,7 @@ public class SpelParserTests {
} }
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse("'abc"); parser.parseRaw("'abc");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);
@ -184,69 +187,69 @@ public class SpelParserTests {
@Test @Test
public void arithmeticPrecedence2() throws EvaluationException,ParseException { public void arithmeticPrecedence2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse("2+3*5"); SpelExpression expr = parser.parseRaw("2+3*5");
Assert.assertEquals(17,expr.getValue()); Assert.assertEquals(17,expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence3() throws EvaluationException,ParseException { public void arithmeticPrecedence3() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("3+10/2"); SpelExpression expr = new SpelExpressionParser().parseRaw("3+10/2");
Assert.assertEquals(8,expr.getValue()); Assert.assertEquals(8,expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence4() throws EvaluationException,ParseException { public void arithmeticPrecedence4() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("10/2+3"); SpelExpression expr = new SpelExpressionParser().parseRaw("10/2+3");
Assert.assertEquals(8,expr.getValue()); Assert.assertEquals(8,expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence5() throws EvaluationException,ParseException { public void arithmeticPrecedence5() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("(4+10)/2"); SpelExpression expr = new SpelExpressionParser().parseRaw("(4+10)/2");
Assert.assertEquals(7,expr.getValue()); Assert.assertEquals(7,expr.getValue());
} }
@Test @Test
public void arithmeticPrecedence6() throws EvaluationException,ParseException { public void arithmeticPrecedence6() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("(3+2)*2"); SpelExpression expr = new SpelExpressionParser().parseRaw("(3+2)*2");
Assert.assertEquals(10,expr.getValue()); Assert.assertEquals(10,expr.getValue());
} }
@Test @Test
public void booleanOperators() throws EvaluationException,ParseException { public void booleanOperators() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("true"); SpelExpression expr = new SpelExpressionParser().parseRaw("true");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("false"); expr = new SpelExpressionParser().parseRaw("false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("false and false"); expr = new SpelExpressionParser().parseRaw("false and false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("true and (true or false)"); expr = new SpelExpressionParser().parseRaw("true and (true or false)");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("true and true or false"); expr = new SpelExpressionParser().parseRaw("true and true or false");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("!true"); expr = new SpelExpressionParser().parseRaw("!true");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parse("!(false or true)"); expr = new SpelExpressionParser().parseRaw("!(false or true)");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class)); Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
} }
@Test @Test
public void testStringLiterals() throws EvaluationException,ParseException { public void testStringLiterals() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("'howdy'"); SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'");
Assert.assertEquals("howdy",expr.getValue()); Assert.assertEquals("howdy",expr.getValue());
expr = new SpelExpressionParser().parse("'hello '' world'"); expr = new SpelExpressionParser().parseRaw("'hello '' world'");
Assert.assertEquals("hello ' world",expr.getValue()); Assert.assertEquals("hello ' world",expr.getValue());
} }
@Test @Test
public void testStringLiterals2() throws EvaluationException,ParseException { public void testStringLiterals2() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parse("'howdy'.substring(0,2)"); SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'.substring(0,2)");
Assert.assertEquals("ho",expr.getValue()); Assert.assertEquals("ho",expr.getValue());
} }
@Test @Test
public void testPositionalInformation() throws EvaluationException, ParseException { public void testPositionalInformation() throws EvaluationException, ParseException {
SpelExpression expr = new SpelExpressionParser().parse("true and true or false"); SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false");
SpelNode rootAst = expr.getAST(); SpelNode rootAst = expr.getAST();
OpOr operatorOr = (OpOr)rootAst; OpOr operatorOr = (OpOr)rootAst;
OpAnd operatorAnd = (OpAnd)operatorOr.getLeftOperand(); OpAnd operatorAnd = (OpAnd)operatorOr.getLeftOperand();
@ -354,7 +357,7 @@ public class SpelParserTests {
private void checkNumber(String expression, Object value, Class<?> type) { private void checkNumber(String expression, Object value, Class<?> type) {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parse(expression); SpelExpression expr = parser.parseRaw(expression);
Object o = expr.getValue(); Object o = expr.getValue();
Assert.assertEquals(value,o); Assert.assertEquals(value,o);
Assert.assertEquals(type,o.getClass()); Assert.assertEquals(type,o.getClass());
@ -367,7 +370,7 @@ public class SpelParserTests {
private void checkNumberError(String expression, SpelMessage expectedMessage) { private void checkNumberError(String expression, SpelMessage expectedMessage) {
try { try {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
parser.parse(expression); parser.parseRaw(expression);
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException); Assert.assertTrue(e instanceof SpelParseException);