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();
}
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");
}
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");
}
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]));
}
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;
}
Aggregations