private void compile(Expression expression) { assertTrue(SpelCompiler.compile(expression)); }
/** * Perform expression compilation. This will only succeed once exit descriptors for all nodes have * been determined. If the compilation fails and has failed more than 100 times the expression is * no longer considered suitable for compilation. */ public boolean compileExpression() { if (this.failedAttempts > FAILED_ATTEMPTS_THRESHOLD) { // Don't try again return false; } if (this.compiledAst == null) { synchronized (this.expression) { // Possibly compiled by another thread before this thread got into the sync block if (this.compiledAst != null) { return true; } SpelCompiler compiler = SpelCompiler.getCompiler(this.configuration.getCompilerClassLoader()); this.compiledAst = compiler.compile(this.ast); if (this.compiledAst == null) { this.failedAttempts++; } } } return (this.compiledAst != null); }
private void assertCanCompile(Expression expression) { assertTrue(SpelCompiler.compile(expression)); }
private void assertCantCompile(Expression expression) { assertFalse(SpelCompiler.compile(expression)); }
@Test public void booleanLiteral() throws Exception { expression = parser.parseExpression("true"); boolean resultI = expression.getValue(1, Boolean.TYPE); assertEquals(true, resultI); assertTrue(SpelCompiler.compile(expression)); boolean resultC = expression.getValue(1, Boolean.TYPE); assertEquals(true, resultC); expression = parser.parseExpression("false"); resultI = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultI); assertTrue(SpelCompiler.compile(expression)); resultC = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultC); }
/** * Perform expression compilation. This will only succeed once exit descriptors for all nodes have * been determined. If the compilation fails and has failed more than 100 times the expression is * no longer considered suitable for compilation. */ public boolean compileExpression() { if (this.failedAttempts > FAILED_ATTEMPTS_THRESHOLD) { // Don't try again return false; } if (this.compiledAst == null) { synchronized (this.expression) { // Possibly compiled by another thread before this thread got into the sync block if (this.compiledAst != null) { return true; } SpelCompiler compiler = SpelCompiler.getCompiler(this.configuration.getCompilerClassLoader()); this.compiledAst = compiler.compile(this.ast); if (this.compiledAst == null) { this.failedAttempts++; } } } return (this.compiledAst != null); }
Expression expression = parser.parseExpression("false and false"); boolean resultI = expression.getValue(1, Boolean.TYPE); SpelCompiler.compile(expression); boolean resultC = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultI); SpelCompiler.compile(expression); resultC = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultI); SpelCompiler.compile(expression); resultC = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultI); SpelCompiler.compile(expression); resultC = expression.getValue(1, Boolean.TYPE); assertEquals(true, resultI);
@Test public void mapAccessorCompilable() { Map<String, Object> testMap = getSimpleTestMap(); StandardEvaluationContext sec = new StandardEvaluationContext(); sec.addPropertyAccessor(new MapAccessor()); SpelExpressionParser sep = new SpelExpressionParser(); // basic Expression ex = sep.parseExpression("foo"); assertEquals("bar",ex.getValue(sec,testMap)); assertTrue(SpelCompiler.compile(ex)); assertEquals("bar",ex.getValue(sec,testMap)); // compound expression ex = sep.parseExpression("foo.toUpperCase()"); assertEquals("BAR",ex.getValue(sec,testMap)); assertTrue(SpelCompiler.compile(ex)); assertEquals("BAR",ex.getValue(sec,testMap)); // nested map Map<String,Map<String,Object>> nestedMap = getNestedTestMap(); ex = sep.parseExpression("aaa.foo.toUpperCase()"); assertEquals("BAR",ex.getValue(sec,nestedMap)); assertTrue(SpelCompiler.compile(ex)); assertEquals("BAR",ex.getValue(sec,nestedMap)); // avoiding inserting checkcast because first part of expression returns a Map ex = sep.parseExpression("getMap().foo"); MapGetter mapGetter = new MapGetter(); assertEquals("bar",ex.getValue(sec,mapGetter)); assertTrue(SpelCompiler.compile(ex)); assertEquals("bar",ex.getValue(sec,mapGetter)); }
Expression expression = parser.parseExpression("false or false"); boolean resultI = expression.getValue(1, Boolean.TYPE); SpelCompiler.compile(expression); boolean resultC = expression.getValue(1, Boolean.TYPE); assertEquals(false, resultI);
/** * Perform expression compilation. This will only succeed once exit descriptors for all nodes have * been determined. If the compilation fails and has failed more than 100 times the expression is * no longer considered suitable for compilation. */ public boolean compileExpression() { if (this.failedAttempts > FAILED_ATTEMPTS_THRESHOLD) { // Don't try again return false; } if (this.compiledAst == null) { synchronized (this.expression) { // Possibly compiled by another thread before this thread got into the sync block if (this.compiledAst != null) { return true; } SpelCompiler compiler = SpelCompiler.getCompiler(this.configuration.getCompilerClassLoader()); this.compiledAst = compiler.compile(this.ast); if (this.compiledAst == null) { this.failedAttempts++; } } } return (this.compiledAst != null); }