@Test public void testTypeReferencesAndQualifiedIdentifierCaching() { SpelExpression e = (SpelExpression) parser.parseExpression("T(java.lang.String)"); assertFalse(e.isWritable(new StandardEvaluationContext())); assertEquals("T(java.lang.String)", e.toStringAST()); assertEquals(String.class, e.getValue(Class.class)); // use cached QualifiedIdentifier: assertEquals("T(java.lang.String)", e.toStringAST()); assertEquals(String.class, e.getValue(Class.class)); }
@Override @Nullable public Object getValue(Object rootObject) throws EvaluationException { if (this.compiledAst != null) { try { return this.compiledAst.getValue(rootObject, getEvaluationContext()); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
/** * Request that an attempt is made to compile the specified expression. It may fail if * components of the expression are not suitable for compilation or the data types * involved are not suitable for compilation. Used for testing. * @return true if the expression was successfully compiled */ public static boolean compile(Expression expression) { return (expression instanceof SpelExpression && ((SpelExpression) expression).compileExpression()); }
@Override @Nullable public Class<?> getValueType(Object rootObject) throws EvaluationException { return getValueType(getEvaluationContext(), rootObject); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor() throws EvaluationException { return getValueTypeDescriptor(getEvaluationContext()); }
@Override public void setValue(Object rootObject, @Nullable Object value) throws EvaluationException { this.ast.setValue( new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration), value); }
@Test public void testSimpleOperations() throws Exception { // no built in support for this: evaluateAndCheckError("'abc'-true",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); eContext.setOperatorOverloader(new StringAndBooleanAddition()); SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'+true"); assertEquals("abctrue",expr.getValue(eContext)); expr = (SpelExpression)parser.parseExpression("'abc'-true"); assertEquals("abc",expr.getValue(eContext)); expr = (SpelExpression)parser.parseExpression("'abc'+null"); assertEquals("abcnull",expr.getValue(eContext)); }
@SuppressWarnings("ConstantConditions") @Override public int compare(T arg1, T arg2) { SpelExpression expressionToUse = getExpression(); expressionToUse.getEvaluationContext().setVariable("arg1", arg1); expressionToUse.getEvaluationContext().setVariable("arg2", arg2); return expressionToUse.getValue(Integer.class) * (asc ? 1 : -1); }
@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)); }
@Test public void projectionTypeDescriptors_1() { StandardEvaluationContext context = new StandardEvaluationContext(new C()); SpelExpressionParser parser = new SpelExpressionParser(); String el1 = "ls.![#this.equals('abc')]"; SpelExpression exp = parser.parseRaw(el1); List<?> value = (List<?>) exp.getValue(context); // value is list containing [true,false] assertEquals(Boolean.class, value.get(0).getClass()); TypeDescriptor evaluated = exp.getValueTypeDescriptor(context); assertEquals(null, evaluated.getElementTypeDescriptor()); }
@Override @Nullable public Object getValue() throws EvaluationException { if (this.compiledAst != null) { try { EvaluationContext context = getEvaluationContext(); return this.compiledAst.getValue(context.getRootObject().getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(getEvaluationContext(), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
@Test public void valueType() { SpelExpressionParser parser = new SpelExpressionParser(); EvaluationContext ctx = new StandardEvaluationContext(); Class<?> c = parser.parseRaw("2").getValueType(); assertEquals(Integer.class, c); c = parser.parseRaw("12").getValueType(ctx); assertEquals(Integer.class, c); c = parser.parseRaw("null").getValueType(); assertNull(c); c = parser.parseRaw("null").getValueType(ctx); assertNull(c); Object o = parser.parseRaw("null").getValue(ctx, Integer.class); assertNull(o); }
@Test public void validateWithExternalTemplate() { RedisStoreMessageSource withExternalTemplate = TestUtils.getPropertyValue(context.getBean("withExternalTemplate"), "source", RedisStoreMessageSource.class); assertEquals("'presidents'", ((SpelExpression) TestUtils.getPropertyValue(withExternalTemplate, "keyExpression")).getExpressionString()); assertEquals("LIST", ((CollectionType) TestUtils.getPropertyValue(withExternalTemplate, "collectionType")).toString()); assertSame(redisTemplate, TestUtils.getPropertyValue(withExternalTemplate, "redisTemplate")); }
@Test public void compilationOfBasicNullSafeMethodReference() { SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader())); SpelExpression expression = parser.parseRaw("#it?.equals(3)"); StandardEvaluationContext context = new StandardEvaluationContext(new Object[] {1}); context.setVariable("it", 3); expression.setEvaluationContext(context); assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); assertCanCompile(expression); context.setVariable("it", 3); assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); }
@Override @Nullable public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(rootObject, context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
private SpelNodeImpl getAst() { SpelExpression spelExpression = (SpelExpression) expression; SpelNode ast = spelExpression.getAST(); return (SpelNodeImpl)ast; }
context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class)); ((SpelExpression) expression).setEvaluationContext(context); assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class)); context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class)); ((SpelExpression) expression).setEvaluationContext(context); assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class)); DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class)); context.setVariable("arg", "there"); ((SpelExpression) expression).setEvaluationContext(context); assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()); assertCanCompile(expression); assertEquals("hey there", expression.getValue(String.class));
@Override @Nullable public Object getValue(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(context.getRootObject().getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
@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(); } }