use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class DebuggerTests method testBreakpoints4.
public void testBreakpoints4() {
String expression = "System.out.println('foo');\n" + "a = new Foo244();\n" + "update (a) { name = 'bar' };\n" + "System.out.println('name:' + a.name);\n" + "return a.name;";
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
Map<String, Macro> macros = new HashMap<String, Macro>();
class TestResult {
boolean firedBefore;
boolean firedAfter;
}
final TestResult result = new TestResult();
interceptors.put("Update", new Interceptor() {
public int doBefore(ASTNode node, VariableResolverFactory factory) {
((WithNode) node).getNestedStatement().getValue(null, factory);
System.out.println("fired update interceptor -- before");
result.firedBefore = true;
return 0;
}
public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
System.out.println("fired update interceptor -- after");
result.firedAfter = true;
return 0;
}
});
macros.put("update", new Macro() {
public String doMacro() {
return "@Update with";
}
});
expression = parseMacros(expression, macros);
ExpressionCompiler compiler = new ExpressionCompiler(expression);
ParserContext ctx = new ParserContext();
ctx.setDebugSymbols(true);
ctx.setSourceFile("test2.mv");
ctx.addImport("Foo244", Foo.class);
ctx.setInterceptors(interceptors);
CompiledExpression compiled = compiler.compile(ctx);
System.out.println("\nExpression:------------");
System.out.println(expression);
System.out.println("------------");
MVELRuntime.registerBreakpoint("test2.mv", 3);
MVELRuntime.registerBreakpoint("test2.mv", 4);
MVELRuntime.registerBreakpoint("test2.mv", 5);
final Set<Integer> breaked = new HashSet<Integer>();
Debugger testDebugger = new Debugger() {
public int onBreak(Frame frame) {
System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
breaked.add(frame.getLineNumber());
return 0;
}
};
MVELRuntime.setThreadDebugger(testDebugger);
assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
assertTrue("did not fire before", result.firedBefore);
assertTrue("did not fire after", result.firedAfter);
assertEquals("did not break at expected points", Make.Set.<Integer>$()._(3)._(4)._(5)._(), breaked);
}
use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class CoreConfidenceTests method testTestIntToLong.
public void testTestIntToLong() {
String s = "1+(long)a";
ParserContext pc = new ParserContext();
pc.addInput("a", Integer.class);
ExpressionCompiler compiler = new ExpressionCompiler(s, pc);
CompiledExpression expr = compiler.compile();
Map vars = new HashMap();
vars.put("a", 1);
Object r = ((ExecutableStatement) expr).getValue(null, new MapVariableResolverFactory(vars));
assertEquals(new Long(2), r);
}
use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class ArithmeticTests method testJIRA161.
public void testJIRA161() {
Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped());
assertEquals(1 == -(-1), MVEL.executeExpression(s));
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)").compile(ctx);
assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression));
}
use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class ArithmeticTests method testStrongTypingModeComparison.
public void testStrongTypingModeComparison() {
ParserContext parserContext = new ParserContext();
parserContext.setStrongTyping(true);
parserContext.addInput("a", Long.class);
CompiledExpression compiledExpression = new ExpressionCompiler("a==0").compile(parserContext);
HashMap<String, Object> variables = new HashMap<String, Object>();
variables.put("a", 0l);
MVEL.executeExpression(compiledExpression, variables);
}
use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class DebugTools method decompile.
private static String decompile(CompiledExpression cExp, boolean nest, DecompileContext context) {
ASTIterator iter = new ASTLinkedList(cExp.getFirstNode());
ASTNode tk;
StringBuffer sbuf = new StringBuffer();
if (!nest) {
sbuf.append("Expression Decompile\n-------------\n");
}
while (iter.hasMoreNodes()) {
sbuf.append("(").append(context.node++).append(") ");
if ((tk = iter.nextNode()) instanceof NestedStatement && ((NestedStatement) tk).getNestedStatement() instanceof CompiledExpression) {
// noinspection StringConcatenationInsideStringBufferAppend
sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
sbuf.append(decompile((CompiledExpression) ((NestedStatement) tk).getNestedStatement(), true, context));
}
if (tk instanceof Substatement && ((Substatement) tk).getStatement() instanceof CompiledExpression) {
// noinspection StringConcatenationInsideStringBufferAppend
sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
sbuf.append(decompile((CompiledExpression) ((Substatement) tk).getStatement(), true, context));
} else // }
if (tk.isDebuggingSymbol()) {
// noinspection StringConcatenationInsideStringBufferAppend
sbuf.append("DEBUG_SYMBOL :: " + tk.toString());
} else if (tk.isLiteral()) {
sbuf.append("LITERAL :: ").append(tk.getLiteralValue()).append("'");
} else if (tk.isOperator()) {
sbuf.append("OPERATOR [").append(getOperatorName(tk.getOperator())).append("]: ").append(tk.getName());
if (tk.isOperator(Operator.END_OF_STMT))
sbuf.append("\n");
} else if (tk.isIdentifier()) {
sbuf.append("REFERENCE :: ").append(tk.getClass().getSimpleName()).append(":").append(tk.getName());
} else if (tk instanceof BinaryOperation) {
BinaryOperation bo = (BinaryOperation) tk;
sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName()).append("} {").append(bo.getRight().getName()).append("}");
} else {
// noinspection StringConcatenationInsideStringBufferAppend
sbuf.append("NODE [" + tk.getClass().getSimpleName() + "] :: " + tk.getName());
}
sbuf.append("\n");
}
sbuf.append("==END==");
return sbuf.toString();
}
Aggregations