Search in sources :

Example 1 with Macro

use of org.mvel2.Macro in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupport.

public void testMacroSupport() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("modify (foo) { aValue = 'poo = poo', bValue = 'poo, poo' }; mod", macros));
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    assertEquals("FOOBAR!", executeExpression(compiler.compile(ctx), null, vars));
}
Also used : HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) Interceptor(org.mvel2.integration.Interceptor)

Example 2 with Macro

use of org.mvel2.Macro in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupportWithStrings.

public void testMacroSupportWithStrings() {
    Map<String, Object> vars = new HashMap<String, Object>();
    Foo foo = new Foo();
    vars.put("foo", foo);
    Map<String, Macro> macros = new HashMap<String, Macro>();
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "drools.modify";
        }
    });
    assertEquals("", foo.aValue);
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("\"This is an modify()\"", macros));
    ParserContext ctx = new ParserContext(null, null, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    assertEquals("This is an modify()", executeExpression(compiler.compile(ctx), null, vars));
}
Also used : HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 3 with Macro

use of org.mvel2.Macro 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 4 with Macro

use of org.mvel2.Macro in project mvel by mikebrock.

the class DebuggerTests method testBreakpoints5.

public void testBreakpoints5() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    String expression = "System.out.println('foo');\r\n" + "a = new Foo244();\r\n" + "a.name = 'bar';\r\n" + "foo.happy();\r\n" + "System.out.println( 'name:' + a.name );               \r\n" + "System.out.println( 'name:' + a.name );         \r\n" + "System.out.println( 'name:' + a.name );     \r\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    System.out.println(DebugTools.decompile(compiled));
    MVELRuntime.registerBreakpoint("test2.mv", 1);
    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 Debugger.STEP_OVER;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    System.out.println("\n==RUN==\n");
    assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not break at line 1", breaked.contains(1));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) CompiledExpression(org.mvel2.compiler.CompiledExpression) 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 5 with Macro

use of org.mvel2.Macro in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupportWithDebugging.

public void testMacroSupportWithDebugging() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("System.out.println('hello');\n" + "System.out.println('bye');\n" + "modify (foo) { aValue = 'poo', \n" + " aValue = 'poo' };\n mod", macros));
    // compiler.setDebugSymbols(true);
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println(frame.getSourceName() + ":" + frame.getLineNumber());
            return Debugger.STEP;
        }
    });
    MVELRuntime.registerBreakpoint("test.mv", 3);
    System.out.println(DebugTools.decompile(compiled));
    Assert.assertEquals("FOOBAR!", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(vars)));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) 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) Interceptor(org.mvel2.integration.Interceptor)

Aggregations

HashMap (java.util.HashMap)6 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)5 Interceptor (org.mvel2.integration.Interceptor)4 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)4 Macro (org.mvel2.Macro)3 ASTNode (org.mvel2.ast.ASTNode)3 WithNode (org.mvel2.ast.WithNode)3 CompiledExpression (org.mvel2.compiler.CompiledExpression)3 Debugger (org.mvel2.debug.Debugger)3 Frame (org.mvel2.debug.Frame)3 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)3 Foo (org.mvel2.tests.core.res.Foo)3 HashSet (java.util.HashSet)2 ParserContext (org.mvel2.ParserContext)2 Map (java.util.Map)1 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)1