Search in sources :

Example 71 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler in project mvel by mvel.

the class DebuggerTests method testBreakpointsAcrossWith.

public void testBreakpointsAcrossWith() {
    String line1 = "System.out.println( \"a1\" );\n";
    String line2 = "c = new Cheese();\n";
    String line3 = "with ( c ) { type = 'cheddar',\n" + "             price = 10 };\n";
    String line4 = "System.out.println( \"a1\" );\n";
    String expr = line1 + line2 + line3 + line4;
    System.out.println(expr);
    ParserContext context = new ParserContext();
    context.addImport("System", System.class);
    context.addImport("Cheese", Cheese.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(5, count);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 72 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler in project mvel by mvel.

the class DebuggerTests method testBreakpointsAcrossComments.

public void testBreakpointsAcrossComments() {
    String expression = // 1
    "/** This is a comment\n" + // 2
    " *  Second comment line\n" + // 3
    " *  Third Comment Line\n" + // 4
    " */\n" + // 5
    "System.out.println('4');\n" + // 6
    "System.out.println('5');\n" + // 7
    "a = 0;\n" + // 8
    "b = 1;\n" + // 9
    "a + b";
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
    System.out.println("Expression:\n------------");
    System.out.println(expression);
    System.out.println("------------");
    CompiledExpression compiled = compiler.compile();
    MVELRuntime.registerBreakpoint("test2.mv", 9);
    final Set<Integer> linesEncountered = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            linesEncountered.add(frame.getLineNumber());
            System.out.println("Breakpoint Encountered [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            System.out.println("vars:" + frame.getFactory().getKnownVariables());
            System.out.println("Resume Execution");
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals(1, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("Debugger did not break at line 9", linesEncountered.contains(9));
}
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 73 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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 74 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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 75 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)219 ParserContext (org.mvel2.ParserContext)119 CompiledExpression (org.mvel2.compiler.CompiledExpression)65 HashMap (java.util.HashMap)39 Serializable (java.io.Serializable)30 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)22 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)19 Map (java.util.Map)18 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 LinkedHashMap (java.util.LinkedHashMap)15 HashSet (java.util.HashSet)10 List (java.util.List)10 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ArrayList (java.util.ArrayList)8 CompileException (org.mvel2.CompileException)8 Interceptor (org.mvel2.integration.Interceptor)8 Macro (org.mvel2.Macro)6 ASTNode (org.mvel2.ast.ASTNode)6