use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testGenericMethods.
public void testGenericMethods() {
String str = "Integer.parseInt( a.getMap().get(\"x\") )";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("a", AGenericTestClass.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
AGenericTestClass a = new AGenericTestClass();
a.setMap(new HashMap<String, String>());
a.getMap().put("x", "10");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("a", a);
Number result = (Number) MVEL.executeExpression(stmt, null, variables);
assertEquals(10, result.intValue());
}
use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testGetCorrectInputs.
public void testGetCorrectInputs() {
String str = "total = total + $cheese.price";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("total", int.class);
pctx.addInput("$cheese", Cheese.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
}
use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.
the class WithTests method testWithAndEnumInPackageImport.
public void testWithAndEnumInPackageImport() {
ParserConfiguration pconf = new ParserConfiguration();
pconf.addPackageImport(MyEnum.class.getPackage().getName());
ParserContext pCtx = new ParserContext(pconf);
pCtx.setStrongTyping(true);
pCtx.addInput("thing", Thing.class);
Thing thing = new Thing("xxx");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("thing", thing);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
MVEL.executeExpression(stmt, null, vars);
assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
use of org.mule.mvel2.ParserConfiguration in project kie-wb-common by kiegroup.
the class EnumDropdownServiceImpl method loadDropDownExpression.
protected String[] loadDropDownExpression(final ClassLoader classLoader, final MVELEvaluator mvelEvaluator, final String[] valuePairs, String expression) {
try {
final Map<String, String> context = new HashMap<String, String>();
for (final String valuePair : valuePairs) {
if (valuePair == null) {
return new String[0];
}
String[] pair = valuePair.split("=");
if (pair.length == 1) {
String[] swap = new String[2];
swap[0] = pair[0];
swap[1] = "";
pair = swap;
}
context.put(pair[0], pair[1]);
}
// first interpolate the pairs
expression = (String) TemplateRuntime.eval(expression, context);
// now we can eval it for real...
final ParserConfiguration pconf = new ParserConfiguration();
final ParserContext pctx = new ParserContext(pconf);
pconf.setClassLoader(classLoader);
final Serializable compiled = MVEL.compileExpression(expression, pctx);
Object result = mvelEvaluator.executeExpression(compiled, new HashMap<String, Object>());
// Handle result of evaluation
if (result instanceof String[]) {
return (String[]) result;
} else if (result instanceof List) {
List l = (List) result;
String[] xs = new String[l.size()];
for (int i = 0; i < xs.length; i++) {
Object el = l.get(i);
xs[i] = el.toString();
}
return xs;
} else {
return null;
}
} catch (Exception e) {
throw ExceptionUtilities.handleException(e);
}
}
use of org.mule.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);
}
}
}
}
Aggregations