Package org.mvel2.compiler

Examples of org.mvel2.compiler.ExpressionCompiler


  protected static Object testCompiledSimple(String ex, Object base, Map map) {
    return MVEL.executeExpression(MVEL.compileExpression(ex), base, map);
  }

  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


  }

  public void testDeepAssignment2() {
    Map map = createTestMap();

    ExpressionCompiler compiler = new ExpressionCompiler("foo.bar.age = 21");
    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

    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

    assertEquals("doopy", f.register);
  }

  public void testInlineWith4() {
    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

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

  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

    vars.put("foo",
        new Foo());
    vars.put("$value",
        new Long(5));

    ExpressionCompiler compiler = new ExpressionCompiler("with (foo) { countTest = $value };");

    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);

    CompiledExpression compiled = compiler.compile(ctx);

    executeExpression(compiled, null, vars);
    executeExpression(compiled, null, vars);
  }
View Full Code Here

TOP

Related Classes of org.mvel2.compiler.ExpressionCompiler

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.