Search in sources :

Example 36 with Accessor

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

the class InlineCollectionNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (accessor != null) {
        return accessor.getValue(ctx, thisValue, factory);
    } else {
        try {
            AccessorOptimizer ao = OptimizerFactory.getThreadAccessorOptimizer();
            if (collectionGraph == null)
                parseGraph(true, null, null);
            accessor = ao.optimizeCollection(pCtx, collectionGraph, egressType, expr, trailingStart, trailingOffset, ctx, thisValue, factory);
            egressType = ao.getEgressType();
            return accessor.getValue(ctx, thisValue, factory);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer)

Example 37 with Accessor

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

the class Union method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (accessor != null) {
        return accessor.getValue(main.getReducedValueAccelerated(ctx, thisValue, factory), thisValue, factory);
    } else {
        try {
            AccessorOptimizer o = OptimizerFactory.getThreadAccessorOptimizer();
            accessor = o.optimizeAccessor(pCtx, expr, start, offset, main.getReducedValueAccelerated(ctx, thisValue, factory), thisValue, factory, false, main.getEgressType());
            return o.getResultOptPass();
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer)

Example 38 with Accessor

use of org.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 39 with Accessor

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

the class ASMAccessorOptimizer method compileAccessor.

private Accessor compileAccessor() {
    assert debug("<<INITIATE COMPILE>>");
    Object curr = ctx;
    try {
        if (!MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING) {
            while (cursor < end) {
                switch(nextSubToken()) {
                    case BEAN:
                        curr = getBeanProperty(curr, capture());
                        break;
                    case METH:
                        curr = getMethod(curr, capture());
                        break;
                    case COL:
                        curr = getCollectionProperty(curr, capture());
                        break;
                    case WITH:
                        curr = getWithProperty(curr);
                        break;
                }
                // check to see if a null safety is enabled on this property.
                if (fields == -1) {
                    if (curr == null) {
                        if (nullSafe) {
                            throw new OptimizationNotSupported();
                        }
                        break;
                    } else {
                        fields = 0;
                    }
                }
                first = false;
                if (nullSafe && cursor < end) {
                    assert debug("DUP");
                    mv.visitInsn(DUP);
                    Label j = new Label();
                    assert debug("IFNONNULL : jump");
                    mv.visitJumpInsn(IFNONNULL, j);
                    assert debug("ARETURN");
                    mv.visitInsn(ARETURN);
                    assert debug("LABEL:jump");
                    mv.visitLabel(j);
                }
            }
        } else {
            while (cursor < end) {
                switch(nextSubToken()) {
                    case BEAN:
                        curr = getBeanPropertyAO(curr, capture());
                        break;
                    case METH:
                        curr = getMethod(curr, capture());
                        break;
                    case COL:
                        curr = getCollectionPropertyAO(curr, capture());
                        break;
                    case WITH:
                        curr = getWithProperty(curr);
                        break;
                }
                // check to see if a null safety is enabled on this property.
                if (fields == -1) {
                    if (curr == null) {
                        if (nullSafe) {
                            throw new OptimizationNotSupported();
                        }
                        break;
                    } else {
                        fields = 0;
                    }
                }
                first = false;
                if (nullSafe && cursor < end) {
                    assert debug("DUP");
                    mv.visitInsn(DUP);
                    Label j = new Label();
                    assert debug("IFNONNULL : jump");
                    mv.visitJumpInsn(IFNONNULL, j);
                    assert debug("ARETURN");
                    mv.visitInsn(ARETURN);
                    assert debug("LABEL:jump");
                    mv.visitLabel(j);
                }
            }
        }
        val = curr;
        _finishJIT();
        return _initializeAccessor();
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e, pCtx);
    } catch (IndexOutOfBoundsException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e, pCtx);
    } catch (PropertyAccessException e) {
        throw new CompileException(e.getMessage(), expr, st, e);
    } catch (CompileException e) {
        throw e;
    } catch (NullPointerException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e, pCtx);
    } catch (OptimizationNotSupported e) {
        throw e;
    } catch (Exception e) {
        throw new CompileException(e.getMessage(), expr, st, e);
    }
}
Also used : Label(org.mvel2.asm.Label) PropertyAccessException(org.mvel2.PropertyAccessException) CompileException(org.mvel2.CompileException) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) CompileException(org.mvel2.CompileException)

Example 40 with Accessor

use of org.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 CompileException (org.mvel2.CompileException)11 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)11 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)11 IOException (java.io.IOException)10 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)10 Map (java.util.Map)9 PropertyAccessException (org.mvel2.PropertyAccessException)9 AccessorNode (org.mvel2.compiler.AccessorNode)9 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)9 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)9 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)9 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)9 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)8 AccessorOptimizer (org.mvel2.optimizers.AccessorOptimizer)8 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)8 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)8 List (java.util.List)7