Search in sources :

Example 16 with Bar

use of org.mvel2.tests.core.res.Bar in project mvel by mikebrock.

the class CoreConfidenceTests method testContextObjMethodCall.

public void testContextObjMethodCall() {
    String str = "getName() == \"bob\"";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", Bar.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Bar ctx = new Bar();
    ctx.setName("bob");
    Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
    assertTrue(result);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 17 with Bar

use of org.mvel2.tests.core.res.Bar in project mvel by mikebrock.

the class CoreConfidenceTests method testThisReferenceMapVirtualObjects.

// interpreted
public void testThisReferenceMapVirtualObjects() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("foo", "bar");
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("this", map);
    assertEquals(true, eval("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)

Example 18 with Bar

use of org.mvel2.tests.core.res.Bar in project mvel by mikebrock.

the class CoreConfidenceTests method testThisReferenceMapVirtualObjects2.

// compiled - asm
public void testThisReferenceMapVirtualObjects2() {
    // 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);
    if (!Boolean.getBoolean("mvel2.disable.jit"))
        OptimizerFactory.setDefaultOptimizer("ASM");
    // 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)

Example 19 with Bar

use of org.mvel2.tests.core.res.Bar in project mvel by mikebrock.

the class IntegrationTests method testIndexedVariableFactory.

public void testIndexedVariableFactory() {
    ParserContext ctx = ParserContext.create();
    String[] vars = { "a", "b" };
    Object[] vals = { "foo", "bar" };
    ctx.setIndexAllocation(true);
    ctx.addIndexedInput(vars);
    String expr = "def myfunc(z) { a + b + z }; myfunc('poop');";
    SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, vals);
    Serializable s = MVEL.compileExpression(expr, ctx);
    //        VariableResolverFactory locals = new CachingMapVariableResolverFactory(new HashMap<String, Object>());
    //        VariableResolverFactory injected = new IndexedVariableResolverFactory(vars, vals, locals);
    assertEquals("foobarpoop", MVEL.executeExpression(s, model.createFactory()));
}
Also used : Serializable(java.io.Serializable) SharedVariableSpaceModel(org.mvel2.util.SharedVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 20 with Bar

use of org.mvel2.tests.core.res.Bar 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)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 CompiledExpression (org.mvel2.compiler.CompiledExpression)9 Foo (org.mvel2.tests.core.res.Foo)9 ParserContext (org.mvel2.ParserContext)8 Serializable (java.io.Serializable)7 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)5 HashMap (java.util.HashMap)4 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)4 Map (java.util.Map)3 HashSet (java.util.HashSet)2 Macro (org.mvel2.Macro)2 Debugger (org.mvel2.debug.Debugger)2 Frame (org.mvel2.debug.Frame)2 Interceptor (org.mvel2.integration.Interceptor)2 Bar (org.mvel2.tests.core.res.Bar)2 FastList (org.mvel2.util.FastList)2 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 ASTNode (org.mvel2.ast.ASTNode)1