Search in sources :

Example 26 with ParserConfiguration

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

the class ControlFlowTests method testForLoopWithVar.

public void testForLoopWithVar() {
    String str = "int height = 100; int j = 0; for (i = 0; i < height; i++) {j++ }; return j;";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map vars = new HashMap();
    assertEquals(new Integer(100), MVEL.executeExpression(stmt, vars));
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 27 with ParserConfiguration

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

the class CoreConfidenceTests method testStrictModeAddAll.

public void testStrictModeAddAll() {
    String str = "list.addAll( o );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("o", Object.class);
    pctx.addInput("list", ArrayList.class);
    try {
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
        fail("This should not compileShared, as o is not of a type Collection");
    } catch (Exception e) {
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) IOException(java.io.IOException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 28 with ParserConfiguration

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

the class CoreConfidenceTests method testMultiplyIntByDouble.

public void testMultiplyIntByDouble() {
    ParserConfiguration conf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("i", Integer.class);
    pctx.addInput("d", Double.class);
    Map vars = new HashMap() {

        {
            put("i", 10);
            put("d", 0.3);
        }
    };
    assertEquals(3.0, MVEL.executeExpression(MVEL.compileExpression("i*d", pctx), vars));
    assertEquals(3.0, MVEL.executeExpression(MVEL.compileExpression("i*0.3", pctx), vars));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 29 with ParserConfiguration

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

the class CoreConfidenceTests method testCharToStringCoercionForComparison.

public void testCharToStringCoercionForComparison() {
    ParserConfiguration conf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("ch", Character.class);
    Map vars = new HashMap() {

        {
            put("ch", 'a');
        }
    };
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("ch == \"a\"", pctx), vars));
    assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("ch == \"b\"", pctx), vars));
// assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("\"a\" == ch", pctx), vars));
// assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("\"b\" == ch", pctx), vars));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 30 with ParserConfiguration

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

the class CoreConfidenceTests method testMethodScoring.

public void testMethodScoring() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    ParserConfiguration pconf = new ParserConfiguration();
    for (Method m : StaticMethods.class.getMethods()) {
        if (Modifier.isStatic(m.getModifiers())) {
            pconf.addImport(m.getName(), m);
        }
    }
    pconf.addImport("TestCase", TestCase.class);
    ParserContext pctx = new ParserContext(pconf);
    Map<String, Object> vars = new HashMap<String, Object>();
    // this is successful
    TestCase.assertTrue(StaticMethods.is(StaticMethods.getList(java.util.Formatter.class)));
    // this also should be fine
    Serializable expr = MVEL.compileExpression("TestCase.assertTrue( is( getList( java.util.Formatter ) ) )", pctx);
    executeExpression(expr, vars);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapObject(org.mvel2.tests.core.res.MapObject) Method(java.lang.reflect.Method) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)74 ParserContext (org.mvel2.ParserContext)70 HashMap (java.util.HashMap)39 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)32 LinkedHashMap (java.util.LinkedHashMap)29 Map (java.util.Map)23 Serializable (java.io.Serializable)10 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)7 CompileException (org.mvel2.CompileException)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MapObject (org.mvel2.tests.core.res.MapObject)5 Method (java.lang.reflect.Method)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 CachedMapVariableResolverFactory (org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory)3 MVELExpressionExecutor (org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor)3 Bar (org.mvel2.tests.core.res.Bar)3