Search in sources :

Example 56 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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 57 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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 58 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler 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)

Example 59 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler in project mvel by mvel.

the class CoreConfidenceTests method testComaProblemStrikesBack.

public void testComaProblemStrikesBack() {
    String ex = "a.explanation = \"There is a coma, in here\"";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
    Serializable s = compiler.compile();
    Base a = new Base();
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", a);
    executeExpression(s, variables);
    assertEquals("There is a coma, in here", a.data);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) RuleBase(org.mvel2.tests.core.res.RuleBase) Base(org.mvel2.tests.core.res.Base)

Example 60 with ExpressionCompiler

use of org.mule.mvel2.compiler.ExpressionCompiler in project mvel by mvel.

the class CoreConfidenceTests method testArray.

public void testArray() {
    String ex = " TestHelper.method(1, new String[]{\"a\", \"b\"});\n" + " TestHelper.method(2, new String[]{new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, new Fooz[]{new Fooz(\"a\"), new Fooz(\"b\")});";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(TestHelper.class);
    ctx.addImport(Fooz.class);
    ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
    OptimizerFactory.setDefaultOptimizer("ASM");
    CompiledExpression expr = compiler.compile();
    executeExpression(expr);
    OptimizerFactory.setDefaultOptimizer("reflective");
    expr = compiler.compile();
    executeExpression(expr);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)219 ParserContext (org.mvel2.ParserContext)119 CompiledExpression (org.mvel2.compiler.CompiledExpression)65 HashMap (java.util.HashMap)39 Serializable (java.io.Serializable)30 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)22 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)19 Map (java.util.Map)18 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 LinkedHashMap (java.util.LinkedHashMap)15 HashSet (java.util.HashSet)10 List (java.util.List)10 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ArrayList (java.util.ArrayList)8 CompileException (org.mvel2.CompileException)8 Interceptor (org.mvel2.integration.Interceptor)8 Macro (org.mvel2.Macro)6 ASTNode (org.mvel2.ast.ASTNode)6