Search in sources :

Example 56 with MapVariableResolverFactory

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);
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Example 57 with MapVariableResolverFactory

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);
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 58 with MapVariableResolverFactory

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;
}
Also used : CountIterator(org.mvel2.templates.util.CountIterator) HashMap(java.util.HashMap) TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) CountIterator(org.mvel2.templates.util.CountIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 59 with MapVariableResolverFactory

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));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Example 60 with MapVariableResolverFactory

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));
// 
// }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) File(java.io.File)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)79 HashMap (java.util.HashMap)43 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)29 ParserContext (org.mvel2.ParserContext)24 Serializable (java.io.Serializable)18 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)17 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)14 TemplateRegistry (org.mvel2.templates.TemplateRegistry)13 Debugger (org.mvel2.debug.Debugger)12 Frame (org.mvel2.debug.Frame)12 HashSet (java.util.HashSet)10 MapObject (org.mvel2.tests.core.res.MapObject)10 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)9 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 Test (org.junit.Test)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6