Search in sources :

Example 21 with ExpressionCompiler

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

the class DebuggerTests method testBreakpointsAcrossComments2.

public void testBreakpointsAcrossComments2() {
    ExpressionCompiler compiler = new ExpressionCompiler(// 1
    "// This is a comment\n" + // 2
    "//Second comment line\n" + // 3
    "//Third Comment Line\n" + // 4
    "\n" + // 5
    "//Test\n" + // 6
    "System.out.println('4');\n" + // 7
    "//System.out.println('5'); \n" + // 8
    "a = 0;\n" + // 9
    "b = 1;\n" + // 10
    " a + b");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    MVELRuntime.registerBreakpoint("test2.mv", 6);
    MVELRuntime.registerBreakpoint("test2.mv", 8);
    MVELRuntime.registerBreakpoint("test2.mv", 9);
    MVELRuntime.registerBreakpoint("test2.mv", 10);
    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(1, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertEquals("did not break at expected lines", Make.Set.<Integer>$()._(6)._(8)._(9)._(10)._(), breaked);
}
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 22 with ExpressionCompiler

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

the class DebuggerTests method testDebugSymbolsWithWindowsLinedEndings.

public void testDebugSymbolsWithWindowsLinedEndings() throws Exception {
    String expr = "   System.out.println( \"a1\" );\r\n" + "   System.out.println( \"a2\" );\r\n" + "   System.out.println( \"a3\" );\r\n" + "   System.out.println( \"a4\" );\r\n";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setDebugSymbols(true);
    ctx.setSourceFile("mysource");
    String s = org.mvel2.debug.DebugTools.decompile(compiler.compile(ctx));
    System.out.println(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 23 with ExpressionCompiler

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

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

the class FunctionsTest method testFunctionDefAndCall2.

public void testFunctionDefAndCall2() {
    ExpressionCompiler compiler = new ExpressionCompiler("function heyFoo() { return 'Foobar'; };\n" + "return heyFoo() + heyFoo();");
    Serializable s = compiler.compile();
    Map<String, Function> m = extractAllDeclaredFunctions((CompiledExpression) s);
    assertTrue(m.containsKey("heyFoo"));
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    OptimizerFactory.setDefaultOptimizer("dynamic");
}
Also used : Function(org.mvel2.ast.Function) Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 25 with ExpressionCompiler

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

the class TypesAndInferenceTests method testMultiTypeVarDeclr.

public void testMultiTypeVarDeclr() {
    String ex = "String a, b, c";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    compiler.compile(ctx);
    assertNotNull(ctx.getVariables());
    assertEquals(3, ctx.getVariables().entrySet().size());
    for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
        assertEquals(String.class, entry.getValue());
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)110 CompiledExpression (org.mvel2.compiler.CompiledExpression)33 ParserContext (org.mvel2.ParserContext)31 Serializable (java.io.Serializable)15 Foo (org.mvel2.tests.core.res.Foo)12 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)11 HashMap (java.util.HashMap)9 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)9 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashSet (java.util.HashSet)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)5 List (java.util.List)4 Interceptor (org.mvel2.integration.Interceptor)4 Map (java.util.Map)3 ASTNode (org.mvel2.ast.ASTNode)3 WithNode (org.mvel2.ast.WithNode)3 Type (java.lang.reflect.Type)2 LinkedHashMap (java.util.LinkedHashMap)2 Macro (org.mvel2.Macro)2