Search in sources :

Example 1 with SpelNodeImpl

use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-infrastructure 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 2 with SpelNodeImpl

use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-infrastructure by TAKETODAY.

the class SpelCompilationCoverageTests method functionReferenceVarargs_SPR12359.

@Test
public void functionReferenceVarargs_SPR12359() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.registerFunction("append", SomeCompareMethod2.class.getDeclaredMethod("append", String[].class));
    context.registerFunction("append2", SomeCompareMethod2.class.getDeclaredMethod("append2", Object[].class));
    context.registerFunction("append3", SomeCompareMethod2.class.getDeclaredMethod("append3", String[].class));
    context.registerFunction("append4", SomeCompareMethod2.class.getDeclaredMethod("append4", String.class, String[].class));
    context.registerFunction("appendChar", SomeCompareMethod2.class.getDeclaredMethod("appendChar", char[].class));
    context.registerFunction("sum", SomeCompareMethod2.class.getDeclaredMethod("sum", int[].class));
    context.registerFunction("sumDouble", SomeCompareMethod2.class.getDeclaredMethod("sumDouble", double[].class));
    context.registerFunction("sumFloat", SomeCompareMethod2.class.getDeclaredMethod("sumFloat", float[].class));
    context.setVariable("stringArray", new String[] { "x", "y", "z" });
    context.setVariable("intArray", new int[] { 5, 6, 9 });
    context.setVariable("doubleArray", new double[] { 5.0d, 6.0d, 9.0d });
    context.setVariable("floatArray", new float[] { 5.0f, 6.0f, 9.0f });
    expression = parser.parseExpression("#append('a','b','c')");
    assertThat(expression.getValue(context).toString()).isEqualTo("abc");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("abc");
    expression = parser.parseExpression("#append('a')");
    assertThat(expression.getValue(context).toString()).isEqualTo("a");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("a");
    expression = parser.parseExpression("#append()");
    assertThat(expression.getValue(context).toString()).isEqualTo("");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("");
    expression = parser.parseExpression("#append(#stringArray)");
    assertThat(expression.getValue(context).toString()).isEqualTo("xyz");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("xyz");
    // This is a methodreference invocation, to compare with functionreference
    expression = parser.parseExpression("append(#stringArray)");
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("xyz");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("xyz");
    expression = parser.parseExpression("#append2('a','b','c')");
    assertThat(expression.getValue(context).toString()).isEqualTo("abc");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("abc");
    expression = parser.parseExpression("append2('a','b')");
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("ab");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("ab");
    expression = parser.parseExpression("#append2('a','b')");
    assertThat(expression.getValue(context).toString()).isEqualTo("ab");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("ab");
    expression = parser.parseExpression("#append2()");
    assertThat(expression.getValue(context).toString()).isEqualTo("");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("");
    expression = parser.parseExpression("#append3(#stringArray)");
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("xyz");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("xyz");
    // TODO fails due to conversionservice handling of String[] to Object...
    // expression = parser.parseExpression("#append2(#stringArray)");
    // assertEquals("xyz", expression.getValue(context).toString());
    // assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable());
    // assertCanCompile(expression);
    // assertEquals("xyz", expression.getValue(context).toString());
    expression = parser.parseExpression("#sum(1,2,3)");
    assertThat(expression.getValue(context)).isEqualTo(6);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(6);
    expression = parser.parseExpression("#sum(2)");
    assertThat(expression.getValue(context)).isEqualTo(2);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(2);
    expression = parser.parseExpression("#sum()");
    assertThat(expression.getValue(context)).isEqualTo(0);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(0);
    expression = parser.parseExpression("#sum(#intArray)");
    assertThat(expression.getValue(context)).isEqualTo(20);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(20);
    expression = parser.parseExpression("#sumDouble(1.0d,2.0d,3.0d)");
    assertThat(expression.getValue(context)).isEqualTo(6);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(6);
    expression = parser.parseExpression("#sumDouble(2.0d)");
    assertThat(expression.getValue(context)).isEqualTo(2);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(2);
    expression = parser.parseExpression("#sumDouble()");
    assertThat(expression.getValue(context)).isEqualTo(0);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(0);
    expression = parser.parseExpression("#sumDouble(#doubleArray)");
    assertThat(expression.getValue(context)).isEqualTo(20);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(20);
    expression = parser.parseExpression("#sumFloat(1.0f,2.0f,3.0f)");
    assertThat(expression.getValue(context)).isEqualTo(6);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(6);
    expression = parser.parseExpression("#sumFloat(2.0f)");
    assertThat(expression.getValue(context)).isEqualTo(2);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(2);
    expression = parser.parseExpression("#sumFloat()");
    assertThat(expression.getValue(context)).isEqualTo(0);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(0);
    expression = parser.parseExpression("#sumFloat(#floatArray)");
    assertThat(expression.getValue(context)).isEqualTo(20);
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo(20);
    expression = parser.parseExpression("#appendChar('abc'.charAt(0),'abc'.charAt(1))");
    assertThat(expression.getValue(context)).isEqualTo("ab");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo("ab");
    expression = parser.parseExpression("#append4('a','b','c')");
    assertThat(expression.getValue(context).toString()).isEqualTo("a::bc");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("a::bc");
    expression = parser.parseExpression("#append4('a','b')");
    assertThat(expression.getValue(context).toString()).isEqualTo("a::b");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("a::b");
    expression = parser.parseExpression("#append4('a')");
    assertThat(expression.getValue(context).toString()).isEqualTo("a::");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("a::");
    expression = parser.parseExpression("#append4('a',#stringArray)");
    assertThat(expression.getValue(context).toString()).isEqualTo("a::xyz");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context).toString()).isEqualTo("a::xyz");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) Test(org.junit.jupiter.api.Test)

Example 3 with SpelNodeImpl

use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-infrastructure 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 4 with SpelNodeImpl

use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-infrastructure by TAKETODAY.

the class InternalSpelExpressionParser method eatPrimaryExpression.

// primaryExpression : startNode (node)? -> ^(EXPRESSION startNode (node)?);
@Nullable
private SpelNodeImpl eatPrimaryExpression() {
    // always a start node
    SpelNodeImpl start = eatStartNode();
    ArrayList<SpelNodeImpl> nodes = null;
    SpelNodeImpl node = eatNode();
    while (node != null) {
        if (nodes == null) {
            nodes = new ArrayList<>(4);
            nodes.add(start);
        }
        nodes.add(node);
        node = eatNode();
    }
    if (start == null || nodes == null) {
        return start;
    }
    return new CompoundExpression(start.getStartPosition(), nodes.get(nodes.size() - 1).getEndPosition(), nodes.toArray(new SpelNodeImpl[0]));
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) CompoundExpression(cn.taketoday.expression.spel.ast.CompoundExpression) Nullable(cn.taketoday.lang.Nullable)

Example 5 with SpelNodeImpl

use of cn.taketoday.expression.spel.ast.SpelNodeImpl in project today-infrastructure by TAKETODAY.

the class InternalSpelExpressionParser method maybeEatProjection.

// projection: PROJECT^ expression RCURLY!;
private boolean maybeEatProjection(boolean nullSafeNavigation) {
    Token t = peekToken();
    if (!peekToken(TokenKind.PROJECT, true)) {
        return false;
    }
    Assert.state(t != null, "No token");
    SpelNodeImpl expr = eatExpression();
    Assert.state(expr != null, "No node");
    eatToken(TokenKind.RSQUARE);
    this.constructedNodes.push(new Projection(nullSafeNavigation, t.startPos, t.endPos, expr));
    return true;
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) Projection(cn.taketoday.expression.spel.ast.Projection)

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