Package org.mvel2.util

Examples of org.mvel2.util.StringAppender


import java.util.Map;

public class UtilsTests extends TestCase {

  public void testMain() {
    assertEquals("foobarfoobar", new StringAppender().append("foo").append('b').append('a').append('r').append("foobar").toString());
  }
View Full Code Here


    assertEquals("foobarfoobar", new StringAppender().append("foo").append('b').append('a').append('r').append("foobar").toString());
  }

  public void testMain2() {
    assertEquals("foo bar test 1 2 3foo bar test 1 2 3",
        new StringAppender(0).append("foo bar ").append("test").append(" 1 2 3")
            .append("foo bar").append(" ").append("test").append(" 1 2 3").toString());
  }
View Full Code Here

            .append("foo bar").append(" ").append("test").append(" 1 2 3").toString());
  }

  public void testMain3() {
    assertEquals("C:/projects/webcat/exploded/resources/productimages/",
        new StringAppender(10).append("C:/projects/webcat/exploded/")
            .append("resources/productimages/").toString());
  }
View Full Code Here

    }
    else if (args.length == 1) {
      throw new CommandException("incorrect number of parameters");
    }
    else {
      StringAppender sbuf = new StringAppender();
      for (int i = 1; i < args.length; i++) {
        sbuf.append(args[i]);
        if (i < args.length) sbuf.append(" ");
      }

      env.put(args[0], sbuf.toString().trim());
    }

    return null;
  }
View Full Code Here

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

    while ((cls = cls.getSuperclass()) != null) {
      list.add(cls.getName());
    }

    StringAppender output = new StringAppender();

    for (int i = list.size() - 1; i != -1; i--) {
      output.append(list.get(i));
      if ((i - 1) != -1) output.append(" -> ");
    }

    return output.toString();
  }
View Full Code Here

  private static void renderMethods(Class cls) {
    Method[] methods = cls.getMethods();

    Method m;
    StringAppender appender = new StringAppender();
    int mf;
    for (int i = 0; i < methods.length; i++) {
      appender.append(TextUtil.paint(' ', PADDING + 2));
      if (((mf = (m = methods[i]).getModifiers()) & Modifier.PUBLIC) != 0) appender.append("public");
      else if ((mf & Modifier.PRIVATE) != 0) appender.append("private");
      else if ((mf & Modifier.PROTECTED) != 0) appender.append("protected");

      appender.append(' ').append(m.getReturnType().getName()).append(' ').append(m.getName()).append("(");
      Class[] parmTypes = m.getParameterTypes();
      for (int y = 0; y < parmTypes.length; y++) {
        if (parmTypes[y].isArray()) {
          appender.append(parmTypes[y].getComponentType().getName() + "[]");
        }
        else {
          appender.append(parmTypes[y].getName());
        }
        if ((y + 1) < parmTypes.length) appender.append(", ");
      }
      appender.append(")");

      if (m.getDeclaringClass() != cls) {
        appender.append("    [inherited from: ").append(m.getDeclaringClass().getName()).append("]");
      }


      if ((i + 1) < methods.length) appender.append('\n');
    }

    System.out.println(appender.toString());
  }
View Full Code Here

    private Marshaller marshaller;
    private StringAppender appender = new StringAppender();

    public MarshallerContext(Marshaller marshaller) {
      this.marshaller = marshaller;
      this.appender = new StringAppender();
    }
View Full Code Here

      '.', '?', '/', '`', ' ', '\t', '\n', '\r'
  };

  public static void main(String[] args) throws IOException {
    DecimalFormat df = new DecimalFormat("###,###.##");
    StringAppender append = new StringAppender();
    int len;
    long start = currentTimeMillis();
    long time;
    double rate;

    int seed;

    boolean flip = false;

    Random rand = new Random(System.currentTimeMillis());
    Random rand1 = new Random(System.currentTimeMillis() + 1);
    Random rand2 = new Random(rand1.nextInt());
    Random rand3 = new Random(rand.nextInt(SALTS.length - 1));
    Random rand4 = new Random(rand3.nextInt());

    for (int run = 0; run < MAX; run++) {
      len = (int) (random() * 500) + 10;
      append.reset();

      for (int i = 0; i < len; i++) {
        append.append(CHAR_TABLE[((SALTS[((rand.nextInt(1000)) + 1) % SALTS.length]) * ((flip = !flip) ? rand1.nextInt(1000) : rand2.nextInt(1000)) + 1) % CHAR_TABLE.length]);
        SALTS[rand3.nextInt(SALTS.length - 1)] ^= rand4.nextInt(1000) + 1;
      }


      try {
        MVEL.eval(append.toString());
      }
      catch (UnresolveablePropertyException e) {
        //ignore
      }
      catch (CompileException e) {
        //ignore
      }
      catch (ArithmeticException e) {
        //ignore
      }
      catch (ScriptRuntimeException e) {
        //ignore
      }
      catch (Exception e) {
        System.out.println("untrapped error!\n---\n" + append.toString() + "\n---\n");
        System.out.flush();
        e.printStackTrace();
        System.err.flush();
      }

View Full Code Here

        return ((PrototypalFunctionInstance) prop).call(ctx, thisReference, new InvokationContextFactory(variableFactory, funcCtx), args);
      }
    }

    if (m == null) {
      StringAppender errorBuild = new StringAppender();
      for (int i = 0; i < args.length; i++) {
        errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
        if (i < args.length - 1) errorBuild.append(", ");
      }

      if ("size".equals(name) && args.length == 0 && cls.isArray()) {
        return getLength(ctx);
      }

//      System.out.println("{ " + new String(property) + " }");

      throw new PropertyAccessException("unable to resolve method: "
          + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]"
          , property, st);
    }
    else {
      for (int i = 0; i < args.length; i++) {
        args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
View Full Code Here

TOP

Related Classes of org.mvel2.util.StringAppender

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.