Search in sources :

Example 31 with ParserContext

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

the class CoreConfidenceTests method testJIRA168.

public void testJIRA168() {
    boolean before = MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL;
    try {
        Map<String, Object> st = new HashMap<String, Object>();
        st.put("__fact__", new ArrayList());
        st.put("__expected__", 0);
        String expressionNaked = "__fact__.size == __expected__";
        String expressionNonNaked = "__fact__.size() == __expected__";
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        // the following works fine
        ParserContext ctx = new ParserContext();
        for (Map.Entry<String, Object> entry : st.entrySet()) {
            ctx.addInput(entry.getKey(), entry.getValue().getClass());
        }
        CompiledExpression expr = new ExpressionCompiler(expressionNaked, ctx).compile();
        Boolean result = (Boolean) executeExpression(expr, st);
        assertTrue(result);
        // the following works fine
        result = (Boolean) MVEL.eval(expressionNonNaked, st);
        assertTrue(result);
        // the following fails
        result = (Boolean) MVEL.eval(expressionNaked, st);
        assertTrue(result);
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = before;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) MapObject(org.mvel2.tests.core.res.MapObject) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 32 with ParserContext

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

the class CoreConfidenceTests method testStrictTypingCompilation.

public void testStrictTypingCompilation() {
    // OptimizerFactory.setDefaultOptimizer(OptimizerFactory.DYNAMIC);
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler("a.foo;\nb.foo;\n x = 5", ctx);
    try {
        compiler.compile();
    } catch (CompileException e) {
        e.printStackTrace();
        return;
    }
    assertTrue(false);
// OptimizerFactory.setDefaultOptimizer(OptimizerFactory.DYNAMIC);
}
Also used : CompileException(org.mvel2.CompileException) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 33 with ParserContext

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

the class CoreConfidenceTests method testNestedClassWithNestedGenericsOnNakedMethod.

public void testNestedClassWithNestedGenericsOnNakedMethod() {
    String str = "deliveries.size";
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Triangle.class);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertEquals(Integer.valueOf(0), (Integer) MVEL.executeExpression(stmt, new Triangle(), new HashMap()));
    str = "deliveries.size == 0";
    stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertTrue((Boolean) MVEL.executeExpression(stmt, new Triangle(), new HashMap()));
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 34 with ParserContext

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

the class CoreConfidenceTests method testInstanceofWithPackageImport.

public void testInstanceofWithPackageImport() {
    ParserConfiguration conf = new ParserConfiguration();
    conf.addPackageImport("org.mvel2.tests.core");
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("value", Object.class);
    Map vars = new HashMap() {

        {
            put("value", new CoreConfidenceTests());
        }
    };
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof CoreConfidenceTests", pctx), vars));
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof " + CoreConfidenceTests.class.getCanonicalName(), 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 35 with ParserContext

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

the class CoreConfidenceTests method testDynamicImportsWithNullConstructorParamWithStrongType.

public void testDynamicImportsWithNullConstructorParamWithStrongType() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", null)", ctx);
    Cheesery p1 = new Cheesery("bobbo", null);
    Cheesery p2 = (Cheesery) executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory());
    assertEquals(p1, p2);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) Cheesery(org.mvel2.tests.core.res.Cheesery) 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