use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class CoreConfidenceTests method testInlineConstructor.
public void testInlineConstructor() {
String str = "cheese = new Cheese().{ type = $c.type };";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("$c", Cheese.class);
pctx.addImport(Cheese.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Cheese $c = new Cheese();
$c.setType("stilton");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("$c", $c);
Cheese cheese = (Cheese) MVEL.executeExpression(stmt, vars);
assertEquals("stilton", cheese.getType());
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class CoreConfidenceTests method testContextObjMethodCall.
public void testContextObjMethodCall() {
String str = "getName() == \"bob\"";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("this", Bar.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Bar ctx = new Bar();
ctx.setName("bob");
Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
assertTrue(result);
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class ArithmeticTests method testModExpr.
public void testModExpr() {
String str = "$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("$y", int.class);
Map<String, Object> vars = new HashMap<String, Object>();
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
for (int i = 0; i < 500; i++) {
int y = i;
boolean expected = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
vars.put("$y", y);
assertEquals(expected, MVEL.eval(str, vars));
assertEquals(expected, ((Boolean) MVEL.executeExpression(stmt, null, vars)).booleanValue());
}
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class TypesAndInferenceTests method testGenerics1.
public void testGenerics1() {
String str = "addresses[0] == new Address(\"s1\") && addresses[0].street == new Address(\"s1\").street";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("this", PersonAddresses.class);
pctx.addImport(Address.class);
pctx.addImport(PersonAddresses.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
PersonAddresses ctx = new PersonAddresses();
ctx.getAddresses().add(new Address("s1"));
Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
assertTrue(result);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ASTNode method optimize.
private Object optimize(Object ctx, Object thisValue, VariableResolverFactory factory) {
if ((fields & DEOP) != 0) {
fields ^= DEOP;
}
AccessorOptimizer optimizer;
Object retVal = null;
if ((fields & NOJIT) != 0 || factory != null && factory.isResolveable(getName())) {
optimizer = getAccessorCompiler(SAFE_REFLECTIVE);
} else {
optimizer = getDefaultAccessorCompiler();
}
ParserContext pCtx;
if ((fields & PCTX_STORED) != 0) {
pCtx = (ParserContext) literal;
} else {
pCtx = new ParserContext(new ParserConfiguration(getInjectedImports(factory), null));
}
try {
pCtx.optimizationNotify();
setAccessor(optimizer.optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, egressType));
} catch (OptimizationNotSupported ne) {
setAccessor((optimizer = getAccessorCompiler(SAFE_REFLECTIVE)).optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, null));
}
if (accessor == null) {
return get(expr, start, offset, ctx, factory, thisValue, pCtx);
}
if (retVal == null) {
retVal = optimizer.getResultOptPass();
}
if (egressType == null) {
egressType = optimizer.getEgressType();
}
return retVal;
}
Aggregations