private SpelNodeImpl getAst() { SpelExpression spelExpression = (SpelExpression) expression; SpelNode ast = spelExpression.getAST(); return (SpelNodeImpl)ast; }
private Operator getOperatorNode(SpelExpression expr) { SpelNode node = expr.getAST(); return findOperator(node); }
/** * Output an indented representation of the expression syntax tree to the specified output stream. * @param printStream the output stream to print into * @param expression the expression to be displayed */ public static void printAbstractSyntaxTree(PrintStream printStream, Expression expression) { printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST start"); printAST(printStream, ((SpelExpression) expression).getAST(), ""); printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST end"); }
@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])"); assertEquals("-1", expression.getValue(context, Integer.class).toString()); // Selection isn't compilable. assertFalse(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); }
/** * Transforms the given SpEL expression to a corresponding MongoDB expression against the given * {@link AggregationOperationContext} {@code context}. * <p> * Exposes the given @{code params} as <code>[0] ... [n]</code>. * * @param expression must not be {@literal null} * @param context must not be {@literal null} * @param params must not be {@literal null} * @return */ public Object transform(String expression, AggregationOperationContext context, Object... params) { Assert.notNull(expression, "Expression must not be null!"); Assert.notNull(context, "AggregationOperationContext must not be null!"); Assert.notNull(params, "Parameters must not be null!"); SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression); ExpressionState state = new ExpressionState(new StandardEvaluationContext(params), CONFIG); ExpressionNode node = ExpressionNode.from(spelExpression.getAST(), state); return transform(new AggregationExpressionTransformationContext<ExpressionNode>(node, null, null, context)); }
@Test public void theMostBasic() { SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expr = parser.parseRaw("2"); assertNotNull(expr); assertNotNull(expr.getAST()); assertEquals(2, expr.getValue()); assertEquals(Integer.class, expr.getValueType()); assertEquals(2, expr.getAST().getValue(null)); }
assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class)); assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class)); assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class));
@Test public void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() throws Exception { Payload payload = new Payload(); expression = parser.parseExpression("DR[0].three"); Object v = expression.getValue(payload); assertEquals("Lorg/springframework/expression/spel/SpelCompilationCoverageTests$Three", getAst().getExitDescriptor()); Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null"); v = expression.getValue(payload); SpelExpression sExpr = (SpelExpression) expression; Ternary ternary = (Ternary) sExpr.getAST(); OpLT oplt = (OpLT) ternary.getChild(0); CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand(); String cExprExitDescriptor = cExpr.getExitDescriptor(); assertEquals("D", cExprExitDescriptor); assertEquals("Z", oplt.getExitDescriptor()); assertCanCompile(expression); Object vc = expression.getValue(payload); assertEquals(payload, v); assertEquals(payload,vc); payload.DR[0].three.four = 0.13d; vc = expression.getValue(payload); assertNull(vc); }
assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("abc", expression.getValue(context).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("a", expression.getValue(context).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("", expression.getValue(context).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("xyz", expression.getValue(context).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("xyz", expression.getValue(context,new SomeCompareMethod2()).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("abc", expression.getValue(context).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("ab", expression.getValue(context, new SomeCompareMethod2()).toString()); assertTrue(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable());
private void checkConstantMap(String expressionText, boolean expectedToBeConstant) { SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText); SpelNode node = expression.getAST(); assertTrue(node instanceof InlineMap); InlineMap inlineMap = (InlineMap) node; if (expectedToBeConstant) { assertTrue(inlineMap.isConstant()); } else { assertFalse(inlineMap.isConstant()); } }
@Test public void arithmeticPlus1() { SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expr = parser.parseRaw("2+2"); assertNotNull(expr); assertNotNull(expr.getAST()); assertEquals(4, expr.getValue()); }
@Test public void arithmeticMultiply1() { SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expr = parser.parseRaw("2*3"); assertNotNull(expr); assertNotNull(expr.getAST()); // printAst(expr.getAST(),0); assertEquals(6, expr.getValue()); }
private void checkConstantList(String expressionText, boolean expectedToBeConstant) { SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText); SpelNode node = expression.getAST(); assertTrue(node instanceof InlineList); InlineList inlineList = (InlineList) node; if (expectedToBeConstant) { assertTrue(inlineList.isConstant()); } else { assertFalse(inlineList.isConstant()); } }
@Test public void positionalInformation() { SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false"); SpelNode rootAst = expr.getAST(); OpOr operatorOr = (OpOr) rootAst; OpAnd operatorAnd = (OpAnd) operatorOr.getLeftOperand(); SpelNode rightOrOperand = operatorOr.getRightOperand(); // check position for final 'false' assertEquals(17, rightOrOperand.getStartPosition()); assertEquals(22, rightOrOperand.getEndPosition()); // check position for first 'true' assertEquals(0, operatorAnd.getLeftOperand().getStartPosition()); assertEquals(4, operatorAnd.getLeftOperand().getEndPosition()); // check position for second 'true' assertEquals(9, operatorAnd.getRightOperand().getStartPosition()); assertEquals(13, operatorAnd.getRightOperand().getEndPosition()); // check position for OperatorAnd assertEquals(5, operatorAnd.getStartPosition()); assertEquals(8, operatorAnd.getEndPosition()); // check position for OperatorOr assertEquals(14, operatorOr.getStartPosition()); assertEquals(16, operatorOr.getEndPosition()); }
/** * Transforms the given SpEL expression to a corresponding MongoDB expression against the given * {@link AggregationOperationContext} {@code context}. * <p> * Exposes the given @{code params} as <code>[0] ... [n]</code>. * * @param expression must not be {@literal null} * @param context must not be {@literal null} * @param params must not be {@literal null} * @return */ public Object transform(String expression, AggregationOperationContext context, Object... params) { Assert.notNull(expression, "Expression must not be null!"); Assert.notNull(context, "AggregationOperationContext must not be null!"); Assert.notNull(params, "Parameters must not be null!"); SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression); ExpressionState state = new ExpressionState(new StandardEvaluationContext(params), CONFIG); ExpressionNode node = ExpressionNode.from(spelExpression.getAST(), state); return transform(new AggregationExpressionTransformationContext<ExpressionNode>(node, null, null, context)); }
@Override public void doWith(Method method) throws IllegalAccessException { Method methodOfOriginalClassIfProxied = findMethod(getTargetClass(bean), method.getName(), method.getParameterTypes()); if (methodOfOriginalClassIfProxied != null) { PreAuthorize preAuthorize = findAnnotation(methodOfOriginalClassIfProxied, PreAuthorize.class); PostAuthorize postAuthorize = findAnnotation(methodOfOriginalClassIfProxied, PostAuthorize.class); List<String> annotations = new ArrayList<>(2); List<String> permissions = new ArrayList<>(); if (preAuthorize != null) { annotations.add(preAuthorize.value()); } if (postAuthorize != null) { annotations.add(postAuthorize.value()); } for (String annotation : annotations) { SpelExpression expression = (SpelExpression) annotationParser.parseExpression(annotation); permissions.addAll(findPermissions(expression.getAST())); } addRoleAndPermissions(permissions); } } });
protected SpelInclusionPolicy(String expression) { SpelExpressionParser parser = new SpelExpressionParser(); this.expression = expression; this.expressionNode = parser.parseRaw(expression).getAST(); this.exp = parser.parseExpression(expression); }