Search in sources :

Example 1 with Bar

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

the class PropertyHandlerTests method testNullPropertyHandler2.

public void testNullPropertyHandler2() {
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    OptimizerFactory.setDefaultOptimizer("reflective");
    PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            return "NULL";
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return "NULL";
        }
    });
    Foo foo = new Foo();
    Bar bar = foo.getBar();
    foo.setBar(null);
    Map map = new HashMap();
    map.put("foo", foo);
    Serializable s = MVEL.compileExpression("foo.bar");
    assertEquals("NULL", MVEL.executeExpression(s, map));
    assertEquals("NULL", MVEL.executeExpression(s, map));
    foo.setBar(bar);
    assertEquals(bar, MVEL.executeExpression(s, map));
}
Also used : Bar(org.mvel2.tests.core.res.Bar) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Bar

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

the class PropertyHandlerTests method testNullPropertyHandler.

public void testNullPropertyHandler() {
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    OptimizerFactory.setDefaultOptimizer("ASM");
    PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            return "NULL";
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return "NULL";
        }
    });
    Foo foo = new Foo();
    Bar bar = foo.getBar();
    foo.setBar(null);
    Map map = new HashMap();
    map.put("foo", foo);
    Serializable s = MVEL.compileExpression("foo.bar");
    assertEquals("NULL", MVEL.executeExpression(s, map));
    assertEquals("NULL", MVEL.executeExpression(s, map));
    foo.setBar(bar);
    assertEquals(bar, MVEL.executeExpression(s, map));
}
Also used : Bar(org.mvel2.tests.core.res.Bar) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Bar

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

the class WithTests method testNewUsingWith.

public void testNewUsingWith() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addImport(Bar.class);
    Serializable s = compileExpression("[ 'foo' : (with ( new Foo() )" + " { bar = with ( new Bar() ) { name = 'ziggy' } }) ]", ctx);
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("ziggy", (((Foo) ((Map) executeExpression(s)).get("foo")).getBar().getName()));
}
Also used : Serializable(java.io.Serializable) ParserContext(org.mvel2.ParserContext)

Example 4 with Bar

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

the class WithTests method testInlineWith3.

public void testInlineWith3() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}, toUC('doopy')}").compile();
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
    assertEquals("foobie", f.getBar().getName());
    assertEquals("doopy", f.register);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 5 with Bar

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

the class WithTests method testInlineWith5.

public void testInlineWith5() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    ParserContext pCtx = new ParserContext();
    pCtx.setStrongTyping(true);
    pCtx.addInput("foo", Foo.class);
    CompiledExpression expr = new ExpressionCompiler("foo.{name='poopy', aValue='bar'}").compile(pCtx);
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

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