Search in sources :

Example 56 with ExpressionCompiler

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

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);
    // debuggingCompiler.setDebugSymbols(true);
    CompiledExpression compiledD = debuggingCompiler.compile(ctx);
    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);
        AbstractParser.resetParserContext();
        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 : 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)

Example 57 with ExpressionCompiler

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

the class CoreConfidenceTests method testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression.

/**
 * Provided by: Phillipe Ombredanne
 */
public void testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression() {
    String expr = // 
    "		System.out.println( message );\n" + // 
    "m.setMessage( \"Goodbye cruel world\" );\n" + // 
    "System.out.println(m.getStatus());\n" + "m.setStatus( Message.GOODBYE );\n";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(false);
    context.addImport("Message", Message.class);
    context.addInput("System", void.class);
    context.addInput("message", Object.class);
    context.addInput("m", Object.class);
    compiler.compile(context);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 58 with ExpressionCompiler

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

the class CoreConfidenceTests method testPropertyVerfierShoudldNotLoopIndefinately.

/**
 * Provided by: Aadi Deshpande
 */
public void testPropertyVerfierShoudldNotLoopIndefinately() {
    String expr = "\t\tmodel.latestHeadlines = $list;\n" + "model.latestHeadlines.add( 0, (model.latestHeadlines[2]) );";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    compiler.setVerifying(true);
    ParserContext pCtx = new ParserContext();
    pCtx.addInput("$list", List.class);
    pCtx.addInput("model", Model.class);
    compiler.compile(pCtx);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 59 with ExpressionCompiler

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

the class CoreConfidenceTests method testMapNestedInsideList.

public void testMapNestedInsideList() {
    ParserContext ctx = new ParserContext();
    ctx.addImport("User", User.class);
    ExpressionCompiler compiler = new ExpressionCompiler("users = [ 'darth'  : new User('Darth', 'Vadar')," + "\n'bobba' : new User('Bobba', 'Feta') ]; [ users.get('darth'), users.get('bobba') ]");
    // Serializable s = compiler.compileShared(ctx);
    List list = (List) executeExpression(compiler.compile(ctx), new HashMap());
    User user = (User) list.get(0);
    assertEquals("Darth", user.getFirstName());
    user = (User) list.get(1);
    assertEquals("Bobba", user.getFirstName());
    compiler = new ExpressionCompiler("users = [ 'darth'  : new User('Darth', 'Vadar')," + "\n'bobba' : new User('Bobba', 'Feta') ]; [ users['darth'], users['bobba'] ]");
    list = (List) executeExpression(compiler.compile(ctx), new HashMap());
    user = (User) list.get(0);
    assertEquals("Darth", user.getFirstName());
    user = (User) list.get(1);
    assertEquals("Bobba", user.getFirstName());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) List(java.util.List)

Example 60 with ExpressionCompiler

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

the class CoreConfidenceTests method testStrictTypingCompilation.

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

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)110 CompiledExpression (org.mvel2.compiler.CompiledExpression)33 ParserContext (org.mvel2.ParserContext)31 Serializable (java.io.Serializable)15 Foo (org.mvel2.tests.core.res.Foo)12 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)11 HashMap (java.util.HashMap)9 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)9 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashSet (java.util.HashSet)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)5 List (java.util.List)4 Interceptor (org.mvel2.integration.Interceptor)4 Map (java.util.Map)3 ASTNode (org.mvel2.ast.ASTNode)3 WithNode (org.mvel2.ast.WithNode)3 Type (java.lang.reflect.Type)2 LinkedHashMap (java.util.LinkedHashMap)2 Macro (org.mvel2.Macro)2