use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.
the class CoreConfidenceTests method testCheeseConstructor.
// public void testClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(HashMap.class);
//
// ResolverTools.appendFactory(mvf, classes);
//
// assertTrue(executeExpression(compileExpression("HashMap map = new HashMap()",
// classes.getImportedClasses()),
// mvf) instanceof HashMap);
// }
//
// public void testSataticClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(Person.class);
//
// ResolverTools.appendFactory(mvf,
// classes);
//
// assertEquals("tom",
// executeExpression(compileExpression("p = new Person('tom'); return p.name;",
// classes.getImportedClasses()),
// mvf));
// }
public void testCheeseConstructor() {
MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
ClassImportResolverFactory classes = new ClassImportResolverFactory();
classes.addClass(Cheese.class);
ResolverTools.appendFactory(mvf, classes);
assertTrue(executeExpression(compileExpression("cheese = new Cheese(\"cheddar\", 15);", classes.getImportedClasses()), mvf) instanceof Cheese);
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class NewObjectPrototype method getReducedValue.
@Override
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
final MapVariableResolverFactory resolverFactory = new MapVariableResolverFactory(new HashMap<String, Object>(), factory);
function.getCompiledBlock().getValue(ctx, thisValue, resolverFactory);
return new PrototypalFunctionInstance(function, resolverFactory);
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class CompiledForEachNode method eval.
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
Iterator[] iters = new Iterator[item.length];
Object o;
for (int i = 0; i < iters.length; i++) {
if ((o = MVEL.executeExpression(ce[i], ctx, factory)) instanceof Iterable) {
iters[i] = ((Iterable) o).iterator();
} else if (o instanceof Object[]) {
iters[i] = new ArrayIterator((Object[]) o);
} else if (o instanceof Integer) {
iters[i] = new CountIterator((Integer) o);
} else {
throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
}
}
Map<String, Object> locals = new HashMap<String, Object>();
MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
int iterate = iters.length;
while (true) {
for (int i = 0; i < iters.length; i++) {
if (!iters[i].hasNext()) {
iterate--;
locals.put(item[i], "");
} else {
locals.put(item[i], iters[i].next());
}
}
if (iterate != 0) {
nestedNode.eval(runtime, appender, ctx, localFactory);
if (sepExpr != null) {
for (Iterator it : iters) {
if (it.hasNext()) {
appender.append(String.valueOf(MVEL.executeExpression(cSepExpr, ctx, factory)));
break;
}
}
}
} else
break;
}
return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class WithTests method testSataticClassImportViaFactoryAndWithModification.
public void testSataticClassImportViaFactoryAndWithModification() {
OptimizerFactory.setDefaultOptimizer("ASM");
MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
ClassImportResolverFactory classes = new ClassImportResolverFactory(null, null, false);
classes.addClass(Person.class);
ResolverTools.appendFactory(mvf, classes);
assertEquals(21, executeExpression(compileExpression("p = new Person('tom'); p.age = 20; " + "with( p ) { age = p.age + 1 }; return p.age;", classes.getImportedClasses()), mvf));
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class CompiledPerformanceTests method testQuickSort.
public void testQuickSort() throws IOException {
Serializable s = MVEL.compileExpression(new String(ParseTools.loadFromFile(new File("samples/scripts/fquicksort.mvel"))));
HashMap map = new HashMap();
MapVariableResolverFactory mvrf = new MapVariableResolverFactory(map);
for (int i = 0; i < 1000000; i++) {
MVEL.executeExpression(s, mvrf);
mvrf.clear();
}
// for (int x = 0; x < 4; x++) {
// Serializable s = MVEL.compileSetExpression("tak.bar.name");
// long time = System.currentTimeMillis();
//
// for (int i = 0; i < ITERATIONS; i++) {
// MVEL.executeSetExpression(s, map, "foobie");
// }
//
// System.out.println("SET PERFORMANCE: " + (System.currentTimeMillis() - time));
//
// time = System.currentTimeMillis();
//
// s = MVEL.compileExpression("tak.bar.name");
//
// for (int i = 0; i < ITERATIONS; i++) {
// MVEL.executeExpression(s, map);
// }
//
// System.out.println("GET PERFORMANCE: " + (System.currentTimeMillis() - time));
//
// }
}
Aggregations