77
77
import org .springframework .expression .spel .ast .TypeReference ;
78
78
import org .springframework .expression .spel .ast .VariableReference ;
79
79
import org .springframework .lang .Nullable ;
80
- import org .springframework .util .Assert ;
81
80
import org .springframework .util .StringUtils ;
82
81
83
82
/**
@@ -137,12 +136,13 @@ protected SpelExpression doParseExpression(String expressionString, @Nullable Pa
137
136
this .tokenStreamPointer = 0 ;
138
137
this .constructedNodes .clear ();
139
138
SpelNodeImpl ast = eatExpression ();
140
- Assert .state (ast != null , "No node" );
139
+ if (ast == null ) {
140
+ throw new SpelParseException (this .expressionString , 0 , SpelMessage .OOD );
141
+ }
141
142
Token t = peekToken ();
142
143
if (t != null ) {
143
- throw new SpelParseException (t .startPos , SpelMessage .MORE_INPUT , toString (nextToken ()));
144
+ throw new SpelParseException (this . expressionString , t .startPos , SpelMessage .MORE_INPUT , toString (nextToken ()));
144
145
}
145
- Assert .isTrue (this .constructedNodes .isEmpty (), "At least one node expected" );
146
146
return new SpelExpression (expressionString , ast , this .configuration );
147
147
}
148
148
catch (InternalParseException ex ) {
@@ -254,20 +254,20 @@ private SpelNodeImpl eatRelationalExpression() {
254
254
if (tk == TokenKind .EQ ) {
255
255
return new OpEQ (t .startPos , t .endPos , expr , rhExpr );
256
256
}
257
- Assert .isTrue (tk == TokenKind .NE , "Not-equals token expected" );
258
- return new OpNE (t .startPos , t .endPos , expr , rhExpr );
257
+ if (tk == TokenKind .NE ) {
258
+ return new OpNE (t .startPos , t .endPos , expr , rhExpr );
259
+ }
259
260
}
260
261
261
262
if (tk == TokenKind .INSTANCEOF ) {
262
263
return new OperatorInstanceof (t .startPos , t .endPos , expr , rhExpr );
263
264
}
264
-
265
265
if (tk == TokenKind .MATCHES ) {
266
266
return new OperatorMatches (this .patternCache , t .startPos , t .endPos , expr , rhExpr );
267
267
}
268
-
269
- Assert . isTrue ( tk == TokenKind . BETWEEN , "Between token expected" );
270
- return new OperatorBetween ( t . startPos , t . endPos , expr , rhExpr );
268
+ if ( tk == TokenKind . BETWEEN ) {
269
+ return new OperatorBetween ( t . startPos , t . endPos , expr , rhExpr );
270
+ }
271
271
}
272
272
return expr ;
273
273
}
@@ -304,8 +304,7 @@ private SpelNodeImpl eatProductExpression() {
304
304
else if (t .kind == TokenKind .DIV ) {
305
305
expr = new OpDivide (t .startPos , t .endPos , expr , rhExpr );
306
306
}
307
- else {
308
- Assert .isTrue (t .kind == TokenKind .MOD , "Mod token expected" );
307
+ else if (t .kind == TokenKind .MOD ) {
309
308
expr = new OpModulus (t .startPos , t .endPos , expr , rhExpr );
310
309
}
311
310
}
@@ -335,26 +334,31 @@ private SpelNodeImpl eatPowerIncDecExpression() {
335
334
// unaryExpression: (PLUS^ | MINUS^ | BANG^ | INC^ | DEC^) unaryExpression | primaryExpression ;
336
335
@ Nullable
337
336
private SpelNodeImpl eatUnaryExpression () {
338
- if (peekToken (TokenKind .PLUS , TokenKind .MINUS , TokenKind .NOT )) {
337
+ if (peekToken (TokenKind .NOT , TokenKind .PLUS , TokenKind .MINUS )) {
339
338
Token t = takeToken ();
340
339
SpelNodeImpl expr = eatUnaryExpression ();
341
- Assert .state (expr != null , "No node" );
340
+ if (expr == null ) {
341
+ throw internalException (t .startPos , SpelMessage .OOD );
342
+ }
342
343
if (t .kind == TokenKind .NOT ) {
343
344
return new OperatorNot (t .startPos , t .endPos , expr );
344
345
}
345
346
if (t .kind == TokenKind .PLUS ) {
346
347
return new OpPlus (t .startPos , t .endPos , expr );
347
348
}
348
- Assert .isTrue (t .kind == TokenKind .MINUS , "Minus token expected" );
349
- return new OpMinus (t .startPos , t .endPos , expr );
349
+ if (t .kind == TokenKind .MINUS ) {
350
+ return new OpMinus (t .startPos , t .endPos , expr );
351
+ }
350
352
}
351
353
if (peekToken (TokenKind .INC , TokenKind .DEC )) {
352
354
Token t = takeToken ();
353
355
SpelNodeImpl expr = eatUnaryExpression ();
354
356
if (t .getKind () == TokenKind .INC ) {
355
357
return new OpInc (t .startPos , t .endPos , false , expr );
356
358
}
357
- return new OpDec (t .startPos , t .endPos , false , expr );
359
+ if (t .kind == TokenKind .DEC ) {
360
+ return new OpDec (t .startPos , t .endPos , false , expr );
361
+ }
358
362
}
359
363
return eatPrimaryExpression ();
360
364
}
@@ -414,7 +418,6 @@ private SpelNodeImpl eatDottedNode() {
414
418
return pop ();
415
419
}
416
420
if (peekToken () == null ) {
417
- // unexpectedly ran out of data
418
421
throw internalException (t .startPos , SpelMessage .OOD );
419
422
}
420
423
else {
@@ -460,8 +463,7 @@ private SpelNodeImpl[] maybeEatMethodArgs() {
460
463
461
464
private void eatConstructorArgs (List <SpelNodeImpl > accumulatedArguments ) {
462
465
if (!peekToken (TokenKind .LPAREN )) {
463
- throw new InternalParseException (new SpelParseException (this .expressionString ,
464
- positionOf (peekToken ()), SpelMessage .MISSING_CONSTRUCTOR_ARGS ));
466
+ throw internalException (positionOf (peekToken ()), SpelMessage .MISSING_CONSTRUCTOR_ARGS );
465
467
}
466
468
consumeArguments (accumulatedArguments );
467
469
eatToken (TokenKind .RPAREN );
@@ -472,7 +474,9 @@ private void eatConstructorArgs(List<SpelNodeImpl> accumulatedArguments) {
472
474
*/
473
475
private void consumeArguments (List <SpelNodeImpl > accumulatedArguments ) {
474
476
Token t = peekToken ();
475
- Assert .state (t != null , "Expected token" );
477
+ if (t == null ) {
478
+ return ;
479
+ }
476
480
int pos = t .startPos ;
477
481
Token next ;
478
482
do {
@@ -575,8 +579,7 @@ else if (peekToken(TokenKind.LITERAL_STRING)) {
575
579
private boolean maybeEatTypeReference () {
576
580
if (peekToken (TokenKind .IDENTIFIER )) {
577
581
Token typeName = peekToken ();
578
- Assert .state (typeName != null , "Expected token" );
579
- if (!"T" .equals (typeName .stringValue ())) {
582
+ if (typeName == null || !"T" .equals (typeName .stringValue ())) {
580
583
return false ;
581
584
}
582
585
// It looks like a type reference but is T being used as a map key?
@@ -605,8 +608,7 @@ private boolean maybeEatTypeReference() {
605
608
private boolean maybeEatNullReference () {
606
609
if (peekToken (TokenKind .IDENTIFIER )) {
607
610
Token nullToken = peekToken ();
608
- Assert .state (nullToken != null , "Expected token" );
609
- if (!"null" .equalsIgnoreCase (nullToken .stringValue ())) {
611
+ if (nullToken == null || !"null" .equalsIgnoreCase (nullToken .stringValue ())) {
610
612
return false ;
611
613
}
612
614
nextToken ();
@@ -619,12 +621,13 @@ private boolean maybeEatNullReference() {
619
621
//projection: PROJECT^ expression RCURLY!;
620
622
private boolean maybeEatProjection (boolean nullSafeNavigation ) {
621
623
Token t = peekToken ();
622
- if (!peekToken (TokenKind .PROJECT , true )) {
624
+ if (t == null || !peekToken (TokenKind .PROJECT , true )) {
623
625
return false ;
624
626
}
625
- Assert .state (t != null , "No token" );
626
627
SpelNodeImpl expr = eatExpression ();
627
- Assert .state (expr != null , "No node" );
628
+ if (expr == null ) {
629
+ throw internalException (t .startPos , SpelMessage .OOD );
630
+ }
628
631
eatToken (TokenKind .RSQUARE );
629
632
this .constructedNodes .push (new Projection (nullSafeNavigation , t .startPos , t .endPos , expr ));
630
633
return true ;
@@ -634,15 +637,13 @@ private boolean maybeEatProjection(boolean nullSafeNavigation) {
634
637
// map = LCURLY (key ':' value (COMMA key ':' value)*) RCURLY
635
638
private boolean maybeEatInlineListOrMap () {
636
639
Token t = peekToken ();
637
- if (!peekToken (TokenKind .LCURLY , true )) {
640
+ if (t == null || !peekToken (TokenKind .LCURLY , true )) {
638
641
return false ;
639
642
}
640
- Assert .state (t != null , "No token" );
641
643
SpelNodeImpl expr = null ;
642
644
Token closingCurly = peekToken ();
643
- if (peekToken (TokenKind .RCURLY , true )) {
645
+ if (closingCurly != null && peekToken (TokenKind .RCURLY , true )) {
644
646
// empty list '{}'
645
- Assert .state (closingCurly != null , "No token" );
646
647
expr = new InlineList (t .startPos , closingCurly .endPos );
647
648
}
648
649
else if (peekToken (TokenKind .COLON , true )) {
@@ -695,23 +696,23 @@ else if (peekToken(TokenKind.COLON, true)) { // map!
695
696
696
697
private boolean maybeEatIndexer () {
697
698
Token t = peekToken ();
698
- if (!peekToken (TokenKind .LSQUARE , true )) {
699
+ if (t == null || !peekToken (TokenKind .LSQUARE , true )) {
699
700
return false ;
700
701
}
701
- Assert .state (t != null , "No token" );
702
702
SpelNodeImpl expr = eatExpression ();
703
- Assert .state (expr != null , "No node" );
703
+ if (expr == null ) {
704
+ throw internalException (t .startPos , SpelMessage .MISSING_SELECTION_EXPRESSION );
705
+ }
704
706
eatToken (TokenKind .RSQUARE );
705
707
this .constructedNodes .push (new Indexer (t .startPos , t .endPos , expr ));
706
708
return true ;
707
709
}
708
710
709
711
private boolean maybeEatSelection (boolean nullSafeNavigation ) {
710
712
Token t = peekToken ();
711
- if (!peekSelectToken ()) {
713
+ if (t == null || !peekSelectToken ()) {
712
714
return false ;
713
715
}
714
- Assert .state (t != null , "No token" );
715
716
nextToken ();
716
717
SpelNodeImpl expr = eatExpression ();
717
718
if (expr == null ) {
@@ -889,9 +890,14 @@ else if (t.kind == TokenKind.LITERAL_STRING) {
889
890
//parenExpr : LPAREN! expression RPAREN!;
890
891
private boolean maybeEatParenExpression () {
891
892
if (peekToken (TokenKind .LPAREN )) {
892
- nextToken ();
893
+ Token t = nextToken ();
894
+ if (t == null ) {
895
+ return false ;
896
+ }
893
897
SpelNodeImpl expr = eatExpression ();
894
- Assert .state (expr != null , "No node" );
898
+ if (expr == null ) {
899
+ throw internalException (t .startPos , SpelMessage .OOD );
900
+ }
895
901
eatToken (TokenKind .RPAREN );
896
902
push (expr );
897
903
return true ;
0 commit comments