simplified Expression api by not forcing serialization requirement

This commit is contained in:
Keith Donald
2008-07-03 22:29:12 +00:00
parent 29cb51240e
commit d6608b8cc9
15 changed files with 116 additions and 247 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.binding.convert;
/**
* A command object that is parameterized with the information necessary to perform a conversion of a source input to a
* target output. Encapsulates knowledge about how to convert source objects to a specific target type using a specific

View File

@@ -1,64 +0,0 @@
/*
* Copyright 2004-2008 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.binding.expression;
import java.io.Serializable;
import org.springframework.core.style.ToStringCreator;
/**
* A simple holder for information about an expression evaluation attempt.
*
* @author Keith Donald
*/
public class EvaluationAttempt implements Serializable {
private Expression expression;
private transient Object context;
/**
* Create an evaluation attempt.
* @param expression the expression that failed to evaluate
* @param context the context of the expression evaluation
*/
public EvaluationAttempt(Expression expression, Object context) {
this.expression = expression;
this.context = context;
}
/**
* Returns the expression that attempted an evaluation.
*/
public Expression getExpression() {
return expression;
}
/**
* Returns the context object in which expression evaluation was attempted.
*/
public Object getContext() {
return context;
}
public String toString() {
return createToString(new ToStringCreator(this)).toString();
}
protected ToStringCreator createToString(ToStringCreator creator) {
return creator.append("expression", expression).append("context", context);
}
}

View File

@@ -22,43 +22,45 @@ package org.springframework.binding.expression;
*/
public class EvaluationException extends RuntimeException {
/**
* The evaluation attempt that failed. Transient because an EvaluationAttempt is not serializable.
*/
private EvaluationAttempt evaluationAttempt;
private Class contextClass;
private String expressionString;
/**
* Creates a new evaluation exception.
* @param evaluationAttempt the evaluation attempt that failed
* @param cause the underlying cause of this exception
* @param contextClass the class of object upon which evaluation was attempted
* @param expressionString the string form of the expression that failed to evaluate
* @param message the exception message
*/
public EvaluationException(EvaluationAttempt evaluationAttempt, Throwable cause) {
this(evaluationAttempt, defaultMessage(evaluationAttempt, cause), cause);
public EvaluationException(Class contextClass, String expressionString, String message) {
this(contextClass, expressionString, message, null);
}
/**
* Creates a new evaluation exception.
* @param evaluationAttempt the evaluation attempt that failed
* @param cause the underlying cause of this exception
* @param contextClass the class of object upon which evaluation was attempted
* @param expressionString the string form of the expression that failed to evaluate
* @param message the exception message
* @param cause the underlying cause of this evaluation exception
*/
public EvaluationException(EvaluationAttempt evaluationAttempt, String message, Throwable cause) {
public EvaluationException(Class contextClass, String expressionString, String message, Throwable cause) {
super(message, cause);
this.evaluationAttempt = evaluationAttempt;
}
/**
* Returns the evaluation attempt that failed.
* The class of object upon which evaluation was attempted and failed.
* @return the context class
*/
public EvaluationAttempt getEvaluationAttempt() {
return evaluationAttempt;
public Class getContextClass() {
return contextClass;
}
private static String defaultMessage(EvaluationAttempt evaluationAttempt, Throwable cause) {
if (cause != null) {
return evaluationAttempt + " failed - " + cause.getMessage();
} else {
return evaluationAttempt + " failed - make sure the expression is evaluatable in the context provided";
}
/**
* The string form of the expression that failed to evaluate against an instance of the the context class.
* @return the expression string
*/
public String getExpressionString() {
return expressionString;
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.binding.expression;
import java.io.Serializable;
/**
* An expression capable of evaluating itself against context objects. Encapsulates the details of a previously parsed
* expression string. Provides a common abstraction for expression evaluation independent of any language like OGNL or
@@ -24,7 +22,7 @@ import java.io.Serializable;
*
* @author Keith Donald
*/
public interface Expression extends Serializable {
public interface Expression {
/**
* Evaluate this expression in the provided context and return the result of evaluation.

View File

@@ -24,10 +24,13 @@ public class PropertyNotFoundException extends EvaluationException {
/**
* Creates a new property not found exception
* @param evaluationAttempt the evaluaion attempt details
* @param contextClass the class of object upon which property evaluation was attempted
* @param property the property that could not be found
* @param cause root cause of the failure
*/
public PropertyNotFoundException(EvaluationAttempt evaluationAttempt, Throwable cause) {
super(evaluationAttempt, cause);
public PropertyNotFoundException(Class contextClass, String property, Throwable cause) {
super(contextClass, property, "Property '" + property + "' not found on context of class ["
+ contextClass.getName() + "]", cause);
}
}

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2004-2008 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.binding.expression;
import org.springframework.core.style.ToStringCreator;
/**
* Records an attempt to set an expression value.
*
* @author Keith Donald
*/
public class SetValueAttempt extends EvaluationAttempt {
/**
* The new value.
*/
private transient Object value;
/**
* Creates a new set attempt.
* @param expression the settable expression
* @param context the target of the expression
* @param value the value that was attempted to be set
*/
public SetValueAttempt(Expression expression, Object context, Object value) {
super(expression, context);
this.value = value;
}
/**
* Returns the value that was attempted to be set.
*/
public Object getValue() {
return value;
}
protected ToStringCreator createToString(ToStringCreator creator) {
return super.createToString(creator).append("value", value);
}
}

View File

@@ -19,10 +19,10 @@ import java.beans.PropertyEditorSupport;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.expression.EvaluationAttempt;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.PropertyNotFoundException;
@@ -76,10 +76,12 @@ public class BeanWrapperExpression implements Expression {
try {
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context);
return beanWrapper.getPropertyValue(expression);
} catch (InvalidPropertyException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
} catch (NotReadablePropertyException e) {
throw new PropertyNotFoundException(context.getClass(), expression, e);
} catch (BeansException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"A BeansException occurred getting the value for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}
@@ -92,10 +94,12 @@ public class BeanWrapperExpression implements Expression {
beanWrapper.registerCustomEditor(converter.getTargetClass(), new PropertyEditorConverter(converter));
}
beanWrapper.setPropertyValue(expression, value);
} catch (InvalidPropertyException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
} catch (NotWritablePropertyException e) {
throw new PropertyNotFoundException(context.getClass(), expression, e);
} catch (BeansException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"A BeansException occurred setting the value of expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "] to [" + value + "]", e);
}
}
@@ -103,10 +107,12 @@ public class BeanWrapperExpression implements Expression {
try {
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context);
return beanWrapper.getPropertyType(expression);
} catch (InvalidPropertyException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
} catch (NotReadablePropertyException e) {
throw new PropertyNotFoundException(context.getClass(), expression, e);
} catch (BeansException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"An BeansException occurred getting the value type for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}

View File

@@ -19,11 +19,11 @@ import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ValueExpression;
import org.springframework.binding.expression.EvaluationAttempt;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.PropertyNotFoundException;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.util.Assert;
/**
@@ -39,6 +39,8 @@ public class ELExpression implements Expression {
private boolean template;
private ConversionService conversionService;
/**
* Creates a new el expression
* @param factory the el context factory for creating the EL context that will be used during expression evaluation
@@ -46,11 +48,14 @@ public class ELExpression implements Expression {
* @param template whether or not this expression is a template expression; if not it was parsed as an implict eval
* expression (without delimiters)
*/
public ELExpression(ELContextFactory factory, ValueExpression valueExpression, boolean template) {
public ELExpression(ELContextFactory factory, ValueExpression valueExpression, ConversionService conversionService,
boolean template) {
Assert.notNull(factory, "The ELContextFactory is required to evaluate EL expressions");
Assert.notNull(valueExpression, "The EL value expression is required for evaluation");
Assert.notNull(conversionService, "The conversion service is required");
this.elContextFactory = factory;
this.valueExpression = valueExpression;
this.conversionService = conversionService;
this.template = template;
}
@@ -63,33 +68,41 @@ public class ELExpression implements Expression {
// 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 spelled correctly?",
null);
throw new EvaluationException(context.getClass(), getExpressionString(), "The expression '"
+ getExpressionString() + "' did not resolve... is the base variable ''"
+ getBaseVariable() + "' spelled correctly?");
}
}
return result;
} catch (javax.el.PropertyNotFoundException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (ELException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"An ELException occurred getting the value for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}
public void setValue(Object context, Object value) throws EvaluationException {
ELContext ctx = elContextFactory.getELContext(context);
try {
Class targetType = getValueType(context);
if (value != null && targetType != null) {
ConversionExecutor converter = conversionService.getConversionExecutor(value.getClass(), targetType);
value = converter.execute(value);
}
valueExpression.setValue(ctx, value);
if (!ctx.isPropertyResolved()) {
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);
throw new EvaluationException(context.getClass(), getExpressionString(), "The expression '"
+ getExpressionString() + "' did not resolve... is the base variable ''" + getBaseVariable()
+ "' spelled correctly?");
}
} catch (javax.el.PropertyNotFoundException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
} catch (ELException ex) {
throw new EvaluationException(new EvaluationAttempt(this, context), ex);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (ELException e) {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An ELException occurred setting the value of expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "] to [" + value + "]", e);
}
}
@@ -98,9 +111,11 @@ public class ELExpression implements Expression {
try {
return valueExpression.getType(ctx);
} catch (javax.el.PropertyNotFoundException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
} catch (ELException ex) {
throw new EvaluationException(new EvaluationAttempt(this, context), ex);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (ELException e) {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An ELException occurred getting the value type for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}
@@ -113,6 +128,16 @@ public class ELExpression implements Expression {
}
}
private String getBaseVariable() {
String expressionString = getExpressionString();
int firstDot = expressionString.indexOf('.');
if (firstDot == -1) {
return expressionString;
} else {
return expressionString.substring(0, firstDot);
}
}
public int hashCode() {
return valueExpression.hashCode();
}

View File

@@ -26,6 +26,8 @@ import javax.el.FunctionMapper;
import javax.el.ValueExpression;
import javax.el.VariableMapper;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.convert.service.DefaultConversionService;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionParser;
import org.springframework.binding.expression.ExpressionVariable;
@@ -47,6 +49,8 @@ public class ELExpressionParser implements ExpressionParser {
*/
private ExpressionFactory expressionFactory;
private ConversionService conversionService = new DefaultConversionService();
private Map contextFactories = new HashMap();
/**
@@ -86,7 +90,7 @@ public class ELExpressionParser implements ExpressionParser {
try {
ValueExpression expression = parseValueExpression(expressionString, context);
ELContextFactory contextFactory = getContextFactory(context.getEvaluationContextType(), expressionString);
return new ELExpression(contextFactory, expression, template);
return new ELExpression(contextFactory, expression, conversionService, template);
} catch (ELException e) {
throw new ParserException(expressionString, e);
}

View File

@@ -27,11 +27,9 @@ import ognl.OgnlException;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.binding.expression.EvaluationAttempt;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.PropertyNotFoundException;
import org.springframework.binding.expression.SetValueAttempt;
/**
* Evaluates a parsed Ognl expression.
@@ -75,9 +73,11 @@ class OgnlExpression implements Expression {
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
return Ognl.getValue(expression, evaluationContext, context, expectedResultType);
} catch (NoSuchPropertyException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (OgnlException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred getting the value for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}
@@ -86,9 +86,11 @@ class OgnlExpression implements Expression {
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
Ognl.setValue(expression, evaluationContext, context, value);
} catch (NoSuchPropertyException e) {
throw new PropertyNotFoundException(new SetValueAttempt(this, context, value), e);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (OgnlException e) {
throw new EvaluationException(new SetValueAttempt(this, context, value), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred setting the value of expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "] to [" + value + "]", e);
}
}
@@ -97,9 +99,11 @@ class OgnlExpression implements Expression {
// OGNL has no native way to get this information
return new BeanWrapperImpl(context).getPropertyDescriptor(expressionString).getPropertyType();
} catch (InvalidPropertyException e) {
throw new PropertyNotFoundException(new EvaluationAttempt(this, context), e);
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (BeansException e) {
throw new EvaluationException(new EvaluationAttempt(this, context), e);
throw new EvaluationException(context.getClass(), getExpressionString(),
"An BeansException occurred getting the value type for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}

View File

@@ -19,7 +19,6 @@ import java.util.Collection;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
@@ -50,8 +49,10 @@ public class CollectionAddingExpression implements Expression {
public void setValue(Object context, Object value) throws EvaluationException {
Object result = getValue(context);
if (result == null) {
throw new EvaluationException(new SetValueAttempt(this, context, value), new IllegalArgumentException(
"The collection expression evaluated to a [null] reference"));
throw new EvaluationException(context.getClass(), collectionExpression.getExpressionString(),
"Unable to access collection value for expression '" + collectionExpression.getExpressionString()
+ "'", new IllegalStateException(
"The collection expression evaluated to a [null] reference"));
}
Assert.isInstanceOf(Collection.class, result, "Not a collection: ");
if (value != null) {
@@ -65,7 +66,7 @@ public class CollectionAddingExpression implements Expression {
}
public String getExpressionString() {
return null;
return collectionExpression.getExpressionString();
}
public String toString() {

View File

@@ -17,7 +17,6 @@ package org.springframework.binding.expression.support;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.core.style.ToStringCreator;
/**
@@ -50,8 +49,7 @@ public class CompositeStringExpression implements Expression {
}
public void setValue(Object context, Object value) throws EvaluationException {
throw new EvaluationException(new SetValueAttempt(this, context, value), new UnsupportedOperationException(
"Cannot set a composite string expression value"));
throw new UnsupportedOperationException("Cannot set a composite string expression value");
}
public Class getValueType(Object context) {

View File

@@ -17,7 +17,6 @@ package org.springframework.binding.expression.support;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.SetValueAttempt;
import org.springframework.util.Assert;
public class LiteralExpression implements Expression {
@@ -53,9 +52,9 @@ public class LiteralExpression implements Expression {
}
public void setValue(Object context, Object value) throws EvaluationException {
throw new EvaluationException(new SetValueAttempt(this, context, value), new UnsupportedOperationException(
throw new UnsupportedOperationException(
"Cannot set a literal expression value. Are you attempting to set a property expression? "
+ "If so, should the expression string be enclosed in eval delimiters?"));
+ "If so, should the expression string be enclosed in eval delimiters?");
}
public Class getValueType(Object context) {