Search in sources :

Example 26 with ParserConfiguration

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

the class CoreConfidenceTests method testMapAccessWithNestedPropertyRepeated.

public void testMapAccessWithNestedPropertyRepeated() {
    /*
     * 181 - Nested property access successful in ReflectiveAccessorOptimizer 
     *   but fails in ASMAccessorOptimizer
     */
    String str = "map[key] == \"one\"";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", POJO.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    POJO ctx = new POJO();
    ctx.getMap().put("1", "one");
    for (int i = 0; i < 500; ++i) {
        try {
            Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
            assertTrue(result);
        } catch (RuntimeException ex) {
            if (i == 0) {
                throw ex;
            }
            throw new IllegalStateException("Expression failed at iteration " + i, ex);
        }
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) MVEL.evalToBoolean(org.mvel2.MVEL.evalToBoolean) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 27 with ParserConfiguration

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

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

Example 29 with ParserConfiguration

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

the class CoreConfidenceTests method checkOperation.

private void checkOperation(String expression, int expectedResult) {
    AtomicInteger i = new AtomicInteger(10);
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("i", i);
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("i", AtomicInteger.class);
    Serializable compiledExpr = MVEL.compileExpression(expression, pctx);
    int result = (Integer) MVEL.executeExpression(compiledExpr, null, factory);
    assertEquals(expectedResult, result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Serializable(java.io.Serializable) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IndexedVariableResolverFactory(org.mvel2.integration.impl.IndexedVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 30 with ParserConfiguration

use of org.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) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3