Search in sources :

Example 6 with DefaultLocalVariableResolverFactory

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

Example 7 with DefaultLocalVariableResolverFactory

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

Example 8 with DefaultLocalVariableResolverFactory

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

Example 9 with DefaultLocalVariableResolverFactory

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

Example 10 with DefaultLocalVariableResolverFactory

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()));
}
Also used : java.util(java.util) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

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