Search in sources :

Example 26 with SpelNodeImpl

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();
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) Test(org.junit.jupiter.api.Test)

Example 27 with SpelNodeImpl

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");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 28 with SpelNodeImpl

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;
}
Also used : OpOr(cn.taketoday.expression.spel.ast.OpOr) SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) Nullable(cn.taketoday.lang.Nullable)

Example 29 with SpelNodeImpl

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();
    }
}
Also used : SpelParseException(cn.taketoday.expression.spel.SpelParseException) InternalParseException(cn.taketoday.expression.spel.InternalParseException) SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl)

Example 30 with SpelNodeImpl

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;
    }
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl)

Aggregations

SpelNodeImpl (cn.taketoday.expression.spel.ast.SpelNodeImpl)48 Nullable (cn.taketoday.lang.Nullable)18 PropertyOrFieldReference (cn.taketoday.expression.spel.ast.PropertyOrFieldReference)6 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)6 Test (org.junit.jupiter.api.Test)6 OpDec (cn.taketoday.expression.spel.ast.OpDec)4 OpInc (cn.taketoday.expression.spel.ast.OpInc)4 OpMinus (cn.taketoday.expression.spel.ast.OpMinus)4 OpPlus (cn.taketoday.expression.spel.ast.OpPlus)4 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)4 ArrayList (java.util.ArrayList)4 InternalParseException (cn.taketoday.expression.spel.InternalParseException)2 SpelParseException (cn.taketoday.expression.spel.SpelParseException)2 Assign (cn.taketoday.expression.spel.ast.Assign)2 CompoundExpression (cn.taketoday.expression.spel.ast.CompoundExpression)2 ConstructorReference (cn.taketoday.expression.spel.ast.ConstructorReference)2 Elvis (cn.taketoday.expression.spel.ast.Elvis)2 FunctionReference (cn.taketoday.expression.spel.ast.FunctionReference)2 Identifier (cn.taketoday.expression.spel.ast.Identifier)2 Indexer (cn.taketoday.expression.spel.ast.Indexer)2