Search in sources :

Example 1 with DefaultLocalVariableResolverFactory

use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.

the class TypesAndInferenceTests method testStrictTypingCompilation3.

public void testStrictTypingCompilation3() throws NoSuchMethodException {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler("message='Hello';b=7;\nSystem.out.println(message + ';' + b);\n" + "System.out.println(message + ';' + b); b");
    assertEquals(7, executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory()));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 2 with DefaultLocalVariableResolverFactory

use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.

the class CoreConfidenceTests method testDynamicImports.

public void testDynamicImports() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("java.util");
    ExpressionCompiler compiler = new ExpressionCompiler("HashMap");
    Serializable s = compiler.compile(ctx);
    assertEquals(HashMap.class, executeExpression(s));
    compiler = new ExpressionCompiler("map = new HashMap(); map.size()");
    s = compiler.compile(ctx);
    assertEquals(0, executeExpression(s, new DefaultLocalVariableResolverFactory()));
}
Also used : Serializable(java.io.Serializable) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 3 with DefaultLocalVariableResolverFactory

use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.

the class CoreConfidenceTests method testDynamicImportsOnNestedExpressions.

public void testDynamicImportsOnNestedExpressions() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", new Cheese(\"cheddar\", 15))");
    Cheesery p1 = new Cheesery("bobbo", new Cheese("cheddar", 15));
    Cheesery p2 = (Cheesery) executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory());
    assertEquals(p1, p2);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 4 with DefaultLocalVariableResolverFactory

use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.

the class Fold method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver("$");
    ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
    List list;
    if (constraintEx != null) {
        Collection col = ((Collection) dataEx.getValue(ctx, thisValue, factory));
        list = new ArrayList(col.size());
        for (Object o : col) {
            itemR.value = o;
            if ((Boolean) constraintEx.getValue(ctx, thisValue, itemFactory)) {
                list.add(subEx.getValue(o, thisValue, itemFactory));
            }
        }
    } else {
        Collection col = ((Collection) dataEx.getValue(ctx, thisValue, factory));
        list = new ArrayList(col.size());
        for (Object o : col) {
            list.add(subEx.getValue(itemR.value = o, thisValue, itemFactory));
        }
    }
    return list;
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ArrayList(java.util.ArrayList) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) ItemResolverFactory(org.mvel2.integration.impl.ItemResolverFactory)

Example 5 with DefaultLocalVariableResolverFactory

use of org.mvel2.integration.impl.DefaultLocalVariableResolverFactory in project mvel by mikebrock.

the class Fold method getReducedValue.

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver("$");
    ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
    List list;
    if (constraintEx != null) {
        Object x = dataEx.getValue(ctx, thisValue, factory);
        if (!(x instanceof Collection))
            throw new CompileException("was expecting type: Collection; but found type: " + (x == null ? "null" : x.getClass().getName()), expr, start);
        list = new ArrayList(((Collection) x).size());
        for (Object o : (Collection) x) {
            itemR.value = o;
            if ((Boolean) constraintEx.getValue(ctx, thisValue, itemFactory)) {
                list.add(subEx.getValue(o, thisValue, itemFactory));
            }
        }
    } else {
        Object x = dataEx.getValue(ctx, thisValue, factory);
        if (!(x instanceof Collection))
            throw new CompileException("was expecting type: Collection; but found type: " + (x == null ? "null" : x.getClass().getName()), expr, start);
        list = new ArrayList(((Collection) x).size());
        for (Object o : (Collection) x) {
            list.add(subEx.getValue(itemR.value = o, thisValue, itemFactory));
        }
    }
    return list;
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CompileException(org.mvel2.CompileException) List(java.util.List) ArrayList(java.util.ArrayList) ItemResolverFactory(org.mvel2.integration.impl.ItemResolverFactory)

Aggregations

DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)13 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)8 ItemResolverFactory (org.mvel2.integration.impl.ItemResolverFactory)4 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 CompileException (org.mvel2.CompileException)2 ParserContext (org.mvel2.ParserContext)2 CompiledExpression (org.mvel2.compiler.CompiledExpression)2 Debugger (org.mvel2.debug.Debugger)2 Frame (org.mvel2.debug.Frame)2 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)2 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)2 java.util (java.util)1 VariableResolver (org.mvel2.integration.VariableResolver)1 FunctionVariableResolverFactory (org.mvel2.integration.impl.FunctionVariableResolverFactory)1