Search in sources :

Example 1 with StaticReferenceAccessor

use of org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor 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 2 with StaticReferenceAccessor

use of org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor in project mvel by mikebrock.

the class ASMAccessorOptimizer method getCollectionPropertyAO.

private Object getCollectionPropertyAO(Object ctx, String prop) throws IllegalAccessException, InvocationTargetException {
    if (prop.length() > 0) {
        ctx = getBeanProperty(ctx, prop);
        first = false;
    }
    if (ctx == null)
        return null;
    assert debug("\n  **  ENTER -> {collection:<<" + prop + ">>; ctx=" + ctx + "}");
    int _start = ++cursor;
    skipWhitespace();
    if (cursor == end)
        throw new CompileException("unterminated '['", expr, st);
    if (scanTo(']'))
        throw new CompileException("unterminated '['", expr, st);
    String tk = new String(expr, _start, cursor - _start);
    assert debug("{collection token:<<" + tk + ">>}");
    ExecutableStatement compiled = (ExecutableStatement) subCompileExpression(tk.toCharArray());
    Object item = compiled.getValue(ctx, variableFactory);
    ++cursor;
    if (ctx instanceof Map) {
        if (hasPropertyHandler(Map.class)) {
            return propHandlerByteCode(tk, ctx, Map.class);
        } else {
            if (first) {
                assert debug("ALOAD 1");
                mv.visitVarInsn(ALOAD, 1);
            }
            assert debug("CHECKCAST java/util/Map");
            mv.visitTypeInsn(CHECKCAST, "java/util/Map");
            Class c = writeLiteralOrSubexpression(compiled);
            if (c != null && c.isPrimitive()) {
                wrapPrimitive(c);
            }
            assert debug("INVOKEINTERFACE: Map.get");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
        }
        return ((Map) ctx).get(item);
    } else if (ctx instanceof List) {
        if (hasPropertyHandler(List.class)) {
            return propHandlerByteCode(tk, ctx, List.class);
        } else {
            if (first) {
                assert debug("ALOAD 1");
                mv.visitVarInsn(ALOAD, 1);
            }
            assert debug("CHECKCAST java/util/List");
            mv.visitTypeInsn(CHECKCAST, "java/util/List");
            writeLiteralOrSubexpression(compiled, int.class);
            assert debug("INVOKEINTERFACE: java/util/List.get");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
            return ((List) ctx).get(convert(item, Integer.class));
        }
    } else if (ctx.getClass().isArray()) {
        if (hasPropertyHandler(Array.class)) {
            return propHandlerByteCode(tk, ctx, Array.class);
        } else {
            if (first) {
                assert debug("ALOAD 1");
                mv.visitVarInsn(ALOAD, 1);
            }
            assert debug("CHECKCAST " + getDescriptor(ctx.getClass()));
            mv.visitTypeInsn(CHECKCAST, getDescriptor(ctx.getClass()));
            writeLiteralOrSubexpression(compiled, int.class, item.getClass());
            Class cls = getBaseComponentType(ctx.getClass());
            if (cls.isPrimitive()) {
                if (cls == int.class) {
                    assert debug("IALOAD");
                    mv.visitInsn(IALOAD);
                } else if (cls == char.class) {
                    assert debug("CALOAD");
                    mv.visitInsn(CALOAD);
                } else if (cls == boolean.class) {
                    assert debug("BALOAD");
                    mv.visitInsn(BALOAD);
                } else if (cls == double.class) {
                    assert debug("DALOAD");
                    mv.visitInsn(DALOAD);
                } else if (cls == float.class) {
                    assert debug("FALOAD");
                    mv.visitInsn(FALOAD);
                } else if (cls == short.class) {
                    assert debug("SALOAD");
                    mv.visitInsn(SALOAD);
                } else if (cls == long.class) {
                    assert debug("LALOAD");
                    mv.visitInsn(LALOAD);
                } else if (cls == byte.class) {
                    assert debug("BALOAD");
                    mv.visitInsn(BALOAD);
                }
                wrapPrimitive(cls);
            } else {
                assert debug("AALOAD");
                mv.visitInsn(AALOAD);
            }
            return Array.get(ctx, convert(item, Integer.class));
        }
    } else if (ctx instanceof CharSequence) {
        if (hasPropertyHandler(CharSequence.class)) {
            return propHandlerByteCode(tk, ctx, CharSequence.class);
        } else {
            if (first) {
                assert debug("ALOAD 1");
                mv.visitVarInsn(ALOAD, 1);
            }
            assert debug("CHECKCAST java/lang/CharSequence");
            mv.visitTypeInsn(CHECKCAST, "java/lang/CharSequence");
            if (item instanceof Integer) {
                intPush((Integer) item);
                assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
                mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
                wrapPrimitive(char.class);
                return ((CharSequence) ctx).charAt((Integer) item);
            } else {
                writeLiteralOrSubexpression(compiled, Integer.class);
                unwrapPrimitive(int.class);
                assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
                mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
                wrapPrimitive(char.class);
                return ((CharSequence) ctx).charAt(convert(item, Integer.class));
            }
        }
    } else {
        TypeDescriptor tDescr = new TypeDescriptor(expr, start, end - start, 0);
        if (tDescr.isArray()) {
            try {
                Class cls = getClassReference((Class) ctx, tDescr, variableFactory, pCtx);
                // rootNode = new StaticReferenceAccessor(cls);
                ldcClassConstant(cls);
                return cls;
            } catch (Exception e) {
            // fall through
            }
        }
        throw new CompileException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), expr, st);
    }
}
Also used : TypeDescriptor(org.mvel2.ast.TypeDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) IOException(java.io.IOException)

Example 3 with StaticReferenceAccessor

use of org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method getCollectionProperty.

/**
 * Handle accessing a property embedded in a collections, map, or array
 *
 * @param ctx  -
 * @param prop -
 * @return -
 * @throws Exception -
 */
private Object getCollectionProperty(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanProperty(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) {
        try {
            idx = (itemStmt = (ExecutableStatement) subCompileExpression(item.toCharArray(), pCtx)).getValue(ctx, thisRef, variableFactory);
        } catch (CompileException e) {
            e.setExpr(this.expr);
            e.setCursor(start);
            throw e;
        }
    }
    ++cursor;
    if (ctx instanceof Map) {
        if (itemSubExpr) {
            addAccessorNode(new MapAccessorNest(itemStmt, null));
        } else {
            addAccessorNode(new MapAccessor(parseInt(item)));
        }
        return ((Map) ctx).get(idx);
    } else if (ctx instanceof List) {
        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 (itemSubExpr) {
            addAccessorNode(new ArrayAccessorNest(itemStmt));
        } else {
            addAccessorNode(new ArrayAccessor(parseInt(item)));
        }
        return Array.get(ctx, (Integer) idx);
    } else if (ctx instanceof CharSequence) {
        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, length, 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.start);
    }
}
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 StaticReferenceAccessor

use of org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNodeAccessor.

private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
    AccessorNode accessorNode;
    if (accessor instanceof DynamicGetAccessor) {
        accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
    } else if (accessor instanceof AccessorNode) {
        accessorNode = (AccessorNode) accessor;
    } else if (accessor instanceof CompiledExpression) {
        return analyzeNode(((CompiledExpression) accessor).getFirstNode());
    } else if (accessor instanceof ListCreator) {
        return analyzeListCreation(((ListCreator) accessor));
    } else if (accessor instanceof ArrayCreator) {
        return analyzeArrayCreation(((ArrayCreator) accessor));
    } else {
        throw new RuntimeException("Unknown accessor type: " + accessor);
    }
    if (accessorNode instanceof VariableAccessor) {
        if (isStaticAccessor(accessorNode)) {
            while (accessorNode instanceof VariableAccessor) {
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            return analyzeNodeAccessor(accessorNode, node);
        }
    }
    while (accessorNode instanceof StaticReferenceAccessor) {
        StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
        Object literal = staticReferenceAccessor.getLiteral();
        accessorNode = accessorNode.getNextNode();
        if (accessorNode == null) {
            return new FixedExpression(literal.getClass(), literal);
        }
    }
    return analyzeExpressionNode(accessorNode, node, null);
}
Also used : AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) CompiledExpression(org.mvel2.compiler.CompiledExpression) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)

Example 5 with StaticReferenceAccessor

use of org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor in project mvel by mikebrock.

the class ASMAccessorOptimizer method getCollectionProperty.

private Object getCollectionProperty(Object ctx, String prop) throws IllegalAccessException, InvocationTargetException {
    if (prop.trim().length() > 0) {
        ctx = getBeanProperty(ctx, prop);
        first = false;
    }
    if (ctx == null)
        return null;
    assert debug("\n  **  ENTER -> {collection:<<" + prop + ">>; ctx=" + ctx + "}");
    if (first) {
        assert debug("ALOAD 1");
        mv.visitVarInsn(ALOAD, 1);
    }
    int start = ++cursor;
    skipWhitespace();
    if (cursor == end)
        throw new CompileException("unterminated '['", expr, st);
    if (scanTo(']'))
        throw new CompileException("unterminated '['", expr, st);
    String tk = new String(expr, start, cursor - start);
    assert debug("{collection token: [" + tk + "]}");
    ExecutableStatement compiled = (ExecutableStatement) subCompileExpression(tk.toCharArray(), pCtx);
    Object item = compiled.getValue(ctx, variableFactory);
    ++cursor;
    if (ctx instanceof Map) {
        assert debug("CHECKCAST java/util/Map");
        mv.visitTypeInsn(CHECKCAST, "java/util/Map");
        Class c = writeLiteralOrSubexpression(compiled);
        if (c != null && c.isPrimitive()) {
            wrapPrimitive(c);
        }
        assert debug("INVOKEINTERFACE: get");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
        return ((Map) ctx).get(item);
    } else if (ctx instanceof List) {
        assert debug("CHECKCAST java/util/List");
        mv.visitTypeInsn(CHECKCAST, "java/util/List");
        writeLiteralOrSubexpression(compiled, int.class);
        assert debug("INVOKEINTERFACE: java/util/List.get");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
        return ((List) ctx).get(convert(item, Integer.class));
    } else if (ctx.getClass().isArray()) {
        assert debug("CHECKCAST " + getDescriptor(ctx.getClass()));
        mv.visitTypeInsn(CHECKCAST, getDescriptor(ctx.getClass()));
        writeLiteralOrSubexpression(compiled, int.class, item.getClass());
        Class cls = getBaseComponentType(ctx.getClass());
        if (cls.isPrimitive()) {
            if (cls == int.class) {
                assert debug("IALOAD");
                mv.visitInsn(IALOAD);
            } else if (cls == char.class) {
                assert debug("CALOAD");
                mv.visitInsn(CALOAD);
            } else if (cls == boolean.class) {
                assert debug("BALOAD");
                mv.visitInsn(BALOAD);
            } else if (cls == double.class) {
                assert debug("DALOAD");
                mv.visitInsn(DALOAD);
            } else if (cls == float.class) {
                assert debug("FALOAD");
                mv.visitInsn(FALOAD);
            } else if (cls == short.class) {
                assert debug("SALOAD");
                mv.visitInsn(SALOAD);
            } else if (cls == long.class) {
                assert debug("LALOAD");
                mv.visitInsn(LALOAD);
            } else if (cls == byte.class) {
                assert debug("BALOAD");
                mv.visitInsn(BALOAD);
            }
            wrapPrimitive(cls);
        } else {
            assert debug("AALOAD");
            mv.visitInsn(AALOAD);
        }
        return Array.get(ctx, convert(item, Integer.class));
    } else if (ctx instanceof CharSequence) {
        assert debug("CHECKCAST java/lang/CharSequence");
        mv.visitTypeInsn(CHECKCAST, "java/lang/CharSequence");
        if (item instanceof Integer) {
            intPush((Integer) item);
            assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
            wrapPrimitive(char.class);
            return ((CharSequence) ctx).charAt((Integer) item);
        } else {
            writeLiteralOrSubexpression(compiled, Integer.class);
            unwrapPrimitive(int.class);
            assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
            wrapPrimitive(char.class);
            return ((CharSequence) ctx).charAt(convert(item, Integer.class));
        }
    } else {
        TypeDescriptor tDescr = new TypeDescriptor(expr, this.start, length, 0);
        if (tDescr.isArray()) {
            try {
                Class cls = getClassReference((Class) ctx, tDescr, variableFactory, pCtx);
                // rootNode = new StaticReferenceAccessor(cls);
                ldcClassConstant(cls);
                return cls;
            } catch (Exception e) {
            // fall through
            }
        }
        throw new CompileException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), expr, st);
    }
}
Also used : TypeDescriptor(org.mvel2.ast.TypeDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) IOException(java.io.IOException)

Aggregations

List (java.util.List)4 Map (java.util.Map)4 TypeDescriptor (org.mvel2.ast.TypeDescriptor)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 WeakHashMap (java.util.WeakHashMap)2 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)2 AccessorNode (org.mvel2.compiler.AccessorNode)1 CompiledExpression (org.mvel2.compiler.CompiledExpression)1 DynamicGetAccessor (org.mvel2.optimizers.dynamic.DynamicGetAccessor)1 ArrayCreator (org.mvel2.optimizers.impl.refl.collection.ArrayCreator)1 ListCreator (org.mvel2.optimizers.impl.refl.collection.ListCreator)1 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)1 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)1 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)1