el-based message resolution; expected failure right now

This commit is contained in:
Keith Donald
2009-06-13 17:18:12 +00:00
parent 97e7dfb398
commit d0079c6058
9 changed files with 284 additions and 154 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression;
import org.springframework.core.convert.TypeDescriptor;
@@ -69,13 +68,13 @@ public interface Expression {
public <T> T getValue(EvaluationContext context, Class<T> desiredResultType) throws EvaluationException;
/**
* Set this expression in the provided context to the value provided.
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using
* the default context.
*
* @param context the context in which to set the value of the expression
* @param value the new value
* @throws EvaluationException if there is a problem during evaluation
* @return the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type
*/
public void setValue(EvaluationContext context, Object value) throws EvaluationException;
public Class getValueType() throws EvaluationException;
/**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for
@@ -91,10 +90,10 @@ public interface Expression {
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using
* the default context.
*
* @return the most general type of value that can be set on this context
* @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type
*/
public Class getValueType() throws EvaluationException;
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException;
/**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method for
@@ -106,23 +105,6 @@ public interface Expression {
*/
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException;
/**
* Returns the most general type that can be passed to the {@link #setValue(EvaluationContext, Object)} method using
* the default context.
*
* @return a type descriptor for the most general type of value that can be set on this context
* @throws EvaluationException if there is a problem determining the type
*/
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException;
/**
* Returns the original string used to create this expression, unmodified.
*
* @return the original expression string
*/
public String getExpressionString();
/**
* Determine if an expression can be written to, i.e. setValue() can be called.
*
@@ -131,4 +113,21 @@ public interface Expression {
* @throws EvaluationException if there is a problem determining if it is writable
*/
public boolean isWritable(EvaluationContext context) throws EvaluationException;
/**
* Set this expression in the provided context to the value provided.
*
* @param context the context in which to set the value of the expression
* @param value the new value
* @throws EvaluationException if there is a problem during evaluation
*/
public void setValue(EvaluationContext context, Object value) throws EvaluationException;
/**
* Returns the original string used to create this expression, unmodified.
*
* @return the original expression string
*/
public String getExpressionString();
}

View File

@@ -31,8 +31,8 @@ public interface ParserContext {
*
* <pre>
* Some literal text
* Hello ${name.firstName}!
* ${3 + 4}
* Hello #{name.firstName}!
* #{3 + 4}
* </pre>
*
* @return true if the expression is a template, false otherwise
@@ -54,5 +54,26 @@ public interface ParserContext {
* @return the suffix that identifies the end of an expression
*/
String getExpressionSuffix();
/**
* The default ParserContext implementation that enables template expression parsing mode.
* The expression prefix is #{ and the expression suffix is }.
* @see #isTemplate()
*/
public static final ParserContext TEMPLATE_EXPRESSION = new ParserContext() {
public String getExpressionPrefix() {
return "#{";
}
public String getExpressionSuffix() {
return "}";
}
public boolean isTemplate() {
return true;
}
};
}

View File

@@ -31,7 +31,13 @@ public class TemplateParserContext implements ParserContext {
private final String expressionSuffix;
/**
* Creates a new TemplateParserContext with the default #{ prefix and } suffix.
*/
public TemplateParserContext() {
this("#{", "}");
}
/**
* Create a new TemplateParserContext for the given prefix and suffix.
* @param expressionPrefix the expression prefix to use

View File

@@ -22,6 +22,7 @@ import org.springframework.expression.Expression;
import org.springframework.expression.common.ExpressionUtils;
import org.springframework.expression.spel.ast.SpelNodeImpl;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert;
/**
* A SpelExpressions represents a parsed (valid) expression that is ready to be evaluated in a specified context. An
@@ -48,35 +49,27 @@ public class SpelExpression implements Expression {
this.configuration = configuration;
}
/**
* @return the expression string that was parsed to create this expression instance
*/
public String getExpressionString() {
return this.expression;
}
/**
* {@inheritDoc}
*/
// implementing Expression
public Object getValue() throws EvaluationException {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext(),configuration);
return this.ast.getValue(expressionState);
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext(), configuration);
return ast.getValue(expressionState);
}
/**
* {@inheritDoc}
*/
public <T> T getValue(Class<T> expectedResultType) throws EvaluationException {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext(), configuration);
Object result = ast.getValue(expressionState);
return ExpressionUtils.convert(expressionState.getEvaluationContext(), result, expectedResultType);
}
public Object getValue(EvaluationContext context) throws EvaluationException {
return this.ast.getValue(new ExpressionState(context,configuration));
Assert.notNull(context, "The EvaluationContext is required");
return ast.getValue(new ExpressionState(context, configuration));
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public <T> T getValue(EvaluationContext context, Class<T> expectedResultType) throws EvaluationException {
Object result = ast.getValue(new ExpressionState(context,configuration));
Object result = ast.getValue(new ExpressionState(context, configuration));
if (result != null && expectedResultType != null) {
Class<?> resultType = result.getClass();
if (!expectedResultType.isAssignableFrom(resultType)) {
@@ -87,25 +80,49 @@ public class SpelExpression implements Expression {
return (T) result;
}
/**
* {@inheritDoc}
*/
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
this.ast.setValue(new ExpressionState(context,configuration), value);
public Class getValueType() throws EvaluationException {
return ast.getValueInternal(new ExpressionState(new StandardEvaluationContext(), configuration)).getTypeDescriptor().getType();
}
/**
* {@inheritDoc}
*/
public boolean isWritable(EvaluationContext context) throws EvaluationException {
return this.ast.isWritable(new ExpressionState(context,configuration));
public Class getValueType(EvaluationContext context) throws EvaluationException {
Assert.notNull(context, "The EvaluationContext is required");
ExpressionState eState = new ExpressionState(context, configuration);
TypeDescriptor typeDescriptor = ast.getValueInternal(eState).getTypeDescriptor();
return typeDescriptor.getType();
}
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException {
return ast.getValueInternal(new ExpressionState(new StandardEvaluationContext(), configuration)).getTypeDescriptor();
}
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException {
Assert.notNull(context, "The EvaluationContext is required");
ExpressionState eState = new ExpressionState(context, configuration);
TypeDescriptor typeDescriptor = ast.getValueInternal(eState).getTypeDescriptor();
return typeDescriptor;
}
public String getExpressionString() {
return expression;
}
public boolean isWritable(EvaluationContext context) throws EvaluationException {
Assert.notNull(context, "The EvaluationContext is required");
return ast.isWritable(new ExpressionState(context, configuration));
}
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
Assert.notNull(context, "The EvaluationContext is required");
ast.setValue(new ExpressionState(context, configuration), value);
}
// impl only
/**
* @return return the Abstract Syntax Tree for the expression
*/
public SpelNode getAST() {
return this.ast;
return ast;
}
/**
@@ -116,49 +133,7 @@ public class SpelExpression implements Expression {
* @return the string representation of the AST
*/
public String toStringAST() {
return this.ast.toStringAST();
}
/**
* {@inheritDoc}
*/
public Class getValueType(EvaluationContext context) throws EvaluationException {
ExpressionState eState = new ExpressionState(context,configuration);
TypeDescriptor typeDescriptor = this.ast.getValueInternal(eState).getTypeDescriptor();
return typeDescriptor.getType();
}
/**
* {@inheritDoc}
*/
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException {
ExpressionState eState = new ExpressionState(context,configuration);
TypeDescriptor typeDescriptor = this.ast.getValueInternal(eState).getTypeDescriptor();
return typeDescriptor;
}
/**
* {@inheritDoc}
*/
public Class getValueType() throws EvaluationException {
return this.ast.getValueInternal(new ExpressionState(new StandardEvaluationContext(),configuration)).getTypeDescriptor().getType();
}
/**
* {@inheritDoc}
*/
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException {
return this.ast.getValueInternal(new ExpressionState(new StandardEvaluationContext(),configuration)).getTypeDescriptor();
}
/**
* {@inheritDoc}
*/
public <T> T getValue(Class<T> expectedResultType) throws EvaluationException {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext(),configuration);
Object result = this.ast.getValue(expressionState);
return ExpressionUtils.convert(expressionState.getEvaluationContext(), result, expectedResultType);
return ast.toStringAST();
}
}