Call ConversionService for null SpEL values

Update SpEL boolean operators to always call the ConversionService
for null values. Primarily to allow null values to be treated as
false by overriding GenericConversionService.convertNullSource().

Issue: SPR-9445
This commit is contained in:
Oliver Becker
2012-11-21 22:53:29 -08:00
committed by Phillip Webb
parent d9bd2e19a2
commit 759c9b35cd
7 changed files with 79 additions and 86 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -60,7 +60,7 @@ public abstract class ExpressionUtils {
@SuppressWarnings("unchecked")
public static <T> T convertTypedValue(EvaluationContext context, TypedValue typedValue, Class<T> targetType) {
Object value = typedValue.getValue();
if (targetType == null || ClassUtils.isAssignableValue(targetType, value)) {
if ((targetType == null) || (value != null && ClassUtils.isAssignableValue(targetType, value))) {
return (T) value;
}
if (context != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,7 +16,6 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
@@ -29,6 +28,7 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
*
* @author Andy Clement
* @author Mark Fisher
* @author Oliver Becker
* @since 3.0
*/
public class OpAnd extends Operator {
@@ -39,38 +39,27 @@ public class OpAnd extends Operator {
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
boolean leftValue;
boolean rightValue;
try {
TypedValue typedValue = getLeftOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
if (getBooleanValue(state, getLeftOperand()) == false) {
// no need to evaluate right operand
return BooleanTypedValue.FALSE;
}
catch (SpelEvaluationException ee) {
ee.setPosition(getLeftOperand().getStartPosition());
throw ee;
}
if (leftValue == false) {
return BooleanTypedValue.forValue(false); // no need to evaluate right operand
}
try {
TypedValue typedValue = getRightOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException ee) {
ee.setPosition(getRightOperand().getStartPosition());
throw ee;
}
return /* leftValue && */BooleanTypedValue.forValue(rightValue);
return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
private void assertTypedValueNotNull(TypedValue typedValue) {
if (TypedValue.NULL.equals(typedValue)) {
private boolean getBooleanValue(ExpressionState state, SpelNodeImpl operand) {
try {
Boolean value = operand.getValue(state, Boolean.class);
assertValueNotNull(value);
return value;
}
catch (SpelEvaluationException ee) {
ee.setPosition(operand.getStartPosition());
throw ee;
}
}
private void assertValueNotNull(Boolean value) {
if (value == null) {
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,9 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
@@ -29,6 +27,7 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
*
* @author Andy Clement
* @author Mark Fisher
* @author Oliver Becker
* @since 3.0
*/
public class OpOr extends Operator {
@@ -39,37 +38,27 @@ public class OpOr extends Operator {
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
boolean leftValue;
boolean rightValue;
try {
TypedValue typedValue = getLeftOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
if (getBooleanValue(state, getLeftOperand())) {
// no need to evaluate right operand
return BooleanTypedValue.TRUE;
}
catch (SpelEvaluationException see) {
see.setPosition(getLeftOperand().getStartPosition());
throw see;
}
if (leftValue == true) {
return BooleanTypedValue.TRUE; // no need to evaluate right operand
}
try {
TypedValue typedValue = getRightOperand().getValueInternal(state);
this.assertTypedValueNotNull(typedValue);
rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
}
catch (SpelEvaluationException see) {
see.setPosition(getRightOperand().getStartPosition()); // TODO end positions here and in similar situations
throw see;
}
return BooleanTypedValue.forValue(leftValue || rightValue);
return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
private void assertTypedValueNotNull(TypedValue typedValue) {
if (TypedValue.NULL.equals(typedValue)) {
private boolean getBooleanValue(ExpressionState state, SpelNodeImpl operand) {
try {
Boolean value = operand.getValue(state, Boolean.class);
assertValueNotNull(value);
return value;
}
catch (SpelEvaluationException ee) {
ee.setPosition(operand.getStartPosition());
throw ee;
}
}
private void assertValueNotNull(Boolean value) {
if (value == null) {
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,9 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
@@ -29,6 +27,7 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
*
* @author Andy Clement
* @author Mark Fisher
* @author Oliver Becker
* @since 3.0
*/
public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so do not extend BinaryOperator
@@ -40,11 +39,10 @@ public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so do
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
try {
TypedValue typedValue = children[0].getValueInternal(state);
if (TypedValue.NULL.equals(typedValue)) {
Boolean value = children[0].getValue(state, Boolean.class);
if (value == null) {
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
}
boolean value = (Boolean) state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
return BooleanTypedValue.forValue(!value);
}
catch (SpelEvaluationException see) {

View File

@@ -130,18 +130,8 @@ public abstract class SpelNodeImpl implements SpelNode {
return (obj instanceof Class ? ((Class<?>) obj) : obj.getClass());
}
@SuppressWarnings("unchecked")
protected final <T> T getValue(ExpressionState state, Class<T> desiredReturnType) throws EvaluationException {
Object result = getValueInternal(state).getValue();
if (result != null && desiredReturnType != null) {
Class<?> resultType = result.getClass();
if (desiredReturnType.isAssignableFrom(resultType)) {
return (T) result;
}
// Attempt conversion to the requested type, may throw an exception
return ExpressionUtils.convert(state.getEvaluationContext(), result, desiredReturnType);
}
return (T) result;
return ExpressionUtils.convertTypedValue(state.getEvaluationContext(), getValueInternal(state), desiredReturnType);
}
public abstract TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException;