use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class PrimitiveTypesTest method testFloatPrimitive.
public void testFloatPrimitive() {
ParserConfiguration conf = new ParserConfiguration();
conf.addImport(FactWithFloat.class);
ParserContext pctx = new ParserContext(conf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("this", FactWithFloat.class);
boolean result = (Boolean) MVEL.executeExpression(MVEL.compileExpression("floatValue == 15.1", pctx), new FactWithFloat(15.1f));
assertTrue(result);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class AsmOptimizerOsgiTest method testCollectionAccessWithInvalidThreadClassLoader.
public void testCollectionAccessWithInvalidThreadClassLoader() {
String expression = "['A', 'B', 'C'] contains 'B'";
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setClassLoader(MVEL.class.getClassLoader());
Serializable compiledExpression = MVEL.compileExpression(expression, new ParserContext(parserConfiguration));
ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(NO_MVEL_CL);
for (int i = 0; i <= DynamicOptimizer.tenuringThreshold; i++) {
Object result = MVEL.executeExpression(compiledExpression);
assertEquals(Boolean.TRUE, result);
}
} finally {
Thread.currentThread().setContextClassLoader(currentCl);
}
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ArraysTests method testMultiDimensionalArrayType.
public void testMultiDimensionalArrayType() {
String str = "$c.cheeses[0][0] = new Cheese('brie', 15)";
ParserConfiguration pconf = new ParserConfiguration();
pconf.addImport(Cheese.class);
ParserContext pctx = new ParserContext(pconf);
pctx.addInput("$c", Column.class);
pctx.setStrongTyping(true);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Map<String, Object> vars = new HashMap<String, Object>();
Column c = new Column("x", 1);
c.setCheeses(new Cheese[5][5]);
vars.put("$c", c);
MVEL.executeExpression(stmt, null, vars);
assertEquals(new Cheese("brie", 15), c.getCheeses()[0][0]);
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class PatternBuilder method setInputs.
protected void setInputs(RuleBuildContext context, ExprBindings descrBranch, Class<?> thisClass, String expr) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
ParserConfiguration conf = data.getParserConfiguration();
conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
final ParserContext pctx = new ParserContext(conf);
pctx.setStrictTypeEnforcement(false);
pctx.setStrongTyping(false);
pctx.addInput("this", thisClass);
// overrides the mvel empty label
pctx.addInput("empty", boolean.class);
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
try {
MVEL.analysisCompile(expr, pctx);
} catch (Exception e) {
// reported during expression analysis, so swallow it at the moment
return;
}
if (!pctx.getInputs().isEmpty()) {
for (String v : pctx.getInputs().keySet()) {
// to an "empty" property, or the if will evaluate to true even if it doesn't
if ("this".equals(v) || (PropertyTools.getFieldOrAccessor(thisClass, v) != null && expr.matches("(^|.*\\W)empty($|\\W.*)"))) {
descrBranch.getFieldAccessors().add(v);
} else if ("empty".equals(v)) {
// do nothing
} else if (!context.getPkg().getGlobals().containsKey(v)) {
descrBranch.getRuleBindings().add(v);
} else {
descrBranch.getGlobalBindings().add(v);
}
}
}
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELBeanCreator method createBean.
@Override
public <T> T createBean(ClassLoader cl, String type, QualifierModel qualifier) throws Exception {
if (qualifier != null) {
throw new IllegalArgumentException("Cannot use a qualifier without a CDI container");
}
ParserConfiguration config = new ParserConfiguration();
config.setClassLoader(cl);
ParserContext ctx = new ParserContext(config);
if (parameters != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
ctx.addVariable(entry.getKey(), entry.getValue().getClass());
}
}
Object compiledExpression = MVEL.compileExpression(type, ctx);
return (T) MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, parameters);
}
Aggregations