use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.
the class ForEachNode method getReducedValue.
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver(item);
ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
Object iterCond = MVEL.eval(expr, start, offset, thisValue, factory);
if (itemType != null && itemType.isArray())
enforceTypeSafety(itemType, getBaseComponentType(iterCond.getClass()));
this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockOffset);
Object v;
if (iterCond instanceof Iterable) {
for (Object o : (Iterable) iterCond) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond != null && iterCond.getClass().isArray()) {
int len = Array.getLength(iterCond);
for (int i = 0; i < len; i++) {
itemR.setValue(Array.get(iterCond, i));
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond instanceof CharSequence) {
for (Object o : iterCond.toString().toCharArray()) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond instanceof Integer) {
int max = (Integer) iterCond + 1;
for (int i = 1; i != max; i++) {
itemR.setValue(i);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else {
throw new CompileException("non-iterable type: " + (iterCond != null ? iterCond.getClass().getName() : "null"), expr, start);
}
return null;
}
use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.
the class ForEachNode method getReducedValueAccelerated.
public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver(item);
ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
Object iterCond = condition.getValue(ctx, thisValue, factory);
if (type == -1) {
determineIterType(iterCond.getClass());
}
Object v;
switch(type) {
case ARRAY:
int len = Array.getLength(iterCond);
for (int i = 0; i < len; i++) {
itemR.setValue(Array.get(iterCond, i));
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
break;
case CHARSEQUENCE:
for (Object o : iterCond.toString().toCharArray()) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
break;
case INTEGER:
int max = (Integer) iterCond + 1;
for (int i = 1; i != max; i++) {
itemR.setValue(i);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
break;
case ITERABLE:
for (Object o : (Iterable) iterCond) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
break;
}
return null;
}
use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.
the class Function method call.
public Object call(Object ctx, Object thisValue, VariableResolverFactory factory, Object[] parms) {
if (parms != null && parms.length != 0) {
// detect tail recursion
if (factory instanceof FunctionVariableResolverFactory && ((FunctionVariableResolverFactory) factory).getIndexedVariableResolvers().length == parms.length) {
FunctionVariableResolverFactory fvrf = (FunctionVariableResolverFactory) factory;
if (fvrf.getFunction().equals(this)) {
VariableResolver[] swapVR = fvrf.getIndexedVariableResolvers();
fvrf.updateParameters(parms);
try {
return compiledBlock.getValue(ctx, thisValue, fvrf);
} finally {
fvrf.setIndexedVariableResolvers(swapVR);
}
}
}
return compiledBlock.getValue(thisValue, new FunctionVariableResolverFactory(this, factory, parameters, parms));
} else if (cMode) {
return compiledBlock.getValue(thisValue, new DefaultLocalVariableResolverFactory(factory, parameters).setNoTilt(true));
} else {
return compiledBlock.getValue(thisValue, new DefaultLocalVariableResolverFactory(factory, parameters).setNoTilt(true));
}
}
use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.
the class CoreConfidenceTests method testDynamicImportsWithNullConstructorParam.
public void testDynamicImportsWithNullConstructorParam() {
ParserContext ctx = new ParserContext();
ctx.addPackageImport("org.mvel2.tests.core.res");
ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", null)");
Cheesery p1 = new Cheesery("bobbo", null);
Cheesery p2 = (Cheesery) executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory());
assertEquals(p1, p2);
}
use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.
the class CoreConfidenceTests method testWhileUsingImports.
public void testWhileUsingImports() {
Map<String, Object> imports = new HashMap<String, Object>();
imports.put("ArrayList", java.util.ArrayList.class);
imports.put("List", java.util.List.class);
ParserContext context = new ParserContext(imports, null, "testfile");
ExpressionCompiler compiler = new ExpressionCompiler("List list = new ArrayList(); return (list == empty)");
assertTrue((Boolean) executeExpression(compiler.compile(context), new DefaultLocalVariableResolverFactory()));
}
Aggregations