Search in sources :

Example 16 with ParserContext

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

the class DebuggerTests method testBreakpoints3.

public void testBreakpoints3() {
    String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext context = new ParserContext();
    context.addImport("System", System.class);
    context.setStrictTypeEnforcement(true);
    context.setDebugSymbols(true);
    context.setSourceFile("mysource");
    String s = org.mvel2.debug.DebugTools.decompile(compiler.compile(context));
    System.out.println("output: " + s);
    int fromIndex = 0;
    int count = 0;
    while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
        count++;
    }
    assertEquals(4, count);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 17 with ParserContext

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

the class FailureTests method testShouldFail6.

public void testShouldFail6() {
    try {
        ParserContext pctx = new ParserContext();
        pctx.setStrongTyping(true);
        MVEL.compileExpression("new int[] {1.5}", pctx);
    } catch (Exception e) {
        return;
    }
    shouldThrowException();
}
Also used : ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException)

Example 18 with ParserContext

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

the class WithTests method testWithAndEnumInPackageImport.

public void testWithAndEnumInPackageImport() {
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addPackageImport(MyEnum.class.getPackage().getName());
    ParserContext pCtx = new ParserContext(pconf);
    pCtx.setStrongTyping(true);
    pCtx.addInput("thing", Thing.class);
    Thing thing = new Thing("xxx");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("thing", thing);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
    MVEL.executeExpression(stmt, null, vars);
    assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) Thing(org.mvel2.tests.core.res.Thing) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 19 with ParserContext

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

the class DebuggerTests method testBreakpoints4.

public void testBreakpoints4() {
    String expression = "System.out.println('foo');\n" + "a = new Foo244();\n" + "update (a) { name = 'bar' };\n" + "System.out.println('name:' + a.name);\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    class TestResult {

        boolean firedBefore;

        boolean firedAfter;
    }
    final TestResult result = new TestResult();
    interceptors.put("Update", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            System.out.println("fired update interceptor -- before");
            result.firedBefore = true;
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            System.out.println("fired update interceptor -- after");
            result.firedAfter = true;
            return 0;
        }
    });
    macros.put("update", new Macro() {

        public String doMacro() {
            return "@Update with";
        }
    });
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setDebugSymbols(true);
    ctx.setSourceFile("test2.mv");
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    MVELRuntime.registerBreakpoint("test2.mv", 3);
    MVELRuntime.registerBreakpoint("test2.mv", 4);
    MVELRuntime.registerBreakpoint("test2.mv", 5);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not fire before", result.firedBefore);
    assertTrue("did not fire after", result.firedAfter);
    assertEquals("did not break at expected points", Make.Set.<Integer>$()._(3)._(4)._(5)._(), breaked);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor) HashSet(java.util.HashSet)

Example 20 with ParserContext

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

the class VariableSpaceCompiler method compileShared.

public static SharedVariableSpaceModel compileShared(String expr, ParserContext pCtx, Object[] vars) {
    String[] varNames = pCtx.getIndexedVarNames();
    ParserContext analysisContext = ParserContext.create();
    analysisContext.setIndexAllocation(true);
    MVEL.analysisCompile(expr, analysisContext);
    Set<String> localNames = analysisContext.getVariables().keySet();
    pCtx.addIndexedLocals(localNames);
    String[] locals = localNames.toArray(new String[localNames.size()]);
    String[] allVars = new String[varNames.length + locals.length];
    System.arraycopy(varNames, 0, allVars, 0, varNames.length);
    System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
    return new SharedVariableSpaceModel(allVars, vars);
}
Also used : ParserContext(org.mvel2.ParserContext)

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