Search in sources :

Example 11 with Accessor

use of org.mule.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method optimizeCollection.

public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.start = this.cursor = start;
    this.length = start + offset;
    this.returnType = type;
    this.ctx = ctx;
    this.variableFactory = factory;
    this.pCtx = pCtx;
    Accessor root = _getAccessor(o, returnType);
    if (property != null && length > start) {
        return new Union(root, property, cursor, offset);
    } else {
        return root;
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Example 12 with Accessor

use of org.mule.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method get.

public static Object get(String expression, Object ctx) {
    int hash = createSignatureHash(expression, ctx);
    Accessor accessor = REFLECTIVE_ACCESSOR_CACHE.get(hash);
    if (accessor != null) {
        return accessor.getValue(ctx, null, null);
    } else {
        REFLECTIVE_ACCESSOR_CACHE.put(hash, accessor = new ReflectiveAccessorOptimizer().optimizeAccessor(getCurrentThreadParserContext(), expression.toCharArray(), 0, expression.length(), ctx, null, null, false, null));
        return accessor.getValue(ctx, null, null);
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Example 13 with Accessor

use of org.mule.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method _getAccessor.

private Accessor _getAccessor(Object o, Class type) {
    if (o instanceof List) {
        Accessor[] a = new Accessor[((List) o).size()];
        int i = 0;
        for (Object item : (List) o) {
            a[i++] = _getAccessor(item, type);
        }
        returnType = List.class;
        return new ListCreator(a);
    } else if (o instanceof Map) {
        Accessor[] k = new Accessor[((Map) o).size()];
        Accessor[] v = new Accessor[k.length];
        int i = 0;
        for (Object item : ((Map) o).keySet()) {
            // key
            k[i] = _getAccessor(item, type);
            // value
            v[i++] = _getAccessor(((Map) o).get(item), type);
        }
        returnType = Map.class;
        return new MapCreator(k, v);
    } else if (o instanceof Object[]) {
        Accessor[] a = new Accessor[((Object[]) o).length];
        int i = 0;
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        try {
            Class base = getBaseComponentType(type);
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + base.getName() + ";", pCtx) : type;
            for (Object item : (Object[]) o) {
                expectType(a[i++] = _getAccessor(item, cls), base, true);
            }
            return new ArrayCreator(a, getSubComponentType(type));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
    } else {
        if (returnType == null)
            returnType = Object.class;
        if (type.isArray()) {
            return new ExprValueAccessor((String) o, type, ctx, variableFactory, pCtx);
        } else {
            return new ExprValueAccessor((String) o, Object.class, ctx, variableFactory, pCtx);
        }
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) List(java.util.List) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) MapCreator(org.mvel2.optimizers.impl.refl.collection.MapCreator)

Example 14 with Accessor

use of org.mule.mvel2.compiler.Accessor in project mvel by mvel.

the class ASMAccessorOptimizer method _getAccessor.

private int _getAccessor(Object o, Class type) {
    if (o instanceof List) {
        assert debug("NEW " + LIST_IMPL);
        mv.visitTypeInsn(NEW, LIST_IMPL);
        assert debug("DUP");
        mv.visitInsn(DUP);
        assert debug("DUP");
        mv.visitInsn(DUP);
        intPush(((List) o).size());
        assert debug("INVOKESPECIAL " + LIST_IMPL + ".<init>");
        mv.visitMethodInsn(INVOKESPECIAL, LIST_IMPL, "<init>", "(I)V");
        for (Object item : (List) o) {
            if (_getAccessor(item, type) != VAL) {
                assert debug("POP");
                mv.visitInsn(POP);
            }
            assert debug("INVOKEINTERFACE java/util/List.add");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            assert debug("POP");
            mv.visitInsn(POP);
            assert debug("DUP");
            mv.visitInsn(DUP);
        }
        returnType = List.class;
        return LIST;
    } else if (o instanceof Map) {
        assert debug("NEW " + MAP_IMPL);
        mv.visitTypeInsn(NEW, MAP_IMPL);
        assert debug("DUP");
        mv.visitInsn(DUP);
        assert debug("DUP");
        mv.visitInsn(DUP);
        intPush(((Map) o).size());
        assert debug("INVOKESPECIAL " + MAP_IMPL + ".<init>");
        mv.visitMethodInsn(INVOKESPECIAL, MAP_IMPL, "<init>", "(I)V");
        for (Object item : ((Map) o).keySet()) {
            mv.visitTypeInsn(CHECKCAST, "java/util/Map");
            if (_getAccessor(item, type) != VAL) {
                assert debug("POP");
                mv.visitInsn(POP);
            }
            if (_getAccessor(((Map) o).get(item), type) != VAL) {
                assert debug("POP");
                mv.visitInsn(POP);
            }
            assert debug("INVOKEINTERFACE java/util/Map.put");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
            assert debug("POP");
            mv.visitInsn(POP);
            assert debug("DUP");
            mv.visitInsn(DUP);
        }
        returnType = Map.class;
        return MAP;
    } else if (o instanceof Object[]) {
        Accessor[] a = new Accessor[((Object[]) o).length];
        int i = 0;
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        try {
            Class componentType = getSubComponentType(type);
            createArray(componentType, ((Object[]) o).length);
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + getBaseComponentType(type).getName() + ";", pCtx) : toNonPrimitiveArray(type);
            assert debug("DUP");
            mv.visitInsn(DUP);
            for (Object item : (Object[]) o) {
                intPush(i);
                if (_getAccessor(item, cls) != VAL) {
                    assert debug("POP");
                    mv.visitInsn(POP);
                }
                if (componentType.isPrimitive()) {
                    unwrapPrimitive(componentType);
                }
                arrayStore(componentType);
                assert debug("DUP");
                mv.visitInsn(DUP);
                i++;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
        return ARRAY;
    } else {
        if (type.isArray()) {
            writeLiteralOrSubexpression(subCompileExpression(((String) o).toCharArray(), pCtx), getSubComponentType(type));
        } else {
            writeLiteralOrSubexpression(subCompileExpression(((String) o).toCharArray(), pCtx));
        }
        return VAL;
    }
}
Also used : ArrayList(java.util.ArrayList) ParseTools.parseParameterList(org.mvel2.util.ParseTools.parseParameterList) List(java.util.List) ParseTools.findClass(org.mvel2.util.ParseTools.findClass) Map(java.util.Map) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Example 15 with Accessor

use of org.mule.mvel2.compiler.Accessor in project mvel by mvel.

the class ASMAccessorOptimizer method _initializeAccessor.

private Accessor _initializeAccessor() throws Exception {
    if (deferFinish) {
        return null;
    }
    /**
     * Hot load the class we just generated.
     */
    Class cls = loadClass(className, cw.toByteArray());
    assert debug("[MVEL JIT Completed Optimization <<" + (expr != null ? new String(expr) : "") + ">>]::" + cls + " (time: " + (System.currentTimeMillis() - time) + "ms)");
    Object o;
    try {
        if (compiledInputs.size() == 0) {
            o = cls.newInstance();
        } else {
            Class[] parms = new Class[compiledInputs.size()];
            for (int i = 0; i < compiledInputs.size(); i++) {
                parms[i] = ExecutableStatement.class;
            }
            o = cls.getConstructor(parms).newInstance(compiledInputs.toArray(new ExecutableStatement[compiledInputs.size()]));
        }
        if (propNull)
            cls.getField("nullPropertyHandler").set(o, getNullPropertyHandler());
        if (methNull)
            cls.getField("nullMethodHandler").set(o, getNullMethodHandler());
    } catch (VerifyError e) {
        System.out.println("**** COMPILER BUG! REPORT THIS IMMEDIATELY AT http://jira.codehaus.org/browse/MVEL");
        System.out.println("Expression: " + (expr == null ? null : new String(expr)));
        throw e;
    }
    return (Accessor) o;
}
Also used : ParseTools.findClass(org.mvel2.util.ParseTools.findClass) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Aggregations

Accessor (org.mvel2.compiler.Accessor)16 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)9 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)9 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)9 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)9 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)7 ArrayAccessor (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor)6 ConstructorAccessor (org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)6 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)6 GetterAccessor (org.mvel2.optimizers.impl.refl.nodes.GetterAccessor)6 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)6 ListAccessor (org.mvel2.optimizers.impl.refl.nodes.ListAccessor)6 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)6 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)6 StaticVarAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor)6 ThisValueAccessor (org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor)6 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)6 AccessorNode (org.mvel2.compiler.AccessorNode)4 List (java.util.List)3