Search in sources :

Example 6 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class FunctionsTest method testMVEL225.

public void testMVEL225() {
    Serializable compileExpression = MVEL.compileExpression("def f() { int a=1;a++;return a; }; f();");
    MapVariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    assertEquals(2, MVEL.executeExpression(compileExpression, factory));
}
Also used : Serializable(java.io.Serializable) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 7 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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 8 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class CoreConfidenceTests method testSysoutNullVariable.

public void testSysoutNullVariable() {
    // Create our root Map object
    Map<String, String> map = new HashMap<String, String>();
    map.put("foo", null);
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("this", map);
    org.mvel2.MVEL.executeExpression(org.mvel2.MVEL.compileExpression("System.out.println(foo);"), map, factory);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 9 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class CoreConfidenceTests method testMVEL187.

public void testMVEL187() {
    ParserContext context = new ParserContext();
    context.addPackageImport("test");
    context.addInput("outer", Outer.class);
    Object compiled = MVEL.compileExpression("outer.getInner().getValue()", context);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("outer", new Outer());
    VariableResolverFactory varsResolver = new MapVariableResolverFactory(vars);
    assertEquals(2, executeExpression(compiled, varsResolver));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) Outer(org.mvel2.tests.core.res.res2.Outer)

Example 10 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class CoreConfidenceTests method testThisReferenceMapVirtualObjects1.

// compiled - reflective
public void testThisReferenceMapVirtualObjects1() {
    // Create our root Map object
    Map<String, String> map = new HashMap<String, String>();
    map.put("foo", "bar");
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("this", map);
    OptimizerFactory.setDefaultOptimizer("reflective");
    // Run test
    assertEquals(true, executeExpression(compileExpression("this.foo == 'bar'"), map, factory));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 HashMap (java.util.HashMap)8 ParserContext (org.mvel2.ParserContext)8 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)8 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)7 Debugger (org.mvel2.debug.Debugger)6 Frame (org.mvel2.debug.Frame)6 HashSet (java.util.HashSet)5 Serializable (java.io.Serializable)3 Interceptor (org.mvel2.integration.Interceptor)3 Iterator (java.util.Iterator)2 Macro (org.mvel2.Macro)2 ASTNode (org.mvel2.ast.ASTNode)2 WithNode (org.mvel2.ast.WithNode)2 ClassImportResolverFactory (org.mvel2.integration.impl.ClassImportResolverFactory)2 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)2 ArrayIterator (org.mvel2.templates.util.ArrayIterator)2 File (java.io.File)1