use of org.mvel2.util.StringAppender 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;
}
use of org.mvel2.util.StringAppender in project mvel by mikebrock.
the class Fuzzer method main.
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();
}
if (run % 25000 == 0 && run != 0) {
rate = run / (time = (currentTimeMillis() - start) / 1000);
System.out.println("Run: " + df.format(run) + " times; " + df.format(time) + "secs; " + df.format(rate) + " avg. per second.");
}
}
}
use of org.mvel2.util.StringAppender in project mvel by mvel.
the class MacroProcessor method parse.
public char[] parse(char[] input) {
setExpression(input);
StringAppender appender = new StringAppender();
int start;
boolean macroArmed = true;
String token;
for (; cursor < length; cursor++) {
start = cursor;
while (cursor < length && isIdentifierPart(expr[cursor])) cursor++;
if (cursor > start) {
if (macros.containsKey(token = new String(expr, start, cursor - start)) && macroArmed) {
appender.append(macros.get(token).doMacro());
} else {
appender.append(token);
}
}
if (cursor < length) {
switch(expr[cursor]) {
case '\\':
cursor++;
break;
case '/':
start = cursor;
if (cursor + 1 != length) {
switch(expr[cursor + 1]) {
case '/':
while (cursor != length && expr[cursor] != '\n') cursor++;
break;
case '*':
int len = length - 1;
while (cursor != len && !(expr[cursor] == '*' && expr[cursor + 1] == '/')) cursor++;
cursor += 2;
break;
}
}
if (cursor < length)
cursor++;
appender.append(new String(expr, start, cursor - start));
if (cursor < length)
cursor--;
break;
case '"':
case '\'':
appender.append(new String(expr, (start = cursor), (cursor = captureStringLiteral(expr[cursor], expr, cursor, length)) - start));
if (cursor >= length)
break;
else if (isIdentifierPart(expr[cursor]))
cursor--;
default:
switch(expr[cursor]) {
case '.':
macroArmed = false;
break;
case ';':
case '{':
case '(':
macroArmed = true;
break;
}
appender.append(expr[cursor]);
}
}
}
return appender.toChars();
}
use of org.mvel2.util.StringAppender in project mvel by mvel.
the class CompileException method generateErrorMessage.
private String generateErrorMessage() {
StringAppender appender = new StringAppender().append("[Error: " + super.getMessage() + "]\n");
int offset = appender.length();
appender.append("[Near : {... ");
offset = appender.length() - offset;
appender.append(showCodeNearError(expr, cursor)).append(" ....}]\n").append(repeatChar(' ', offset));
if (msgOffset < 0)
msgOffset = 0;
appender.append(repeatChar(' ', msgOffset)).append('^');
calcRowAndColumn();
if (evaluationContext != null) {
appender.append("\n").append("In ").append(evaluationContext);
} else if (lineNumber != -1) {
appender.append("\n").append("[Line: " + lineNumber + ", Column: " + (column) + "]");
}
return appender.toString();
}
use of org.mvel2.util.StringAppender in project mvel by mvel.
the class ExpressionCompiler method compile.
public CompiledExpression compile() {
try {
this.debugSymbols = pCtx.isDebugSymbols();
return _compile();
} finally {
if (pCtx.isFatalError()) {
StringAppender err = new StringAppender();
Iterator<ErrorDetail> iter = pCtx.getErrorList().iterator();
ErrorDetail e;
while (iter.hasNext()) {
e = iter.next();
e = ErrorUtil.rewriteIfNeeded(e, expr, cursor);
if (e.getExpr() != expr) {
iter.remove();
} else {
err.append("\n - ").append("(").append(e.getLineNumber()).append(",").append(e.getColumn()).append(")").append(" ").append(e.getMessage());
}
}
// noinspection ThrowFromFinallyBlock
throw new CompileException("Failed to compileShared: " + pCtx.getErrorList().size() + " compilation error(s): " + err.toString(), pCtx.getErrorList(), expr, cursor, pCtx);
}
}
}
Aggregations