use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class DebuggerTests method testBreakpoints.
public void testBreakpoints() {
ParserContext ctx = new ParserContext();
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n", ctx);
System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
CompiledExpression compiled = compiler.compile();
MVELRuntime.registerBreakpoint("test.mv", 7);
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(10, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
assertTrue("did not break at line 7", breaked.contains(7));
}
use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class FunctionsTest method testFunctionReuse.
public void testFunctionReuse() {
VariableResolverFactory functionFactory = new MapVariableResolverFactory();
MVEL.eval("def foo() { \"foo\"; }; def bar() { \"bar\" };", functionFactory);
VariableResolverFactory myVarFactory = new MapVariableResolverFactory();
myVarFactory.setNextFactory(functionFactory);
Serializable s = MVEL.compileExpression("foo() + bar();");
assertEquals("foobar", MVEL.executeExpression(s, myVarFactory));
}
use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class FunctionsTest method testMVEL225.
public void testMVEL225() {
Serializable compileExpression = MVEL.compileExpression("def f() { int a=1;a++;return a; }; f();");
MapVariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
assertEquals(2, MVEL.executeExpression(compileExpression, factory));
}
use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class ForEachNode method eval.
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
Iterator[] iters = new Iterator[item.length];
Object o;
for (int i = 0; i < iters.length; i++) {
if ((o = MVEL.eval(expression[i], ctx, factory)) instanceof Iterable) {
iters[i] = ((Iterable) o).iterator();
} else if (o instanceof Object[]) {
iters[i] = new ArrayIterator((Object[]) o);
} else {
throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
}
}
Map<String, Object> locals = new HashMap<String, Object>();
MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
int iterate = iters.length;
while (true) {
for (int i = 0; i < iters.length; i++) {
if (!iters[i].hasNext()) {
iterate--;
locals.put(item[i], "");
} else {
locals.put(item[i], iters[i].next());
}
}
if (iterate != 0) {
nestedNode.eval(runtime, appender, ctx, localFactory);
if (sepExpr != null) {
for (Iterator it : iters) {
if (it.hasNext()) {
appender.append(String.valueOf(MVEL.eval(sepExpr, ctx, factory)));
break;
}
}
}
} else
break;
}
return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class SharedFuncLib method eval.
public <T> T eval(String formula, Map<String, Object> context, Class<T> toType) {
VariableResolverFactory myVariableResolverFactory = new MapVariableResolverFactory();
myVariableResolverFactory.setNextFactory(functionFactory);
return MVEL.eval(formula, context, myVariableResolverFactory, toType);
}
Aggregations