Search in sources :

Example 1 with Type

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

the class ASMAccessorOptimizer method optimizeCollection.

public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.expr = property;
    this.cursor = this.start = start;
    this.end = start + offset;
    this.length = offset;
    this.returnType = type;
    this.compiledInputs = new ArrayList<ExecutableStatement>();
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    _initJIT();
    literal = true;
    _getAccessor(o, type);
    _finishJIT();
    try {
        Accessor compiledAccessor = _initializeAccessor();
        if (property != null && length > start) {
            return new Union(compiledAccessor, property, start, length);
        } else {
            return compiledAccessor;
        }
    } catch (Exception e) {
        throw new OptimizationFailure("could not optimize collection", e);
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Union(org.mvel2.optimizers.impl.refl.nodes.Union) IOException(java.io.IOException)

Example 2 with Type

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

the class ASMAccessorOptimizer method writeOutNullHandler.

private void writeOutNullHandler(Member member, int type) {
    assert debug("DUP");
    mv.visitInsn(DUP);
    Label j = new Label();
    assert debug("IFNONNULL : jump");
    mv.visitJumpInsn(IFNONNULL, j);
    assert debug("POP");
    mv.visitInsn(POP);
    assert debug("ALOAD 0");
    mv.visitVarInsn(ALOAD, 0);
    if (type == 0) {
        this.propNull = true;
        assert debug("GETFIELD 'nullPropertyHandler'");
        mv.visitFieldInsn(GETFIELD, className, "nullPropertyHandler", "L" + NAMESPACE + "integration/PropertyHandler;");
    } else {
        this.methNull = true;
        assert debug("GETFIELD 'nullMethodHandler'");
        mv.visitFieldInsn(GETFIELD, className, "nullMethodHandler", "L" + NAMESPACE + "integration/PropertyHandler;");
    }
    assert debug("LDC '" + member.getName() + "'");
    mv.visitLdcInsn(member.getName());
    assert debug("ALOAD 1");
    mv.visitVarInsn(ALOAD, 1);
    assert debug("ALOAD 3");
    mv.visitVarInsn(ALOAD, 3);
    assert debug("INVOKEINTERFACE PropertyHandler.getProperty");
    mv.visitMethodInsn(INVOKEINTERFACE, NAMESPACE + "integration/PropertyHandler", "getProperty", "(Ljava/lang/String;Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;");
    assert debug("LABEL:jump");
    mv.visitLabel(j);
}
Also used : Label(org.mvel2.asm.Label)

Example 3 with Type

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

the class ReflectiveAccessorOptimizer method getCollectionPropertyAO.

private Object getCollectionPropertyAO(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanPropertyAO(ctx, prop);
    }
    if (ctx == null)
        return null;
    int _start = ++cursor;
    skipWhitespace();
    if (cursor == end)
        throw new CompileException("unterminated '['", this.expr, this.start);
    String item;
    if (scanTo(']'))
        throw new CompileException("unterminated '['", this.expr, this.start);
    item = new String(expr, _start, cursor - _start);
    boolean itemSubExpr = true;
    Object idx = null;
    try {
        idx = parseInt(item);
        itemSubExpr = false;
    } catch (Exception e) {
    // not a number;
    }
    ExecutableStatement itemStmt = null;
    if (itemSubExpr) {
        idx = (itemStmt = (ExecutableStatement) subCompileExpression(item.toCharArray(), pCtx)).getValue(ctx, thisRef, variableFactory);
    }
    ++cursor;
    if (ctx instanceof Map) {
        if (hasPropertyHandler(Map.class)) {
            return propHandler(item, ctx, Map.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new MapAccessorNest(itemStmt, null));
            } else {
                addAccessorNode(new MapAccessor(parseInt(item)));
            }
            return ((Map) ctx).get(idx);
        }
    } else if (ctx instanceof List) {
        if (hasPropertyHandler(List.class)) {
            return propHandler(item, ctx, List.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new ListAccessorNest(itemStmt, null));
            } else {
                addAccessorNode(new ListAccessor(parseInt(item)));
            }
            return ((List) ctx).get((Integer) idx);
        }
    } else if (ctx.getClass().isArray()) {
        if (hasPropertyHandler(Array.class)) {
            return propHandler(item, ctx, Array.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new ArrayAccessorNest(itemStmt));
            } else {
                addAccessorNode(new ArrayAccessor(parseInt(item)));
            }
            return Array.get(ctx, (Integer) idx);
        }
    } else if (ctx instanceof CharSequence) {
        if (hasPropertyHandler(CharSequence.class)) {
            return propHandler(item, ctx, CharSequence.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new IndexedCharSeqAccessorNest(itemStmt));
            } else {
                addAccessorNode(new IndexedCharSeqAccessor(parseInt(item)));
            }
            return ((CharSequence) ctx).charAt((Integer) idx);
        }
    } else {
        TypeDescriptor tDescr = new TypeDescriptor(expr, this.start, end - this.start, 0);
        if (tDescr.isArray()) {
            Class cls = getClassReference((Class) ctx, tDescr, variableFactory, pCtx);
            rootNode = new StaticReferenceAccessor(cls);
            return cls;
        }
        throw new CompileException("illegal use of []: unknown type: " + ctx.getClass().getName(), this.expr, this.st);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) TypeDescriptor(org.mvel2.ast.TypeDescriptor) List(java.util.List) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 4 with Type

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

the class ForEachNode 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.eval(expression[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) 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.eval(sepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 5 with Type

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

the class DefaultLocalVariableResolverFactory method createVariable.

public VariableResolver createVariable(String name, Object value, Class<?> type) {
    if (indexedVariableNames == null)
        return super.createVariable(name, value, type);
    VariableResolver vr;
    boolean newVar = false;
    try {
        int idx;
        if ((idx = variableIndexOf(name)) != -1) {
            vr = new SimpleValueResolver(value);
            if (indexedVariableResolvers[idx] == null) {
                indexedVariableResolvers[idx] = vr;
            }
            variableResolvers.put(indexedVariableNames[idx], vr);
            vr = indexedVariableResolvers[idx];
            newVar = true;
        } else {
            return super.createVariable(name, value, type);
        }
    } catch (UnresolveablePropertyException e) {
        vr = null;
    }
    if (!newVar && vr != null && vr.getType() != null) {
        throw new RuntimeException("variable already defined within scope: " + vr.getType() + " " + name);
    } else {
        addResolver(name, vr = new MapVariableResolver(variables, name, type)).setValue(value);
        return vr;
    }
}
Also used : UnresolveablePropertyException(org.mvel2.UnresolveablePropertyException) VariableResolver(org.mvel2.integration.VariableResolver)

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