Search in sources :

Example 1 with NullSafe

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

the class ReflectiveAccessorOptimizer method compileGetChain.

private Accessor compileGetChain() {
    Object curr = ctx;
    cursor = start;
    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;
                    case DONE:
                        break;
                }
                first = false;
                if (curr != null)
                    returnType = curr.getClass();
                if (cursor < end) {
                    if (nullSafe) {
                        int os = expr[cursor] == '.' ? 1 : 0;
                        addAccessorNode(new NullSafe(expr, cursor + os, length - cursor - os, pCtx));
                        if (curr == null)
                            break;
                    }
                    if (curr == null)
                        throw new NullPointerException();
                }
                staticAccess = false;
            }
        } 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;
                    case DONE:
                        break;
                }
                first = false;
                if (curr != null)
                    returnType = curr.getClass();
                if (cursor < end) {
                    if (nullSafe) {
                        int os = expr[cursor] == '.' ? 1 : 0;
                        addAccessorNode(new NullSafe(expr, cursor + os, length - cursor - os, pCtx));
                        if (curr == null)
                            break;
                    }
                    if (curr == null)
                        throw new NullPointerException();
                }
                staticAccess = false;
            }
        }
        val = curr;
        return rootNode;
    } catch (InvocationTargetException e) {
        if (MVEL.INVOKED_METHOD_EXCEPTIONS_BUBBLE) {
            if (e.getTargetException() instanceof RuntimeException) {
                throw (RuntimeException) e.getTargetException();
            } else {
                throw new RuntimeException(e);
            }
        }
        throw new PropertyAccessException(new String(expr, start, length) + ": " + e.getTargetException().getMessage(), this.expr, this.st, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(new String(expr, start, length) + ": " + e.getMessage(), this.expr, this.st, e, pCtx);
    } catch (IndexOutOfBoundsException e) {
        throw new PropertyAccessException(new String(expr, start, length) + ": array index out of bounds.", this.expr, this.st, e, pCtx);
    } catch (CompileException e) {
        throw e;
    } catch (NullPointerException e) {
        throw new PropertyAccessException("null pointer: " + new String(expr, start, length), this.expr, this.st, e, pCtx);
    } catch (Exception e) {
        e.printStackTrace();
        throw new CompileException(e.getMessage(), this.expr, st, e);
    }
}
Also used : PropertyAccessException(org.mvel2.PropertyAccessException) CompileException(org.mvel2.CompileException) NullSafe(org.mvel2.optimizers.impl.refl.nodes.NullSafe) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException)

Example 2 with NullSafe

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

the class ASMAccessorOptimizer method getBeanProperty.

private Object getBeanProperty(Object ctx, String property) throws IllegalAccessException, InvocationTargetException {
    assert debug("\n  **  ENTER -> {bean: " + property + "; ctx=" + ctx + "}");
    if ((pCtx == null ? currType : pCtx.getVarOrInputTypeOrNull(property)) == Object.class && !pCtx.isStrongTyping()) {
        currType = null;
    }
    if (returnType != null && returnType.isPrimitive()) {
        // noinspection unchecked
        wrapPrimitive(returnType);
    }
    boolean classRef = false;
    Class<?> cls;
    if (ctx instanceof Class) {
        if (MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS && "class".equals(property)) {
            ldcClassConstant((Class<?>) ctx);
            return ctx;
        }
        cls = (Class<?>) ctx;
        classRef = true;
    } else if (ctx != null) {
        cls = ctx.getClass();
    } else {
        cls = null;
    }
    if (hasPropertyHandler(cls)) {
        PropertyHandler prop = getPropertyHandler(cls);
        if (prop instanceof ProducesBytecode) {
            ((ProducesBytecode) prop).produceBytecodeGet(mv, property, variableFactory);
            return prop.getProperty(property, ctx, variableFactory);
        } else {
            throw new RuntimeException("unable to compileShared: custom accessor does not support producing bytecode: " + prop.getClass().getName());
        }
    }
    Member member = cls != null ? getFieldOrAccessor(cls, property) : null;
    if (member != null && classRef && (member.getModifiers() & Modifier.STATIC) == 0) {
        member = null;
    }
    if (member != null && hasGetListeners()) {
        mv.visitVarInsn(ALOAD, 1);
        mv.visitLdcInsn(member.getName());
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESTATIC, NAMESPACE + "integration/GlobalListenerFactory", "notifyGetListeners", "(Ljava/lang/Object;Ljava/lang/String;L" + NAMESPACE + "integration/VariableResolverFactory;)V");
        notifyGetListeners(ctx, member.getName(), variableFactory);
    }
    if (first) {
        if ("this".equals(property)) {
            assert debug("ALOAD 2");
            mv.visitVarInsn(ALOAD, 2);
            return thisRef;
        } else if (variableFactory != null && variableFactory.isResolveable(property)) {
            if (variableFactory.isIndexedFactory() && variableFactory.isTarget(property)) {
                int idx;
                try {
                    loadVariableByIndex(idx = variableFactory.variableIndexOf(property));
                } catch (Exception e) {
                    throw new OptimizationFailure(property);
                }
                return variableFactory.getIndexedVariableResolver(idx).getValue();
            } else {
                try {
                    loadVariableByName(property);
                } catch (Exception e) {
                    throw new OptimizationFailure("critical error in JIT", e);
                }
                return variableFactory.getVariableResolver(property).getValue();
            }
        } else {
            assert debug("ALOAD 1");
            mv.visitVarInsn(ALOAD, 1);
        }
    }
    if (member instanceof Field) {
        return optimizeFieldMethodProperty(ctx, property, cls, member);
    } else if (member != null) {
        Object o;
        if (first) {
            assert debug("ALOAD 1 (B)");
            mv.visitVarInsn(ALOAD, 1);
        }
        try {
            o = ((Method) member).invoke(ctx, EMPTYARG);
            if (returnType != member.getDeclaringClass()) {
                assert debug("CHECKCAST " + getInternalName(member.getDeclaringClass()));
                mv.visitTypeInsn(CHECKCAST, getInternalName(member.getDeclaringClass()));
            }
            returnType = ((Method) member).getReturnType();
            assert debug("INVOKEVIRTUAL " + member.getName() + ":" + returnType);
            mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(member.getDeclaringClass()), member.getName(), getMethodDescriptor((Method) member));
        } catch (IllegalAccessException e) {
            Method iFaceMeth = determineActualTargetMethod((Method) member);
            if (iFaceMeth == null)
                throw new PropertyAccessException("could not access field: " + cls.getName() + "." + property, expr, st, e, pCtx);
            assert debug("CHECKCAST " + getInternalName(iFaceMeth.getDeclaringClass()));
            mv.visitTypeInsn(CHECKCAST, getInternalName(iFaceMeth.getDeclaringClass()));
            returnType = iFaceMeth.getReturnType();
            assert debug("INVOKEINTERFACE " + member.getName() + ":" + returnType);
            mv.visitMethodInsn(INVOKEINTERFACE, getInternalName(iFaceMeth.getDeclaringClass()), member.getName(), getMethodDescriptor((Method) member));
            o = iFaceMeth.invoke(ctx, EMPTYARG);
        } catch (IllegalArgumentException e) {
            if (member.getDeclaringClass().equals(ctx)) {
                try {
                    Class c = Class.forName(member.getDeclaringClass().getName() + "$" + property);
                    throw new CompileException("name collision between innerclass: " + c.getCanonicalName() + "; and bean accessor: " + property + " (" + member.toString() + ")", expr, tkStart);
                } catch (ClassNotFoundException e2) {
                // fallthru
                }
            }
            throw e;
        }
        if (hasNullPropertyHandler()) {
            if (o == null)
                o = getNullPropertyHandler().getProperty(member.getName(), ctx, variableFactory);
            writeOutNullHandler(member, 0);
        }
        currType = toNonPrimitiveType(returnType);
        return o;
    } else if (ctx instanceof Map && (((Map) ctx).containsKey(property) || nullSafe)) {
        assert debug("CHECKCAST java/util/Map");
        mv.visitTypeInsn(CHECKCAST, "java/util/Map");
        assert debug("LDC: \"" + property + "\"");
        mv.visitLdcInsn(property);
        assert debug("INVOKEINTERFACE: get");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
        return ((Map) ctx).get(property);
    } else if (first && "this".equals(property)) {
        assert debug("ALOAD 2");
        // load the thisRef value.
        mv.visitVarInsn(ALOAD, 2);
        return this.thisRef;
    } else if ("length".equals(property) && ctx.getClass().isArray()) {
        anyArrayCheck(ctx.getClass());
        assert debug("ARRAYLENGTH");
        mv.visitInsn(ARRAYLENGTH);
        wrapPrimitive(int.class);
        return getLength(ctx);
    } else if (LITERALS.containsKey(property)) {
        Object lit = LITERALS.get(property);
        if (lit instanceof Class) {
            ldcClassConstant((Class) lit);
        }
        return lit;
    } else {
        Object ts = tryStaticAccess();
        if (ts != null) {
            if (ts instanceof Class) {
                ldcClassConstant((Class) ts);
                return ts;
            } else if (ts instanceof Method) {
                writeFunctionPointerStub(((Method) ts).getDeclaringClass(), (Method) ts);
                return ts;
            } else {
                Field f = (Field) ts;
                return optimizeFieldMethodProperty(ctx, property, cls, f);
            }
        } else if (ctx instanceof Class) {
            /**
             * This is our ugly support for function pointers.  This works but needs to be re-thought out at some
             * point.
             */
            Class c = (Class) ctx;
            for (Method m : c.getMethods()) {
                if (property.equals(m.getName())) {
                    if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
                        assert debug("POP");
                        mv.visitInsn(POP);
                        assert debug("INVOKESTATIC " + m.getName());
                        mv.visitMethodInsn(INVOKESTATIC, getInternalName(m.getDeclaringClass()), m.getName(), getMethodDescriptor(m));
                        returnType = m.getReturnType();
                        return m.invoke(null, EMPTY_OBJ_ARR);
                    } else {
                        writeFunctionPointerStub(c, m);
                        return m;
                    }
                }
            }
            try {
                Class subClass = findClass(variableFactory, c.getName() + "$" + property, pCtx);
                ldcClassConstant(subClass);
                return subClass;
            } catch (ClassNotFoundException cnfe) {
            // fall through.
            }
        } else if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
            return getMethod(ctx, property);
        }
        if (ctx == null) {
            throw new PropertyAccessException("unresolvable property or identifier: " + property, expr, st, pCtx);
        } else {
            throw new PropertyAccessException("could not access: " + property + "; in class: " + ctx.getClass().getName(), expr, st, pCtx);
        }
    }
}
Also used : OptimizationFailure(org.mvel2.OptimizationFailure) PropertyAccessException(org.mvel2.PropertyAccessException) Method(java.lang.reflect.Method) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) Field(java.lang.reflect.Field) PropertyHandlerFactory.hasNullPropertyHandler(org.mvel2.integration.PropertyHandlerFactory.hasNullPropertyHandler) PropertyHandlerFactory.hasPropertyHandler(org.mvel2.integration.PropertyHandlerFactory.hasPropertyHandler) PropertyHandlerFactory.getPropertyHandler(org.mvel2.integration.PropertyHandlerFactory.getPropertyHandler) PropertyHandler(org.mvel2.integration.PropertyHandler) PropertyHandlerFactory.getNullPropertyHandler(org.mvel2.integration.PropertyHandlerFactory.getNullPropertyHandler) CompileException(org.mvel2.CompileException) Member(java.lang.reflect.Member) Map(java.util.Map)

Example 3 with NullSafe

use of org.mvel2.optimizers.impl.refl.nodes.NullSafe 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) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException)

Example 4 with NullSafe

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

the class ReflectiveAccessorOptimizer method getBeanProperty.

private Object getBeanProperty(Object ctx, String property) throws Exception {
    if ((pCtx == null ? currType : pCtx.getVarOrInputTypeOrNull(property)) == Object.class && !pCtx.isStrongTyping()) {
        currType = null;
    }
    if (first) {
        if ("this".equals(property)) {
            addAccessorNode(new ThisValueAccessor());
            return this.thisRef;
        } else if (variableFactory != null && variableFactory.isResolveable(property)) {
            if (variableFactory.isIndexedFactory() && variableFactory.isTarget(property)) {
                int idx;
                addAccessorNode(new IndexedVariableAccessor(idx = variableFactory.variableIndexOf(property)));
                VariableResolver vr = variableFactory.getIndexedVariableResolver(idx);
                if (vr == null) {
                    variableFactory.setIndexedVariableResolver(idx, variableFactory.getVariableResolver(property));
                }
                return variableFactory.getIndexedVariableResolver(idx).getValue();
            } else {
                addAccessorNode(new VariableAccessor(property));
                return variableFactory.getVariableResolver(property).getValue();
            }
        }
    }
    boolean classRef = false;
    Class<?> cls;
    if (ctx instanceof Class) {
        if (MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS && "class".equals(property)) {
            return ctx;
        }
        cls = (Class<?>) ctx;
        classRef = true;
    } else if (ctx != null) {
        cls = ctx.getClass();
    } else {
        cls = currType;
    }
    if (hasPropertyHandler(cls)) {
        PropertyHandlerAccessor acc = new PropertyHandlerAccessor(property, cls, getPropertyHandler(cls));
        addAccessorNode(acc);
        return acc.getValue(ctx, thisRef, variableFactory);
    }
    Member member = cls != null ? getFieldOrAccessor(cls, property) : null;
    if (member != null && classRef && (member.getModifiers() & Modifier.STATIC) == 0) {
        member = null;
    }
    Object o;
    if (member instanceof Method) {
        try {
            o = ctx != null ? ((Method) member).invoke(ctx, EMPTYARG) : null;
            if (hasNullPropertyHandler()) {
                addAccessorNode(new GetterAccessorNH((Method) member, getNullPropertyHandler()));
                if (o == null)
                    o = getNullPropertyHandler().getProperty(member.getName(), ctx, variableFactory);
            } else {
                addAccessorNode(new GetterAccessor((Method) member));
            }
        } catch (IllegalAccessException e) {
            Method iFaceMeth = determineActualTargetMethod((Method) member);
            if (iFaceMeth == null)
                throw new PropertyAccessException("could not access field: " + cls.getName() + "." + property, this.expr, this.start, pCtx);
            o = iFaceMeth.invoke(ctx, EMPTYARG);
            if (hasNullPropertyHandler()) {
                addAccessorNode(new GetterAccessorNH((Method) member, getNullMethodHandler()));
                if (o == null)
                    o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
            } else {
                addAccessorNode(new GetterAccessor(iFaceMeth));
            }
        } catch (IllegalArgumentException e) {
            if (member.getDeclaringClass().equals(ctx)) {
                try {
                    Class c = Class.forName(member.getDeclaringClass().getName() + "$" + property);
                    throw new CompileException("name collision between innerclass: " + c.getCanonicalName() + "; and bean accessor: " + property + " (" + member.toString() + ")", expr, tkStart);
                } catch (ClassNotFoundException e2) {
                // fallthru
                }
            }
            throw e;
        }
        currType = toNonPrimitiveType(((Method) member).getReturnType());
        return o;
    } else if (member != null) {
        Field f = (Field) member;
        if ((f.getModifiers() & Modifier.STATIC) != 0) {
            o = f.get(null);
            if (hasNullPropertyHandler()) {
                addAccessorNode(new StaticVarAccessorNH((Field) member, getNullMethodHandler()));
                if (o == null)
                    o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
            } else {
                addAccessorNode(new StaticVarAccessor((Field) member));
            }
        } else {
            o = ctx != null ? f.get(ctx) : null;
            if (hasNullPropertyHandler()) {
                addAccessorNode(new FieldAccessorNH((Field) member, getNullMethodHandler()));
                if (o == null)
                    o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
            } else {
                addAccessorNode(new FieldAccessor((Field) member));
            }
        }
        currType = toNonPrimitiveType(f.getType());
        return o;
    } else if (ctx instanceof Map && (((Map) ctx).containsKey(property) || nullSafe)) {
        addAccessorNode(new MapAccessor(property));
        return ((Map) ctx).get(property);
    } else if (ctx != null && "length".equals(property) && ctx.getClass().isArray()) {
        addAccessorNode(new ArrayLength());
        return getLength(ctx);
    } else if (LITERALS.containsKey(property)) {
        addAccessorNode(new StaticReferenceAccessor(ctx = LITERALS.get(property)));
        return ctx;
    } else {
        Object tryStaticMethodRef = tryStaticAccess();
        staticAccess = true;
        if (tryStaticMethodRef != null) {
            if (tryStaticMethodRef instanceof Class) {
                addAccessorNode(new StaticReferenceAccessor(tryStaticMethodRef));
                return tryStaticMethodRef;
            } else if (tryStaticMethodRef instanceof Field) {
                addAccessorNode(new StaticVarAccessor((Field) tryStaticMethodRef));
                return ((Field) tryStaticMethodRef).get(null);
            } else {
                addAccessorNode(new StaticReferenceAccessor(tryStaticMethodRef));
                return tryStaticMethodRef;
            }
        } else if (ctx instanceof Class) {
            Class c = (Class) ctx;
            for (Method m : c.getMethods()) {
                if (property.equals(m.getName())) {
                    if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
                        o = m.invoke(null, EMPTY_OBJ_ARR);
                        if (hasNullMethodHandler()) {
                            addAccessorNode(new MethodAccessorNH(m, new ExecutableStatement[0], getNullMethodHandler()));
                            if (o == null)
                                o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
                        } else {
                            addAccessorNode(new MethodAccessor(m, new ExecutableStatement[0]));
                        }
                        return o;
                    } else {
                        addAccessorNode(new StaticReferenceAccessor(m));
                        return m;
                    }
                }
            }
            try {
                Class subClass = findClass(variableFactory, c.getName() + "$" + property, pCtx);
                addAccessorNode(new StaticReferenceAccessor(subClass));
                return subClass;
            } catch (ClassNotFoundException cnfe) {
            // fall through.
            }
        } else if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
            return getMethod(ctx, property);
        }
        // if it is not already using this as context try to read the property value from this
        if (ctx != this.thisRef && this.thisRef != null) {
            addAccessorNode(new ThisValueAccessor());
            return getBeanProperty(this.thisRef, property);
        }
        if (ctx == null) {
            throw new PropertyAccessException("unresolvable property or identifier: " + property, expr, start, pCtx);
        } else {
            throw new PropertyAccessException("could not access: " + property + "; in class: " + ctx.getClass().getName(), expr, start, pCtx);
        }
    }
}
Also used : FieldAccessorNH(org.mvel2.optimizers.impl.refl.nodes.FieldAccessorNH) FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) DynamicFieldAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor) Field(java.lang.reflect.Field) GetterAccessorNH(org.mvel2.optimizers.impl.refl.nodes.GetterAccessorNH) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) StaticVarAccessorNH(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessorNH) CompileException(org.mvel2.CompileException) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) Member(java.lang.reflect.Member) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) PropertyAccessException(org.mvel2.PropertyAccessException) ArrayLength(org.mvel2.optimizers.impl.refl.nodes.ArrayLength) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) Method(java.lang.reflect.Method) PropertyHandlerAccessor(org.mvel2.optimizers.impl.refl.nodes.PropertyHandlerAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) MethodAccessorNH(org.mvel2.optimizers.impl.refl.nodes.MethodAccessorNH) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableResolver(org.mvel2.integration.VariableResolver) Map(java.util.Map)

Example 5 with NullSafe

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

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);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } catch (IndexOutOfBoundsException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } 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);
    } catch (OptimizationNotSupported e) {
        throw e;
    } catch (Exception e) {
        throw new CompileException(e.getMessage(), expr, st, e);
    }
}
Also used : Label(org.mvel2.asm.Label) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 CompileException (org.mvel2.CompileException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Map (java.util.Map)3 Field (java.lang.reflect.Field)2 Member (java.lang.reflect.Member)2 Method (java.lang.reflect.Method)2 Label (org.mvel2.asm.Label)2 PropertyHandler (org.mvel2.integration.PropertyHandler)2 OptimizationNotSupported (org.mvel2.optimizers.OptimizationNotSupported)2 OptimizationFailure (org.mvel2.OptimizationFailure)1 PropertyHandlerFactory.getNullPropertyHandler (org.mvel2.integration.PropertyHandlerFactory.getNullPropertyHandler)1 PropertyHandlerFactory.getPropertyHandler (org.mvel2.integration.PropertyHandlerFactory.getPropertyHandler)1 PropertyHandlerFactory.hasNullPropertyHandler (org.mvel2.integration.PropertyHandlerFactory.hasNullPropertyHandler)1 PropertyHandlerFactory.hasPropertyHandler (org.mvel2.integration.PropertyHandlerFactory.hasPropertyHandler)1 VariableResolver (org.mvel2.integration.VariableResolver)1 ArrayLength (org.mvel2.optimizers.impl.refl.nodes.ArrayLength)1 DynamicFieldAccessor (org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor)1 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)1