use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMVEL236.
public void testMVEL236() {
StringBuffer buffer = new StringBuffer();
buffer.append("MyInterface2 var2 = (MyInterface2)var1;");
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("var1", MyInterface3.class);
ctx.addImport(MyInterface2.class);
ctx.addImport(MyInterface3.class);
try {
CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testParameterizedTypeInStrictMode2.
public void testParameterizedTypeInStrictMode2() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("ctx", Object.class);
ExpressionCompiler compiler = new ExpressionCompiler("org.mvel2.DataConversion.convert(ctx, String).toUpperCase()");
assertEquals(String.class, compiler.compile(ctx).getKnownEgressType());
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testTypeVarDeclr.
public void testTypeVarDeclr() {
String ex = "String a;";
ParserContext ctx = new ParserContext();
ExpressionCompiler compiler = new ExpressionCompiler(ex);
compiler.compile(ctx);
assertNotNull(ctx.getVariables());
assertEquals(1, ctx.getVariables().entrySet().size());
for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
assertEquals(String.class, entry.getValue());
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TemplateTests method testMVEL244.
public void testMVEL244() {
Foo244 foo = new Foo244("plop");
String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", foo);
System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ForNode method buildForEach.
private boolean buildForEach(char[] condition, int start, int offset, int blockStart, int blockEnd, int fields, ParserContext pCtx) {
int end = start + offset;
int cursor = nextCondPart(condition, start, end, false);
boolean varsEscape = false;
try {
ParserContext spCtx = pCtx;
if (pCtx != null) {
spCtx = pCtx.createSubcontext().createColoringSubcontext();
} else {
spCtx = new ParserContext();
}
this.initializer = (ExecutableStatement) subCompileExpression(condition, start, cursor - start - 1, spCtx);
if (pCtx != null) {
pCtx.pushVariableScope();
}
try {
expectType(this.condition = (ExecutableStatement) subCompileExpression(condition, start = cursor, (cursor = nextCondPart(condition, start, end, false)) - start - 1, spCtx), Boolean.class, ((fields & COMPILE_IMMEDIATE) != 0));
} catch (CompileException e) {
if (e.getExpr().length == 0) {
e.setExpr(expr);
while (start < expr.length && ParseTools.isWhitespace(expr[start])) {
start++;
}
e.setCursor(start);
}
throw e;
}
this.after = (ExecutableStatement) subCompileExpression(condition, start = cursor, (nextCondPart(condition, start, end, true)) - start, spCtx);
if (spCtx != null && (fields & COMPILE_IMMEDIATE) != 0 && spCtx.isVariablesEscape()) {
if (pCtx != spCtx)
pCtx.addVariables(spCtx.getVariables());
varsEscape = true;
} else if (spCtx != null && pCtx != null) {
pCtx.addVariables(spCtx.getVariables());
}
this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockEnd, spCtx);
} catch (NegativeArraySizeException e) {
throw new CompileException("wrong syntax; did you mean to use 'foreach'?", expr, start);
}
return varsEscape;
}
Aggregations