SPR-6059 Avoiding NPE for unary-not and ternary operators. If the value is null instead of a valid boolean or Boolean, it will now trigger an EvaluationException.

This commit is contained in:
Mark Fisher
2009-10-28 21:57:09 +00:00
parent a2cd909306
commit f4460dcbf7
3 changed files with 25 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
@@ -36,7 +37,7 @@ public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so do
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
try {
boolean value = (Boolean)state.convertValue(children[0].getValueInternal(state), BOOLEAN_TYPE_DESCRIPTOR);
boolean value = (Boolean) state.convertValue(children[0].getValueInternal(state), TypeDescriptor.valueOf(boolean.class));
return BooleanTypedValue.forValue(!value);
}
catch (SpelEvaluationException see) {

View File

@@ -19,6 +19,8 @@ package org.springframework.expression.spel.ast;
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;
/**
* Represents a ternary expression, for example: "someCheck()?true:false".
@@ -43,6 +45,10 @@ public class Ternary extends SpelNodeImpl {
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
Boolean value = children[0].getValue(state, Boolean.class);
if (value == null) {
throw new SpelEvaluationException(getChild(0).getStartPosition(),
SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
}
if (value.booleanValue()) {
return children[1].getValueInternal(state);
} else {