use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class InlineCollectionsTests method testToListStrictMode.
@SuppressWarnings({ "UnnecessaryBoxing" })
public void testToListStrictMode() {
String text = "misc.toList(foo.bar.name, 'hello', 42, ['key1' : 'value1'," + " c : [ foo.bar.age, 'car', 42 ]], [42, [c : 'value1']] )";
ParserContext ctx = new ParserContext();
ctx.addInput("misc", MiscTestClass.class);
ctx.addInput("foo", Foo.class);
ctx.addInput("c", String.class);
ctx.setStrictTypeEnforcement(true);
ExpressionCompiler compiler = new ExpressionCompiler(text, ctx);
List list = (List) executeExpression(compiler.compile(), createTestMap());
assertSame("dog", list.get(0));
assertEquals("hello", list.get(1));
assertEquals(new Integer(42), list.get(2));
Map map = (Map) list.get(3);
assertEquals("value1", map.get("key1"));
List nestedList = (List) map.get("cat");
assertEquals(14, nestedList.get(0));
assertEquals("car", nestedList.get(1));
assertEquals(42, nestedList.get(2));
nestedList = (List) list.get(4);
assertEquals(42, nestedList.get(0));
map = (Map) nestedList.get(1);
assertEquals("value1", map.get("cat"));
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class TemplateCompiler method captureOrbInternal.
private int captureOrbInternal() {
try {
ParserContext pCtx = new ParserContext();
cursor = balancedCaptureWithLineAccounting(template, start = cursor, length, '{', pCtx);
line += pCtx.getLineCount();
int ret = start + 1;
start = cursor + 1;
return ret;
} catch (CompileException e) {
e.setLineNumber(line);
e.setColumn((cursor - colStart) + 1);
throw e;
}
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class VariableSpaceCompiler method compile.
public static SimpleVariableSpaceModel compile(String expr, ParserContext pCtx) {
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 SimpleVariableSpaceModel(allVars);
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class GenericsTypeInferenceTest method testInferLastTypeParametersFromPropertyMethod.
public final void testInferLastTypeParametersFromPropertyMethod() {
ParserContext context = new ParserContext();
context.setStrongTyping(true);
context.addInput("a", A.class);
final CompiledExpression compiledExpression = new ExpressionCompiler("a.getFooMap()[\"key\"].someMethod()", context).compile();
final Object val = MVEL.executeExpression(compiledExpression, new AWrapper());
assertEquals("Expression did not evaluate correctly: ", "bar", val);
assertNotNull("No type parameters detected", context.getLastTypeParameters());
assertEquals("Wrong parametric type inferred", String.class, context.getLastTypeParameters()[0]);
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class ArithmeticTests method testMathCeilWithDoubleCast.
public void testMathCeilWithDoubleCast() {
String str = "Math.ceil( (double) x / 3 )";
ParserConfiguration pconf = new ParserConfiguration();
pconf.addImport("Math", Math.class);
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("x", Integer.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Map vars = new HashMap();
vars.put("x", 4);
assertEquals(Math.ceil((double) 4 / 3), MVEL.executeExpression(stmt, vars));
}
Aggregations