Search in sources :

Example 41 with Type

use of org.mvel2.asm.Type 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)

Example 42 with Type

use of org.mvel2.asm.Type 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 43 with Type

use of org.mvel2.asm.Type 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 44 with Type

use of org.mvel2.asm.Type in project mvel by mikebrock.

the class InlineCollectionNode method execGraph.

private Object execGraph(Object o, Class type, Object ctx, VariableResolverFactory factory) {
    if (o instanceof List) {
        ArrayList list = new ArrayList(((List) o).size());
        for (Object item : (List) o) {
            list.add(execGraph(item, type, ctx, factory));
        }
        return list;
    } else if (o instanceof Map) {
        HashMap map = new HashMap();
        for (Object item : ((Map) o).keySet()) {
            map.put(execGraph(item, type, ctx, factory), execGraph(((Map) o).get(item), type, ctx, factory));
        }
        return map;
    } else if (o instanceof Object[]) {
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        Object newArray = Array.newInstance(getSubComponentType(type), ((Object[]) o).length);
        try {
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + getBaseComponentType(type).getName() + ";", AbstractParser.getCurrentThreadParserContext()) : type;
            int c = 0;
            for (Object item : (Object[]) o) {
                Array.set(newArray, c++, execGraph(item, cls, ctx, factory));
            }
            return newArray;
        } catch (IllegalArgumentException e) {
            throw new CompileException("type mismatch in array", expr, start, e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
    } else {
        if (type.isArray()) {
            return MVEL.eval((String) o, ctx, factory, getBaseComponentType(type));
        } else {
            return MVEL.eval((String) o, ctx, factory);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with Type

use of org.mvel2.asm.Type in project mvel by mikebrock.

the class InlineCollectionNode method parseGraph.

private void parseGraph(boolean compile, Class type, ParserContext pCtx) {
    CollectionParser parser = new CollectionParser();
    if (type == null) {
        collectionGraph = ((List) parser.parseCollection(expr, start, offset, compile, pCtx)).get(0);
    } else {
        collectionGraph = ((List) parser.parseCollection(expr, start, offset, compile, type, pCtx)).get(0);
    }
    trailingStart = parser.getCursor() + 2;
    trailingOffset = offset - (trailingStart - start);
    if (this.egressType == null)
        this.egressType = collectionGraph.getClass();
}
Also used : CollectionParser(org.mvel2.util.CollectionParser)

Aggregations

MethodVisitor (org.mvel2.asm.MethodVisitor)19 Map (java.util.Map)15 Label (org.mvel2.asm.Label)13 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)12 List (java.util.List)11 Type (org.mvel2.asm.Type)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CompileException (org.mvel2.CompileException)7 ParserContext (org.mvel2.ParserContext)7 HashMap (java.util.HashMap)6 TypeDescriptor (org.mvel2.ast.TypeDescriptor)6 WeakHashMap (java.util.WeakHashMap)4 WorkingMemory (org.drools.core.WorkingMemory)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Tuple (org.drools.core.spi.Tuple)4 FieldVisitor (org.mvel2.asm.FieldVisitor)4 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)4 LeftTuple (org.drools.core.reteoo.LeftTuple)3 DeclarationMatcher (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher)3