Search in sources :

Example 26 with ParserContext

use of org.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 27 with ParserContext

use of org.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 28 with ParserContext

use of org.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 29 with ParserContext

use of org.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)

Example 30 with ParserContext

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

the class CoreConfidenceTests method testNoArgMethodInheritance.

public void testNoArgMethodInheritance() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrictTypeEnforcement(true);
    parserContext.setStrongTyping(true);
    parserContext.addInput("a", Parent.class);
    parserContext.addInput("b", Child.class);
    assertEquals(Object.class, MVEL.analyze("a.getSomething()", parserContext));
    assertEquals(String.class, MVEL.analyze("b.getSomething()", parserContext));
}
Also used : ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)340 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)189 HashMap (java.util.HashMap)132 Serializable (java.io.Serializable)92 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)68 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 LinkedHashMap (java.util.LinkedHashMap)62 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)60 CompileException (org.mvel2.CompileException)51 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)28 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 ArrayList (java.util.ArrayList)22 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)21 MapObject (org.mvel2.tests.core.res.MapObject)19 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)14 IOException (java.io.IOException)13