Search in sources :

Example 26 with CompiledExpression

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

the class CoreConfidenceTests method testArray.

public void testArray() {
    String ex = " TestHelper.method(1, new String[]{\"a\", \"b\"});\n" + " TestHelper.method(2, new String[]{new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, new Fooz[]{new Fooz(\"a\"), new Fooz(\"b\")});";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(TestHelper.class);
    ctx.addImport(Fooz.class);
    ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
    OptimizerFactory.setDefaultOptimizer("ASM");
    CompiledExpression expr = compiler.compile();
    executeExpression(expr);
    OptimizerFactory.setDefaultOptimizer("reflective");
    expr = compiler.compile();
    executeExpression(expr);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 27 with CompiledExpression

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

the class CoreConfidenceTests method testTestIntToLong.

public void testTestIntToLong() {
    String s = "1+(long)a";
    ParserContext pc = new ParserContext();
    pc.addInput("a", Integer.class);
    ExpressionCompiler compiler = new ExpressionCompiler(s, pc);
    CompiledExpression expr = compiler.compile();
    Map vars = new HashMap();
    vars.put("a", 1);
    Object r = ((ExecutableStatement) expr).getValue(null, new MapVariableResolverFactory(vars));
    assertEquals(new Long(2), r);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 28 with CompiledExpression

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

the class DebuggerTests method testDebuggerInvoke.

public void testDebuggerInvoke() {
    count = 0;
    MVELRuntime.resetDebugger();
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            if (frame.getFactory().isResolveable("a1")) {
                a1++;
            }
            if (frame.getFactory().isResolveable("a4")) {
                a4++;
                System.out.println("HEI " + frame.getLineNumber());
            }
            count++;
            return 0;
        }
    });
    String src = "a1=7;\na2=8;\na3=9;\na4=10;\na5=11;\na6=12;\na7=13;\na8=14;";
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("mysource");
    ctx.setDebugSymbols(true);
    ExpressionCompiler c = new ExpressionCompiler(src, ctx);
    CompiledExpression compexpr = c.compile();
    System.out.println(decompile(compexpr));
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 7);
    VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
    MVEL.executeDebugger(compexpr, null, factory);
    System.out.println(a1);
    System.out.println(a4);
    System.out.println(count);
    assertEquals(2, a1);
    // test passes but the breakpoint should be received by line 7, not by line 3
    assertEquals(1, a4);
    // three breakpoints FAILS
    assertEquals(3, count);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 29 with CompiledExpression

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

the class DebuggerTests method testBreakpointsAcrossComments2.

public void testBreakpointsAcrossComments2() {
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    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" + " a + b", // 10
    ctx);
    CompiledExpression compiled = compiler.compile();
    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 30 with CompiledExpression

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

the class DebuggerTests method testDebuggerInvoke2.

public void testDebuggerInvoke2() {
    count = 0;
    MVELRuntime.resetDebugger();
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            count++;
            return 0;
        }
    });
    String src = "a1=7;\na2=8;\nSystem.out.println(\"h\");\nac=23;\nde=23;\nge=23;\ngef=34;";
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("mysource");
    ctx.setDebugSymbols(true);
    ExpressionCompiler c = new ExpressionCompiler(src, ctx);
    CompiledExpression compexpr = c.compile();
    System.out.println(decompile(compexpr));
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 2);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 4);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 5);
    VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
    MVEL.executeDebugger(compexpr, null, factory);
    System.out.println(count);
    assertEquals(5, count);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

CompiledExpression (org.mvel2.compiler.CompiledExpression)81 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)65 ParserContext (org.mvel2.ParserContext)48 HashMap (java.util.HashMap)20 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)20 Foo (org.mvel2.tests.core.res.Foo)20 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)15 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)14 DataType (org.mule.runtime.api.metadata.DataType)11 HashSet (java.util.HashSet)10 Test (org.junit.Test)9 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 ParserContext (org.mule.mvel2.ParserContext)8 Map (java.util.Map)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6 CompileException (org.mvel2.CompileException)6 ASTNode (org.mvel2.ast.ASTNode)6 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)6