Search in sources :

Example 81 with ParserContext

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

the class DebuggerTests method testDebugSymbolsSingleStatement.

public void testDebugSymbolsSingleStatement() {
    String ex = "System.out.println( Cheese.STILTON );";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(Cheese.class);
    try {
        ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
        CompiledExpression expr = compiler.compile();
        // executing the following line with a MVEL.executeExpression() works fine
        // but executeDebugger() fails
        MVEL.executeDebugger(expr, null, (VariableResolverFactory) null);
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Should not raise exception: " + e.getMessage());
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 82 with ParserContext

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

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";
    ParserContext context = new ParserContext();
    context.addImport("System", System.class);
    context.setStrictTypeEnforcement(true);
    context.setDebugSymbols(true);
    context.setSourceFile("mysource");
    ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
    String s = org.mvel2.debug.DebugTools.decompile(compiler.compile());
    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 83 with ParserContext

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

the class DebuggerTests method testBreakpoints.

public void testBreakpoints() {
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n", ctx);
    System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
    CompiledExpression compiled = compiler.compile();
    MVELRuntime.registerBreakpoint("test.mv", 7);
    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(10, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not break at line 7", breaked.contains(7));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) HashSet(java.util.HashSet)

Example 84 with ParserContext

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

the class IndexedVariablesTests method testVariableInjection1.

public void testVariableInjection1() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = k;" + "}; \n" + "for (i = 0; i < 5000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, values);
    Serializable indexCompile = MVEL.compileExpression(expr, ctx);
    Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", 10);
    map.put("y", 20);
    map.put("z", 30);
    assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory()));
// 
// for (int x = 0; x < 10; x++) {
// long tm = System.currentTimeMillis();
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(indexCompile, model.createFactory());
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(StaticInjection (ms): " + tm + ")");
// 
// tm = System.currentTimeMillis();
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("x", 10);
// map.put("y", 20);
// map.put("z", 30);
// 
// MapVariableResolverFactory factory = new MapVariableResolverFactory(map);
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(dynamicCompile, factory);
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(MapInjection    (ms): " + tm + ")");
// }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SharedVariableSpaceModel(org.mvel2.util.SharedVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 85 with ParserContext

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

the class IndexedVariablesTests method testVariableInjection3.

public void testVariableInjection3() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "def add(a,b) { a + b }; foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = add(5,10);" + "}; \n" + "for (i = 0; i < 100000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx);
    Serializable indexCompile = MVEL.compileExpression(expr, ctx);
    Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", 10);
    map.put("y", 20);
    map.put("z", 30);
    assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory(values)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) 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