Search in sources :

Example 46 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class CoreConfidenceTests method testSetterViaMapNotation.

public void testSetterViaMapNotation() {
    TestClass tc = new TestClass();
    tc.getExtra().put("test", "value");
    ParserContext ctx = new ParserContext();
    ctx.withInput("this", TestClass.class);
    ctx.setStrongTyping(true);
    String expression = "extra[\"test\"]";
    Serializable compiled = MVEL.compileSetExpression(expression, tc.getClass(), ctx);
    MVEL.executeSetExpression(compiled, tc, "value3");
    assertEquals("value3", tc.getExtra().get("test"));
}
Also used : Serializable(java.io.Serializable) TestClass(org.mvel2.tests.core.res.TestClass) ParserContext(org.mvel2.ParserContext)

Example 47 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class CoreConfidenceTests method testPrimitiveNumberCoercionDuringDivisionShouldWorkOnBothSide.

public void testPrimitiveNumberCoercionDuringDivisionShouldWorkOnBothSide() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrictTypeEnforcement(true);
    parserContext.setStrongTyping(true);
    parserContext.addInput("a", int.class);
    parserContext.addInput("b", int.class);
    int a = 1;
    int b = 2;
    Object res = a / b;
    System.out.printf("Result class Java: %s\nResult value: %s\n", res.getClass(), res);
    Object resBoolean = a / b < 0.99;
    System.out.println("Result Boolean: " + resBoolean);
    Serializable constantDoubleLeft = MVEL.compileExpression("0.99 >= a / b", parserContext);
    Object resultLeft = MVEL.executeExpression(constantDoubleLeft, new HashMap() {

        {
            put("a", 1);
            put("b", 2);
        }
    });
    assertEquals(true, resultLeft);
    Serializable constantDoubleRight = MVEL.compileExpression("a / b < 0.99", parserContext);
    Object resultRight = MVEL.executeExpression(constantDoubleRight, new HashMap() {

        {
            put("a", 1);
            put("b", 2);
        }
    });
    assertEquals(true, resultRight);
    parserContext.addInput("c", double.class);
    parserContext.addInput("d", double.class);
    Serializable constantIntRight = MVEL.compileExpression("c / d > 0", parserContext);
    Object resultRightInt = MVEL.executeExpression(constantIntRight, new HashMap() {

        {
            put("c", 1);
            put("d", 2);
        }
    });
    assertEquals(true, resultRightInt);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext)

Example 48 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class CoreConfidenceTests method testListNestedInsideList.

public void testListNestedInsideList() {
    ParserContext ctx = new ParserContext();
    ctx.addImport("User", User.class);
    ExpressionCompiler compiler = new ExpressionCompiler("users = [ new User('Darth', 'Vadar'), " + "new User('Bobba', 'Feta') ]; [ users.get( 0 ), users.get( 1 ) ]", ctx);
    List list = (List) executeExpression(compiler.compile(), new HashMap());
    User user = (User) list.get(0);
    assertEquals("Darth", user.getFirstName());
    user = (User) list.get(1);
    assertEquals("Bobba", user.getFirstName());
    compiler = new ExpressionCompiler("users = [ new User('Darth', 'Vadar'), " + "new User('Bobba', 'Feta') ]; [ users[0], users[1] ]", ctx);
    list = (List) executeExpression(compiler.compile(), new HashMap());
    user = (User) list.get(0);
    assertEquals("Darth", user.getFirstName());
    user = (User) list.get(1);
    assertEquals("Bobba", user.getFirstName());
}
Also used : User(org.mvel2.tests.core.res.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) List(java.util.List) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext)

Example 49 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class CoreConfidenceTests method testStringConcatenation3.

public void testStringConcatenation3() {
    // BUG: return type of the string concatenation is inferred as double instead of String
    String ex = "services.log($av + \"Drop +5%: \"+$sb+\" avg: $\"+percent($av)+\" price: $\"+$pr );";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    ctx.addInput("$sb", String.class);
    ctx.addInput("$av", double.class);
    ctx.addInput("$pr", double.class);
    ctx.addInput("services", Services.class);
    ctx.addImport("percent", MVEL.getStaticMethod(String.class, "valueOf", new Class[] { double.class }));
    try {
        Serializable compiledExpression = MVEL.compileExpression(ex, ctx);
        Services services = new Services() {

            public void log(String text) {
            }
        };
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("services", services);
        vars.put("$sb", "RHT");
        vars.put("$av", 15.0);
        vars.put("$pr", 10.0);
        MVEL.executeExpression(compiledExpression, vars);
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Should not raise exception: " + e.getMessage());
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestClass(org.mvel2.tests.core.res.TestClass) PublicClass(org.mvel2.tests.core.res.res2.PublicClass) MyClass(org.mvel2.tests.core.res.MyClass) OverloadedClass(org.mvel2.tests.core.res.res2.OverloadedClass) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext)

Example 50 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class CoreConfidenceTests method testStaticNestedWithMethodCall.

public void testStaticNestedWithMethodCall() {
    String expr = "item = new Item( \"Some Item\"); $msg.addItem( item ); return $msg";
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(false);
    context.addImport("Message", Message.class);
    context.addImport("Item", Item.class);
    // Serializable compiledExpression = compiler.compileShared(context);
    Map vars = new HashMap();
    vars.put("$msg", new Message());
    ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
    Message msg = (Message) executeExpression(compiler.compile(), vars);
    Item item = (Item) msg.getItems().get(0);
    assertEquals("Some Item", item.getName());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10