use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class CoreConfidenceTests method testStringConcatenation3.
public void testStringConcatenation3() {
// BUG: return type of the string concatenation is inferred as double instead of String
String ex = "services.log($av + \"Drop +5%: \"+$sb+\" avg: $\"+percent($av)+\" price: $\"+$pr );";
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.setStrictTypeEnforcement(true);
ctx.addInput("$sb", String.class);
ctx.addInput("$av", double.class);
ctx.addInput("$pr", double.class);
ctx.addInput("services", Services.class);
ctx.addImport("percent", MVEL.getStaticMethod(String.class, "valueOf", new Class[] { double.class }));
try {
Serializable compiledExpression = MVEL.compileExpression(ex, ctx);
Services services = new Services() {
public void log(String text) {
}
};
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("services", services);
vars.put("$sb", "RHT");
vars.put("$av", 15.0);
vars.put("$pr", 10.0);
MVEL.executeExpression(compiledExpression, vars);
} catch (Throwable e) {
e.printStackTrace();
fail("Should not raise exception: " + e.getMessage());
}
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class CoreConfidenceTests method testSetAccessorOverloadedEqualsStrictMode.
public void testSetAccessorOverloadedEqualsStrictMode() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("foo", Foo.class);
try {
CompiledExpression expr = new ExpressionCompiler("foo.bar = 0").compile(ctx);
} catch (CompileException e) {
// should fail.
e.printStackTrace();
return;
}
assertTrue(false);
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class DebuggerTests method testBreakpoints5.
public void testBreakpoints5() {
OptimizerFactory.setDefaultOptimizer("ASM");
String expression = "System.out.println('foo');\r\n" + "a = new Foo244();\r\n" + "a.name = 'bar';\r\n" + "foo.happy();\r\n" + "System.out.println( 'name:' + a.name ); \r\n" + "System.out.println( 'name:' + a.name ); \r\n" + "System.out.println( 'name:' + a.name ); \r\n" + "return a.name;";
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
Map<String, Macro> macros = new HashMap<String, Macro>();
expression = parseMacros(expression, macros);
ExpressionCompiler compiler = new ExpressionCompiler(expression);
ParserContext ctx = new ParserContext();
ctx.setSourceFile("test2.mv");
ctx.setDebugSymbols(true);
ctx.addImport("Foo244", Foo.class);
ctx.setInterceptors(interceptors);
CompiledExpression compiled = compiler.compile(ctx);
System.out.println("\nExpression:------------");
System.out.println(expression);
System.out.println("------------");
System.out.println(DebugTools.decompile(compiled));
MVELRuntime.registerBreakpoint("test2.mv", 1);
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 Debugger.STEP_OVER;
}
};
MVELRuntime.setThreadDebugger(testDebugger);
System.out.println("\n==RUN==\n");
assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
assertTrue("did not break at line 1", breaked.contains(1));
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class DebuggerTests method testBreakpoints.
public void testBreakpoints() {
ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n");
System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
ParserContext ctx = new ParserContext();
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
CompiledExpression compiled = compiler.compile(ctx);
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.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class DebuggerTests method testDebuggerInvoke2.
public void testDebuggerInvoke2() {
count = 0;
MVELRuntime.resetDebugger();
MVELRuntime.setThreadDebugger(new Debugger() {
public int onBreak(Frame frame) {
count++;
return 0;
}
});
String src = "a1=7;\na2=8;\nSystem.out.println(\"h\");\nac=23;\nde=23;\nge=23;\ngef=34;";
ExpressionCompiler c = new ExpressionCompiler(src);
ParserContext ctx = new ParserContext();
ctx.setSourceFile("mysource");
ctx.setDebugSymbols(true);
CompiledExpression compexpr = c.compile(ctx);
System.out.println(decompile(compexpr));
MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 2);
MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 4);
MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 5);
VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
MVEL.executeDebugger(compexpr, null, factory);
System.out.println(count);
assertEquals(5, count);
}
Aggregations