use of org.mule.mvel2.ParserContext in project mvel by mikebrock.
the class DebuggerTests method testBreakpoints3.
public void testBreakpoints3() {
String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n";
ExpressionCompiler compiler = new ExpressionCompiler(expr);
ParserContext context = new ParserContext();
context.addImport("System", System.class);
context.setStrictTypeEnforcement(true);
context.setDebugSymbols(true);
context.setSourceFile("mysource");
String s = org.mvel2.debug.DebugTools.decompile(compiler.compile(context));
System.out.println("output: " + s);
int fromIndex = 0;
int count = 0;
while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
count++;
}
assertEquals(4, count);
}
use of org.mule.mvel2.ParserContext in project mvel by mikebrock.
the class FailureTests method testShouldFail6.
public void testShouldFail6() {
try {
ParserContext pctx = new ParserContext();
pctx.setStrongTyping(true);
MVEL.compileExpression("new int[] {1.5}", pctx);
} catch (Exception e) {
return;
}
shouldThrowException();
}
use of org.mule.mvel2.ParserContext in project mvel by mikebrock.
the class WithTests method testWithAndEnumInPackageImport.
public void testWithAndEnumInPackageImport() {
ParserConfiguration pconf = new ParserConfiguration();
pconf.addPackageImport(MyEnum.class.getPackage().getName());
ParserContext pCtx = new ParserContext(pconf);
pCtx.setStrongTyping(true);
pCtx.addInput("thing", Thing.class);
Thing thing = new Thing("xxx");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("thing", thing);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
MVEL.executeExpression(stmt, null, vars);
assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
use of org.mule.mvel2.ParserContext 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.ParserContext in project mvel by mikebrock.
the class VariableSpaceCompiler method compileShared.
public static SharedVariableSpaceModel compileShared(String expr, ParserContext pCtx, Object[] vars) {
String[] varNames = pCtx.getIndexedVarNames();
ParserContext analysisContext = ParserContext.create();
analysisContext.setIndexAllocation(true);
MVEL.analysisCompile(expr, analysisContext);
Set<String> localNames = analysisContext.getVariables().keySet();
pCtx.addIndexedLocals(localNames);
String[] locals = localNames.toArray(new String[localNames.size()]);
String[] allVars = new String[varNames.length + locals.length];
System.arraycopy(varNames, 0, allVars, 0, varNames.length);
System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
return new SharedVariableSpaceModel(allVars, vars);
}
Aggregations