null resolution support

This commit is contained in:
Keith Donald
2008-04-11 15:53:31 +00:00
parent ab3d438119
commit a1b336d93d
6 changed files with 30 additions and 67 deletions

View File

@@ -44,9 +44,14 @@ public class ELExpression implements Expression {
try {
Object result = valueExpression.getValue(ctx);
if (result == null && !ctx.isPropertyResolved()) {
EvaluationAttempt attempt = new EvaluationAttempt(this, context);
throw new EvaluationException(attempt, attempt
+ " failed: the expression path did not resolve--is the base variable incorrect?", null);
if (getExpressionString().equals("null")) {
// special case for handling reserved null keyword
return null;
} else {
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) {

View File

@@ -1,60 +0,0 @@
package org.springframework.binding.expression.el;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
/**
* Resolves the special 'null' variable, indicating a null value.
*
* @author Keith Donald
*/
public class NullELResolver extends ELResolver {
private static final String NULL_VARIABLE_NAME = "null";
public Class getCommonPropertyType(ELContext context, Object base) {
return null;
}
public Iterator getFeatureDescriptors(ELContext context, Object base) {
return null;
}
public Class getType(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return null;
} else {
return null;
}
}
public Object getValue(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return null;
} else {
return null;
}
}
public boolean isReadOnly(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return true;
} else {
return false;
}
}
public void setValue(ELContext context, Object base, Object property, Object value) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
throw new PropertyNotWritableException("The 'null' value cannot be set");
}
}
}

View File

@@ -42,6 +42,11 @@ public class ELExpressionParserTests extends TestCase {
}
}
public void testParseNull() {
Expression exp = parser.parseExpression("null", null);
assertEquals(null, exp.getValue(null));
}
public void testParseEmptyExpressionString() {
String expressionString = "";
try {

View File

@@ -27,7 +27,6 @@ import javax.el.VariableMapper;
import org.springframework.binding.expression.el.DefaultELResolver;
import org.springframework.binding.expression.el.ELContextFactory;
import org.springframework.binding.expression.el.ELExpressionParser;
import org.springframework.binding.expression.el.NullELResolver;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.expression.el.ActionMethodELResolver;
import org.springframework.webflow.expression.el.ImplicitFlowVariableELResolver;
@@ -53,7 +52,6 @@ public class JsfManagedBeanAwareELExpressionParser extends ELExpressionParser {
RequestContext context = (RequestContext) target;
List customResolvers = new ArrayList();
customResolvers.add(new RequestContextELResolver(context));
customResolvers.add(new NullELResolver());
customResolvers.add(new ImplicitFlowVariableELResolver(context));
customResolvers.add(new ScopeSearchingELResolver(context));
customResolvers.add(new SpringBeanWebFlowELResolver(context));

View File

@@ -27,7 +27,6 @@ import javax.el.VariableMapper;
import org.springframework.binding.expression.el.DefaultELResolver;
import org.springframework.binding.expression.el.ELContextFactory;
import org.springframework.binding.expression.el.ELExpressionParser;
import org.springframework.binding.expression.el.NullELResolver;
import org.springframework.webflow.execution.RequestContext;
/**
@@ -55,7 +54,6 @@ public class WebFlowELExpressionParser extends ELExpressionParser {
RequestContext context = (RequestContext) target;
List customResolvers = new ArrayList();
customResolvers.add(new RequestContextELResolver(context));
customResolvers.add(new NullELResolver());
customResolvers.add(new ImplicitFlowVariableELResolver(context));
customResolvers.add(new ScopeSearchingELResolver(context));
customResolvers.add(new SpringBeanWebFlowELResolver(context));

View File

@@ -14,6 +14,7 @@ import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.engine.AnnotatedAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.TestAction;
import org.springframework.webflow.test.MockRequestContext;
@@ -148,4 +149,20 @@ public class WebFlowELExpressionParserTests extends TestCase {
assertEquals("setupForm", action.getMethod());
}
public void testResolveEventAttributes() {
MockRequestContext context = new MockRequestContext();
LocalAttributeMap attributes = new LocalAttributeMap();
attributes.put("foo", "bar");
context.setCurrentEvent(new Event(this, "event", attributes));
Expression exp = parser.parseExpression("currentEvent.foo", new FluentParserContext()
.evaluate(RequestContext.class));
assertEquals("bar", exp.getValue(context));
}
public void testResolveNull() {
MockRequestContext context = new MockRequestContext();
Expression exp = parser.parseExpression("null", new FluentParserContext().evaluate(RequestContext.class));
assertEquals(null, exp.getValue(context));
}
}