use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-framework by TAKETODAY.
the class SpelCompilationCoverageTests method functionReferenceNonCompilableArguments_SPR12359.
@Test
public void functionReferenceNonCompilableArguments_SPR12359() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { "1" });
context.registerFunction("negate", SomeCompareMethod2.class.getDeclaredMethod("negate", Integer.TYPE));
context.setVariable("arg", "2");
int[] ints = new int[] { 1, 2, 3 };
context.setVariable("ints", ints);
expression = parser.parseExpression("#negate(#ints.?[#this<2][0])");
assertThat(expression.getValue(context, Integer.class).toString()).isEqualTo("-1");
// Selection isn't compilable.
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isFalse();
}
use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-framework by TAKETODAY.
the class SpelCompilationCoverageTests method compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs.
@Test
public void compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs() throws Exception {
StandardEvaluationContext context = null;
// Here the target method takes Object... and we are passing a string
expression = parser.parseExpression("#doFormat('hey %s', 'there')");
context = new StandardEvaluationContext();
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
expression = parser.parseExpression("#doFormat([0], 'there')");
context = new StandardEvaluationContext(new Object[] { "hey %s" });
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
expression = parser.parseExpression("#doFormat([0], #arg)");
context = new StandardEvaluationContext(new Object[] { "hey %s" });
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
context.setVariable("arg", "there");
((SpelExpression) expression).setEvaluationContext(context);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("hey there");
}
use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-framework by TAKETODAY.
the class InternalSpelExpressionParser method eatLogicalOrExpression.
// logicalOrExpression : logicalAndExpression (OR^ logicalAndExpression)*;
@Nullable
private SpelNodeImpl eatLogicalOrExpression() {
SpelNodeImpl expr = eatLogicalAndExpression();
while (peekIdentifierToken("or") || peekToken(TokenKind.SYMBOLIC_OR)) {
// consume OR
Token t = takeToken();
SpelNodeImpl rhExpr = eatLogicalAndExpression();
checkOperands(t, expr, rhExpr);
expr = new OpOr(t.startPos, t.endPos, expr, rhExpr);
}
return expr;
}
use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-framework by TAKETODAY.
the class InternalSpelExpressionParser method doParseExpression.
@Override
protected SpelExpression doParseExpression(String expressionString, @Nullable ParserContext context) throws ParseException {
try {
this.expressionString = expressionString;
Tokenizer tokenizer = new Tokenizer(expressionString);
this.tokenStream = tokenizer.process();
this.tokenStreamLength = this.tokenStream.size();
this.tokenStreamPointer = 0;
this.constructedNodes.clear();
SpelNodeImpl ast = eatExpression();
Assert.state(ast != null, "No node");
Token t = peekToken();
if (t != null) {
throw new SpelParseException(t.startPos, SpelMessage.MORE_INPUT, toString(nextToken()));
}
Assert.isTrue(this.constructedNodes.isEmpty(), "At least one node expected");
return new SpelExpression(expressionString, ast, this.configuration);
} catch (InternalParseException ex) {
throw ex.getCause();
}
}
use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-framework by TAKETODAY.
the class InternalSpelExpressionParser method maybeEatParenExpression.
// parenExpr : LPAREN! expression RPAREN!;
private boolean maybeEatParenExpression() {
if (peekToken(TokenKind.LPAREN)) {
nextToken();
SpelNodeImpl expr = eatExpression();
Assert.state(expr != null, "No node");
eatToken(TokenKind.RPAREN);
push(expr);
return true;
} else {
return false;
}
}
Aggregations