Package org.mvel2.compiler

Examples of org.mvel2.compiler.CompiledExpression


  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) {
View Full Code Here


    ParserContext ctx = new ParserContext();

    ctx.addInput("foo", Foo.class);
    ctx.setStrongTyping(true);

    CompiledExpression ce = compiler.compile(ctx);

    executeExpression(ce, map);

    assertEquals(((Foo) map.get("foo")).getBar().getAge(), 21);
  }
View Full Code Here

    Object val = test("java.math.BigDecimal a = new java.math.BigDecimal( 10.0 ); java.math.BigDecimal b = new java.math.BigDecimal( 10.0 ); java.math.BigDecimal c = a + b; return c; ");
    assertEquals(new BigDecimal(20), val);
  }

  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()));
  }
View Full Code Here

    assertEquals("Hello", foo.aValue);
    assertEquals("Goodbye", foo.bValue);
  }

  public void testInlineWith() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name='poopy', aValue='bar'}").compile();
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
  }
View Full Code Here

    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
  }

  public void testInlineWith2() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}}").compile();

    Foo f = (Foo) executeExpression(expr, createTestMap());

    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
View Full Code Here

    assertEquals("bar", f.aValue);
    assertEquals("foobie", f.getBar().getName());
  }

  public void testInlineWith3() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}, toUC('doopy')}").compile();

    Foo f = (Foo) executeExpression(expr, createTestMap());

    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
View Full Code Here

    assertEquals("foobie", f.getBar().getName());
    assertEquals("doopy", f.register);
  }

  public void testInlineWith3a() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name='poopy',aValue='bar',bar.{name='foobie'},toUC('doopy')}").compile();

    Foo f = (Foo) executeExpression(expr, createTestMap());

    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
View Full Code Here

    OptimizerFactory.setDefaultOptimizer("ASM");
    ExpressionCompiler expr = new ExpressionCompiler("new Foo().{ name = 'bar' }");
    ParserContext pCtx = new ParserContext();
    pCtx.addImport(Foo.class);

    CompiledExpression c = expr.compile(pCtx);

    Foo f = (Foo) executeExpression(c);

    assertEquals("bar", f.getName());
View Full Code Here

    ParserContext pCtx = new ParserContext();
    pCtx.setStrongTyping(true);

    pCtx.addInput("foo", Foo.class);

    CompiledExpression expr = new ExpressionCompiler("foo.{name='poopy', aValue='bar'}").compile(pCtx);
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
  }
View Full Code Here

  }

  public void testInlineWithImpliedThis() {
    Base b = new Base();
    ExpressionCompiler expr = new ExpressionCompiler(".{ data = 'foo' }");
    CompiledExpression compiled = expr.compile();

    executeExpression(compiled, b);

    assertEquals(b.data, "foo");
  }
View Full Code Here

TOP

Related Classes of org.mvel2.compiler.CompiledExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.