Search in sources :

Example 1 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class ForNode method buildForEach.

private boolean buildForEach(char[] condition, int start, int offset, int blockStart, int blockEnd, int fields, ParserContext pCtx) {
    int end = start + offset;
    int cursor = nextCondPart(condition, start, end, false);
    boolean varsEscape = false;
    try {
        ParserContext spCtx = pCtx;
        if (pCtx != null) {
            spCtx = pCtx.createSubcontext().createColoringSubcontext();
        } else {
            spCtx = new ParserContext();
        }
        this.initializer = (ExecutableStatement) subCompileExpression(condition, start, cursor - start - 1, spCtx);
        if (pCtx != null) {
            pCtx.pushVariableScope();
        }
        try {
            expectType(this.condition = (ExecutableStatement) subCompileExpression(condition, start = cursor, (cursor = nextCondPart(condition, start, end, false)) - start - 1, spCtx), Boolean.class, ((fields & COMPILE_IMMEDIATE) != 0));
        } catch (CompileException e) {
            if (e.getExpr().length == 0) {
                e.setExpr(expr);
                while (start < expr.length && ParseTools.isWhitespace(expr[start])) {
                    start++;
                }
                e.setCursor(start);
            }
            throw e;
        }
        this.after = (ExecutableStatement) subCompileExpression(condition, start = cursor, (nextCondPart(condition, start, end, true)) - start, spCtx);
        if (spCtx != null && (fields & COMPILE_IMMEDIATE) != 0 && spCtx.isVariablesEscape()) {
            if (pCtx != spCtx)
                pCtx.addVariables(spCtx.getVariables());
            varsEscape = true;
        } else if (spCtx != null && pCtx != null) {
            pCtx.addVariables(spCtx.getVariables());
        }
        this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockEnd, spCtx);
    } catch (NegativeArraySizeException e) {
        throw new CompileException("wrong syntax; did you mean to use 'foreach'?", expr, start);
    }
    return varsEscape;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext)

Example 2 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class WithTests method testNewUsingWith.

public void testNewUsingWith() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addImport(Bar.class);
    Serializable s = compileExpression("[ 'foo' : (with ( new Foo() )" + " { bar = with ( new Bar() ) { name = 'ziggy' } }) ]", ctx);
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("ziggy", (((Foo) ((Map) executeExpression(s)).get("foo")).getBar().getName()));
}
Also used : Serializable(java.io.Serializable) ParserContext(org.mvel2.ParserContext)

Example 3 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class WithTests method testExecuteCoercionTwice.

public void testExecuteCoercionTwice() {
    OptimizerFactory.setDefaultOptimizer("reflective");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    vars.put("$value", new Long(5));
    ExpressionCompiler compiler = new ExpressionCompiler("with (foo) { countTest = $value };");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    executeExpression(compiled, null, vars);
    executeExpression(compiled, null, vars);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 4 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class WithTests method testExecuteCoercionTwice2.

public void testExecuteCoercionTwice2() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    vars.put("$value", new Long(5));
    ExpressionCompiler compiler = new ExpressionCompiler("with (foo) { countTest = $value };");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    executeExpression(compiled, null, vars);
    executeExpression(compiled, null, vars);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 5 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mikebrock.

the class WithTests method testInlineWith5.

public void testInlineWith5() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    ParserContext pCtx = new ParserContext();
    pCtx.setStrongTyping(true);
    pCtx.addInput("foo", Foo.class);
    CompiledExpression expr = new ExpressionCompiler("foo.{name='poopy', aValue='bar'}").compile(pCtx);
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10