SPR-6866: unhelpful NPE when expression badly formed

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3064 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Andy Clement 2010-03-09 00:55:03 +00:00
parent eeb15e4809
commit 43ea572fd5
2 changed files with 21 additions and 2 deletions

View File

@ -336,7 +336,12 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
if (maybeEatMethodOrProperty(nullSafeNavigation) || maybeEatFunctionOrVar() || maybeEatProjection(nullSafeNavigation) || maybeEatSelection(nullSafeNavigation)) {
return pop();
}
raiseInternalException(t.startpos,SpelMessage.UNEXPECTED_DATA_AFTER_DOT,toString(peekToken()));
if (peekToken()==null) {
// unexpectedly ran out of data
raiseInternalException(t.startpos,SpelMessage.OOD);
} else {
raiseInternalException(t.startpos,SpelMessage.UNEXPECTED_DATA_AFTER_DOT,toString(peekToken()));
}
return null;
}

View File

@ -20,8 +20,8 @@ import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
@ -222,6 +222,20 @@ public class EvaluationTests extends ExpressionTestCase {
"org.springframework.expression.spel.testresources.Inventor");
}
@Test
public void testRogueTrailingDotCausesNPE_SPR6866() {
try {
new SpelExpressionParser().parseExpression("placeOfBirth.foo.");
Assert.fail("Should have failed to parse");
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.OOD,spe.getMessageCode());
Assert.assertEquals(16,spe.getPosition());
}
}
// nested properties
@Test
public void testPropertiesNested01() {