add EventAttributeELResolver
improved invalid variable ex message
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.core.style.ToStringCreator;
|
||||
public class EvaluationAttempt {
|
||||
|
||||
private Expression expression;
|
||||
|
||||
private Object context;
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,19 +15,17 @@
|
||||
*/
|
||||
package org.springframework.binding.expression;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* Indicates an expression evaluation failed.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class EvaluationException extends NestedRuntimeException {
|
||||
public class EvaluationException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* The evaluation attempt that failed. Transient because an EvaluationAttempt is not serializable.
|
||||
*/
|
||||
private transient EvaluationAttempt evaluationAttempt;
|
||||
private EvaluationAttempt evaluationAttempt;
|
||||
|
||||
/**
|
||||
* Creates a new evaluation exception.
|
||||
@@ -35,7 +33,17 @@ public class EvaluationException extends NestedRuntimeException {
|
||||
* @param cause the underlying cause of this exception
|
||||
*/
|
||||
public EvaluationException(EvaluationAttempt evaluationAttempt, Throwable cause) {
|
||||
super(evaluationAttempt + " failed - make sure the expression is evaluatable in the context provided", cause);
|
||||
this(evaluationAttempt, evaluationAttempt
|
||||
+ " failed - make sure the expression is evaluatable in the context provided", cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new evaluation exception.
|
||||
* @param evaluationAttempt the evaluation attempt that failed
|
||||
* @param cause the underlying cause of this exception
|
||||
*/
|
||||
public EvaluationException(EvaluationAttempt evaluationAttempt, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.evaluationAttempt = evaluationAttempt;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,9 @@ public class ELExpression implements Expression {
|
||||
try {
|
||||
Object result = valueExpression.getValue(ctx);
|
||||
if (result == null && !ctx.isPropertyResolved()) {
|
||||
throw new EvaluationException(new EvaluationAttempt(this, context), null);
|
||||
EvaluationAttempt attempt = new EvaluationAttempt(this, context);
|
||||
throw new EvaluationException(attempt, attempt
|
||||
+ " failed: the expression path did not resolve--is the base variable incorrect?", null);
|
||||
}
|
||||
return result;
|
||||
} catch (javax.el.PropertyNotFoundException e) {
|
||||
@@ -59,7 +61,9 @@ public class ELExpression implements Expression {
|
||||
try {
|
||||
valueExpression.setValue(ctx, value);
|
||||
if (!ctx.isPropertyResolved()) {
|
||||
throw new EvaluationException(new SetValueAttempt(this, context, value), null);
|
||||
SetValueAttempt attempt = new SetValueAttempt(this, context, value);
|
||||
throw new EvaluationException(attempt, attempt
|
||||
+ " failed: the expression path did not resolve--is the base variable incorrect?", null);
|
||||
}
|
||||
} catch (javax.el.PropertyNotFoundException e) {
|
||||
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
|
||||
|
||||
@@ -12,6 +12,7 @@ import javax.el.VariableMapper;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.jboss.el.ExpressionFactoryImpl;
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.binding.expression.ExpressionVariable;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
@@ -70,12 +71,24 @@ public class ELExpressionParserTests extends TestCase {
|
||||
assertEquals(new Long(2), exp.getValue(new TestBean()));
|
||||
}
|
||||
|
||||
public void testParseEvalExpressionWithContextCustomTestBeanResolver() {
|
||||
public void testParseEvalExpressionWithContextCustomELVariableResolver() {
|
||||
String expressionString = "specialProperty";
|
||||
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().evaluate(TestBean.class));
|
||||
assertEquals("Custom resolver resolved this special property!", exp.getValue(new TestBean()));
|
||||
}
|
||||
|
||||
public void testParseBeanEvalExpressionInvalidELVariable() {
|
||||
try {
|
||||
String expressionString = "bogus";
|
||||
Expression exp = parser.parseExpression(expressionString, new FluentParserContext()
|
||||
.evaluate(TestBean.class));
|
||||
exp.getValue(new TestBean());
|
||||
fail("Should have failed");
|
||||
} catch (EvaluationException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseLiteralExpression() {
|
||||
String expressionString = "'value'";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
@@ -181,9 +194,10 @@ public class ELExpressionParserTests extends TestCase {
|
||||
|
||||
public Object getValue(ELContext arg0, Object arg1, Object arg2) {
|
||||
if (arg1 == null && arg2.equals("specialProperty")) {
|
||||
arg0.setPropertyResolved(true);
|
||||
return "Custom resolver resolved this special property!";
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user