use of org.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.mvel2.ParserConfiguration in project mvel by mikebrock.
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(nameCache)) {
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);
}
if (retVal == null) {
retVal = optimizer.getResultOptPass();
}
if (egressType == null) {
egressType = optimizer.getEgressType();
}
return retVal;
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class CoreConfidenceTests method testContextFieldNotFound.
public void testContextFieldNotFound() {
String str = "'stilton'.equals( type );";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.addInput("this", Cheese.class);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
MVEL.executeExpression(stmt, new Cheese(), new HashMap());
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class CoreConfidenceTests method testStrTriangleEqualsEquals.
public void testStrTriangleEqualsEquals() {
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
try {
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.addInput("this", Triangle.class);
pctx.setStrongTyping(true);
String str = "this.strLabel == this";
try {
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
fail("should have failed");
} catch (CompileException e) {
System.out.println();
return;
}
} finally {
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
}
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class CoreConfidenceTests method testStrictModeAddAll.
public void testStrictModeAddAll() {
String str = "list.addAll( o );";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("o", Object.class);
pctx.addInput("list", ArrayList.class);
try {
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
fail("This should not compileShared, as o is not of a type Collection");
} catch (Exception e) {
}
}
Aggregations