Search in sources :

Example 66 with MapVariableResolverFactory

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

the class CoreConfidenceTests method analyzeBigDecimalOperation.

private void analyzeBigDecimalOperation(String expression, BigDecimal a, BigDecimal b, BigDecimal expected) {
    ParserContext pctx = new ParserContext();
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("a", BigDecimal.class);
    pctx.addInput("b", BigDecimal.class);
    Serializable compiledExpr = MVEL.compileExpression(expression, pctx);
    AtomicInteger i = new AtomicInteger(10);
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("a", a);
    factory.createVariable("b", b);
    if (a == null || b == null) {
        try {
            MVEL.executeExpression(compiledExpr, null, factory);
            fail("should throw a NPE");
        } catch (NullPointerException npe) {
        // expected
        }
    } else {
        Object result = MVEL.executeExpression(compiledExpr, null, factory);
        assertEquals(expected, result);
    }
}
Also used : Serializable(java.io.Serializable) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IndexedVariableResolverFactory(org.mvel2.integration.impl.IndexedVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext)

Example 67 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testForLoopWithSpaces.

public void testForLoopWithSpaces() {
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("strings", Arrays.asList("test"));
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("strings", List.class);
    String expression = "for (   String   s : strings ) {\n" + "  return s;\n" + "}";
    Serializable compiledExpr = MVEL.compileExpression(expression, pctx);
    assertEquals("test", MVEL.executeExpression(compiledExpr, null, factory));
}
Also used : Serializable(java.io.Serializable) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IndexedVariableResolverFactory(org.mvel2.integration.impl.IndexedVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 68 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testCheeseConstructor.

// public void testClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(HashMap.class);
// 
// ResolverTools.appendFactory(mvf, classes);
// 
// assertTrue(executeExpression(compileExpression("HashMap map = new HashMap()",
// classes.getImportedClasses()),
// mvf) instanceof HashMap);
// }
// 
// public void testSataticClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(Person.class);
// 
// ResolverTools.appendFactory(mvf,
// classes);
// 
// assertEquals("tom",
// executeExpression(compileExpression("p = new Person('tom'); return p.name;",
// classes.getImportedClasses()),
// mvf));
// }
public void testCheeseConstructor() {
    MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
    ClassImportResolverFactory classes = new ClassImportResolverFactory(null, null, false);
    classes.addClass(Cheese.class);
    ResolverTools.appendFactory(mvf, classes);
    assertTrue(executeExpression(compileExpression("cheese = new Cheese(\"cheddar\", 15);", classes.getImportedClasses()), mvf) instanceof Cheese);
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) Cheese(org.mvel2.tests.core.res.Cheese) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Example 69 with MapVariableResolverFactory

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

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";
        }
    });
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    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), ctx);
    // compiler.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile();
    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) Macro(org.mvel2.Macro) 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) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor)

Example 70 with MapVariableResolverFactory

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

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);
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
    CompiledExpression compiled = compiler.compile();
    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)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)79 HashMap (java.util.HashMap)43 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)29 ParserContext (org.mvel2.ParserContext)24 Serializable (java.io.Serializable)18 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)17 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)14 TemplateRegistry (org.mvel2.templates.TemplateRegistry)13 Debugger (org.mvel2.debug.Debugger)12 Frame (org.mvel2.debug.Frame)12 HashSet (java.util.HashSet)10 MapObject (org.mvel2.tests.core.res.MapObject)10 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)9 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 Test (org.junit.Test)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6