Search in sources :

Example 76 with CompiledExpression

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

the class MutationsTests method testUnQualifiedStaticTyping.

public void testUnQualifiedStaticTyping() {
    CompiledExpression ce = (CompiledExpression) compileExpression("import java.math.BigDecimal; BigDecimal a = new BigDecimal( 10.0 ); BigDecimal b = new BigDecimal( 10.0 ); BigDecimal c = a + b; return c; ");
    assertEquals(new BigDecimal(20), testCompiledSimple("import java.math.BigDecimal; BigDecimal a = new BigDecimal( 10.0 ); BigDecimal b = new BigDecimal( 10.0 ); BigDecimal c = a + b; return c; ", new HashMap()));
}
Also used : HashMap(java.util.HashMap) CompiledExpression(org.mvel2.compiler.CompiledExpression) BigDecimal(java.math.BigDecimal)

Example 77 with CompiledExpression

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

the class AbstractTest method _test.

protected static Object _test(String ex) {
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    StringAppender failErrors = new StringAppender();
    CompiledExpression compiled = compiler.compile();
    Object first = null, second = null, third = null, fourth = null, fifth = null, sixth = null, seventh = null, eighth = null;
    System.out.println(DebugTools.decompile((Serializable) compiled));
    if (!Boolean.getBoolean("mvel2.disable.jit")) {
        setDefaultOptimizer("ASM");
        try {
            first = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nFIRST TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
        try {
            second = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nSECOND TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
    }
    try {
        third = MVEL.eval(ex, new Base(), createTestMap());
    } catch (Exception e) {
        failErrors.append("\nTHIRD TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (first != null && !first.getClass().isArray()) {
        if (!first.equals(second)) {
            System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 2 (Compiled Re-Run / JIT) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
        if (!first.equals(third)) {
            if (failErrors != null)
                System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 3 (Compiled to Interpreted) [first: " + valueOf(first) + " (" + (first != null ? first.getClass().getName() : null) + "); third: " + valueOf(third) + " (" + (third != null ? third.getClass().getName() : "null") + ")]");
        }
    }
    setDefaultOptimizer("reflective");
    Serializable compiled2 = compileExpression(ex);
    try {
        fourth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFOURTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    try {
        fifth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        e.printStackTrace();
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFIFTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (fourth != null && !fourth.getClass().isArray()) {
        if (!fourth.equals(fifth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run X2) [fourth: " + valueOf(fourth) + "; fifth: " + valueOf(fifth) + "]");
        }
    }
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("unittest");
    ctx.setDebugSymbols(true);
    ExpressionCompiler debuggingCompiler = new ExpressionCompiler(ex, ctx);
    // debuggingCompiler.setDebugSymbols(true);
    CompiledExpression compiledD = debuggingCompiler.compile();
    try {
        sixth = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSIXTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (sixth != null && !sixth.getClass().isArray()) {
        if (!fifth.equals(sixth)) {
            System.out.println("Payload 1 -- No Symbols: ");
            System.out.println(decompile(compiled));
            System.out.println();
            System.out.println("Payload 2 -- With Symbols: ");
            System.out.println(decompile(compiledD));
            System.out.println();
            throw new AssertionError("Different result from test 5 and 6 (Compiled to Compiled+DebuggingSymbols) [first: " + valueOf(fifth) + "; second: " + valueOf(sixth) + "]");
        }
    }
    try {
        seventh = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSEVENTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (seventh != null && !seventh.getClass().isArray()) {
        if (!seventh.equals(sixth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    try {
        Serializable xx = serializationTest(compiledD);
        eighth = executeExpression(xx, new Base(), new MapVariableResolverFactory(createTestMap()));
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nEIGHTH TEST (Serializability): { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (eighth != null && !eighth.getClass().isArray()) {
        if (!eighth.equals(seventh)) {
            throw new AssertionError("Different result from test 7 and 8 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    if (failErrors.length() > 0) {
        System.out.println(decompile(compiledD));
        throw new AssertionError("Detailed Failure Report:\n" + failErrors.toString());
    }
    return fourth;
}
Also used : Serializable(java.io.Serializable) StringAppender(org.mvel2.util.StringAppender) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) Base(org.mvel2.tests.core.res.Base) CharArrayWriter(java.io.CharArrayWriter) PrintWriter(java.io.PrintWriter)

Example 78 with CompiledExpression

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

the class ArithmeticTests method testJIRA161.

public void testJIRA161() {
    Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped());
    assertEquals(1 == -(-1), MVEL.executeExpression(s));
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)", ctx).compile();
    assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression));
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 79 with CompiledExpression

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

the class CoreConfidenceTests method testSetAccessorOverloadedEqualsStrictMode.

public void testSetAccessorOverloadedEqualsStrictMode() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    try {
        CompiledExpression expr = new ExpressionCompiler("foo.bar = 0", ctx).compile();
    } catch (CompileException e) {
        // should fail.
        e.printStackTrace();
        return;
    }
    assertTrue(false);
}
Also used : CompileException(org.mvel2.CompileException) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 80 with CompiledExpression

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

the class CoreConfidenceTests method testArray2.

public void testArray2() {
    String ex = " TestHelper.method(1, {\"a\", \"b\"});\n" + " TestHelper.method(2, {new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, {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

CompiledExpression (org.mvel2.compiler.CompiledExpression)81 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)65 ParserContext (org.mvel2.ParserContext)48 HashMap (java.util.HashMap)20 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)20 Foo (org.mvel2.tests.core.res.Foo)20 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)15 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)14 DataType (org.mule.runtime.api.metadata.DataType)11 HashSet (java.util.HashSet)10 Test (org.junit.Test)9 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 ParserContext (org.mule.mvel2.ParserContext)8 Map (java.util.Map)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6 CompileException (org.mvel2.CompileException)6 ASTNode (org.mvel2.ast.ASTNode)6 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)6