Search in sources :

Example 6 with PropertyAccessException

use of org.mule.mvel2.PropertyAccessException in project mvel by mvel.

the class ASMAccessorOptimizer method optimizeSetAccessor.

public Accessor optimizeSetAccessor(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory, boolean rootThisRef, Object value, Class ingressType) {
    this.expr = property;
    this.start = this.cursor = start;
    this.end = start + offset;
    this.length = start + offset;
    this.first = true;
    this.ingressType = ingressType;
    compiledInputs = new ArrayList<ExecutableStatement>();
    this.pCtx = pCtx;
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    char[] root = null;
    PropertyVerifier verifier = new PropertyVerifier(property, this.pCtx = pCtx);
    int split = findLastUnion();
    if (split != -1) {
        root = subset(property, 0, split);
    }
    AccessorNode rootAccessor = null;
    _initJIT2();
    if (root != null) {
        int _length = this.length;
        int _end = this.end;
        char[] _expr = this.expr;
        this.length = end = (this.expr = root).length;
        // run the compiler but don't finish building.
        deferFinish = true;
        noinit = true;
        compileAccessor();
        ctx = this.val;
        this.expr = _expr;
        this.cursor = start + root.length + 1;
        this.length = _length - root.length - 1;
        this.end = this.cursor + this.length;
    } else {
        assert debug("ALOAD 1");
        mv.visitVarInsn(ALOAD, 1);
    }
    try {
        skipWhitespace();
        if (collection) {
            int st = cursor;
            whiteSpaceSkip();
            if (st == end)
                throw new PropertyAccessException("unterminated '['", expr, start, pCtx);
            if (scanTo(']'))
                throw new PropertyAccessException("unterminated '['", expr, start, pCtx);
            String ex = new String(expr, st, cursor - st).trim();
            assert debug("CHECKCAST " + ctx.getClass().getName());
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            if (ctx instanceof Map) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Map.class)) {
                    propHandlerByteCodePut(ex, ctx, Map.class, value);
                } else {
                    // noinspection unchecked
                    ((Map) ctx).put(eval(ex, ctx, variableFactory), convert(value, returnType = verifier.analyze()));
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (value != null && returnType != value.getClass()) {
                        dataConversion(returnType);
                        checkcast(returnType);
                    }
                    assert debug("INVOKEINTERFACE 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("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else if (ctx instanceof List) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(List.class)) {
                    propHandlerByteCodePut(ex, ctx, List.class, value);
                } else {
                    // noinspection unchecked
                    ((List) ctx).set(eval(ex, ctx, variableFactory, Integer.class), convert(value, returnType = verifier.analyze()));
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
                    unwrapPrimitive(int.class);
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (value != null && !value.getClass().isAssignableFrom(returnType)) {
                        dataConversion(returnType);
                        checkcast(returnType);
                    }
                    assert debug("INVOKEINTERFACE List.set");
                    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
                propHandlerByteCodePut(ex, ctx, ctx.getClass(), value);
            } else if (ctx.getClass().isArray()) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Array.class)) {
                    propHandlerByteCodePut(ex, ctx, Array.class, value);
                } else {
                    Class type = getBaseComponentType(ctx.getClass());
                    Object idx = eval(ex, ctx, variableFactory);
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx), int.class);
                    if (!(idx instanceof Integer)) {
                        dataConversion(Integer.class);
                        idx = DataConversion.convert(idx, Integer.class);
                        unwrapPrimitive(int.class);
                    }
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (type.isPrimitive())
                        unwrapPrimitive(type);
                    else if (!type.equals(value.getClass())) {
                        dataConversion(type);
                    }
                    arrayStore(type);
                    // noinspection unchecked
                    Array.set(ctx, (Integer) idx, convert(value, type));
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else {
                throw new PropertyAccessException("cannot bind to collection property: " + new String(expr) + ": not a recognized collection type: " + ctx.getClass(), expr, start, pCtx);
            }
            deferFinish = false;
            noinit = false;
            _finishJIT();
            try {
                deferFinish = false;
                return _initializeAccessor();
            } catch (Exception e) {
                throw new CompileException("could not generate accessor", expr, start, e);
            }
        }
        String tk = new String(expr, this.cursor, this.length);
        Member member = getFieldOrWriteAccessor(ctx.getClass(), tk, value == null ? null : ingressType);
        if (GlobalListenerFactory.hasSetListeners()) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitLdcInsn(tk);
            mv.visitVarInsn(ALOAD, 3);
            mv.visitVarInsn(ALOAD, 4);
            mv.visitMethodInsn(INVOKESTATIC, NAMESPACE + "integration/GlobalListenerFactory", "notifySetListeners", "(Ljava/lang/Object;Ljava/lang/String;L" + NAMESPACE + "integration/VariableResolverFactory;Ljava/lang/Object;)V");
            GlobalListenerFactory.notifySetListeners(ctx, tk, variableFactory, value);
        }
        if (member instanceof Field) {
            checkcast(ctx.getClass());
            Field fld = (Field) member;
            Label jmp = null;
            Label jmp2 = new Label();
            if (fld.getType().isPrimitive()) {
                assert debug("ASTORE 5");
                mv.visitVarInsn(ASTORE, 5);
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                if (value == null)
                    value = PropertyTools.getPrimitiveInitialValue(fld.getType());
                jmp = new Label();
                assert debug("IFNOTNULL jmp");
                mv.visitJumpInsn(IFNONNULL, jmp);
                assert debug("ALOAD 5");
                mv.visitVarInsn(ALOAD, 5);
                assert debug("ICONST_0");
                mv.visitInsn(ICONST_0);
                assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
                mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
                assert debug("GOTO jmp2");
                mv.visitJumpInsn(GOTO, jmp2);
                assert debug("jmp:");
                mv.visitLabel(jmp);
                assert debug("ALOAD 5");
                mv.visitVarInsn(ALOAD, 5);
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                unwrapPrimitive(fld.getType());
            } else {
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                checkcast(fld.getType());
            }
            if (jmp == null && value != null && !fld.getType().isAssignableFrom(value.getClass())) {
                if (!canConvert(fld.getType(), value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + fld.getType(), expr, start);
                }
                dataConversion(fld.getType());
                fld.set(ctx, convert(value, fld.getType()));
            } else {
                fld.set(ctx, value);
            }
            assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
            mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
            assert debug("jmp2:");
            mv.visitLabel(jmp2);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
        } else if (member != null) {
            assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            Method meth = (Method) member;
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            Class targetType = meth.getParameterTypes()[0];
            Label jmp;
            Label jmp2 = new Label();
            if (value != null && !targetType.isAssignableFrom(value.getClass())) {
                if (!canConvert(targetType, value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + meth.getParameterTypes()[0], expr, start);
                }
                dataConversion(getWrapperClass(targetType));
                if (targetType.isPrimitive()) {
                    unwrapPrimitive(targetType);
                } else
                    checkcast(targetType);
                meth.invoke(ctx, convert(value, meth.getParameterTypes()[0]));
            } else {
                if (targetType.isPrimitive()) {
                    if (value == null)
                        value = PropertyTools.getPrimitiveInitialValue(targetType);
                    jmp = new Label();
                    assert debug("IFNOTNULL jmp");
                    mv.visitJumpInsn(IFNONNULL, jmp);
                    assert debug("ICONST_0");
                    mv.visitInsn(ICONST_0);
                    assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
                    mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
                    assert debug("GOTO jmp2");
                    mv.visitJumpInsn(GOTO, jmp2);
                    assert debug("jmp:");
                    mv.visitLabel(jmp);
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    unwrapPrimitive(targetType);
                } else {
                    checkcast(targetType);
                }
                meth.invoke(ctx, value);
            }
            assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
            mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
            assert debug("jmp2:");
            mv.visitLabel(jmp2);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
        } else if (ctx instanceof Map) {
            assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            assert debug("LDC '" + tk + "'");
            mv.visitLdcInsn(tk);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            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("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            // noinspection unchecked
            ((Map) ctx).put(tk, value);
        } else {
            throw new PropertyAccessException("could not access property (" + tk + ") in: " + ingressType.getName(), expr, start, pCtx);
        }
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException("could not access property", expr, start, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException("could not access property", expr, start, e, pCtx);
    }
    try {
        deferFinish = false;
        noinit = false;
        _finishJIT();
        return _initializeAccessor();
    } catch (Exception e) {
        throw new CompileException("could not generate accessor", expr, start, e);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) PropertyVerifier(org.mvel2.compiler.PropertyVerifier) Label(org.mvel2.asm.Label) 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) InvocationTargetException(java.lang.reflect.InvocationTargetException) Array(java.lang.reflect.Array) ReflectionUtil.toNonPrimitiveArray(org.mvel2.util.ReflectionUtil.toNonPrimitiveArray) Field(java.lang.reflect.Field) AccessorNode(org.mvel2.compiler.AccessorNode) CompileException(org.mvel2.CompileException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Member(java.lang.reflect.Member)

Example 7 with PropertyAccessException

use of org.mule.mvel2.PropertyAccessException 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 8 with PropertyAccessException

use of org.mule.mvel2.PropertyAccessException in project mvel by mvel.

the class ReflectiveAccessorOptimizer method getMethod.

@SuppressWarnings({ "unchecked" })
private Object getMethod(Object ctx, String name, Object[] args, Class[] argTypes, ExecutableStatement[] es) throws Exception {
    if (first && variableFactory != null && variableFactory.isResolveable(name)) {
        Object ptr = variableFactory.getVariableResolver(name).getValue();
        if (ptr instanceof Method) {
            ctx = ((Method) ptr).getDeclaringClass();
            name = ((Method) ptr).getName();
        } else if (ptr instanceof MethodStub) {
            ctx = ((MethodStub) ptr).getClassReference();
            name = ((MethodStub) ptr).getMethodName();
        } else if (ptr instanceof FunctionInstance) {
            FunctionInstance func = (FunctionInstance) ptr;
            if (!name.equals(func.getFunction().getName())) {
                getBeanProperty(ctx, name);
                addAccessorNode(new DynamicFunctionAccessor(es));
            } else {
                addAccessorNode(new FunctionAccessor(func, es));
            }
            return func.call(ctx, thisRef, variableFactory, args);
        } else {
            throw new OptimizationFailure("attempt to optimize a method call for a reference that does not point to a method: " + name + " (reference is type: " + (ctx != null ? ctx.getClass().getName() : null) + ")");
        }
        first = false;
    }
    if (ctx == null && currType == null) {
        throw new PropertyAccessException("null pointer or function not found: " + name, this.expr, this.start, pCtx);
    }
    boolean classTarget = false;
    Class<?> cls = currType != null ? currType : ((classTarget = ctx instanceof Class) ? (Class<?>) ctx : ctx.getClass());
    currType = null;
    Method m;
    Class[] parameterTypes = null;
    /**
     * Try to find an instance method from the class target.
     */
    if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != null) {
        parameterTypes = m.getParameterTypes();
    }
    if (m == null && classTarget) {
        /**
         * If we didn't find anything, maybe we're looking for the actual java.lang.Class methods.
         */
        if ((m = getBestCandidate(argTypes, name, cls, Class.class.getMethods(), false)) != null) {
            parameterTypes = m.getParameterTypes();
        }
    }
    // If we didn't find anything and the declared class is different from the actual one try also with the actual one
    if (m == null && ctx != null && cls != ctx.getClass() && !(ctx instanceof Class)) {
        cls = ctx.getClass();
        if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != null) {
            parameterTypes = m.getParameterTypes();
        }
    }
    if (m == null) {
        StringAppender errorBuild = new StringAppender();
        if ("size".equals(name) && args.length == 0 && cls.isArray()) {
            addAccessorNode(new ArrayLength());
            return getLength(ctx);
        }
        // if it is not already using this as context try to access the method this
        if (ctx != this.thisRef && this.thisRef != null) {
            addAccessorNode(new ThisValueAccessor());
            return getMethod(this.thisRef, name, args, argTypes, es);
        }
        for (int i = 0; i < args.length; i++) {
            errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
            if (i < args.length - 1)
                errorBuild.append(", ");
        }
        throw new PropertyAccessException("unable to resolve method: " + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]", this.expr, this.st, pCtx);
    }
    if (es != null) {
        ExecutableStatement cExpr;
        for (int i = 0; i < es.length; i++) {
            cExpr = es[i];
            if (cExpr.getKnownIngressType() == null) {
                cExpr.setKnownIngressType(paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
                cExpr.computeTypeConversionRule();
            }
            if (!cExpr.isConvertableIngressEgress()) {
                args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
            }
        }
    } else {
        /**
         * Coerce any types if required.
         */
        for (int i = 0; i < args.length; i++) args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
    }
    Method method = getWidenedTarget(cls, m);
    Object o = ctx != null ? method.invoke(ctx, normalizeArgsForVarArgs(parameterTypes, args, m.isVarArgs())) : null;
    if (hasNullMethodHandler()) {
        addAccessorNode(new MethodAccessorNH(method, (ExecutableStatement[]) es, getNullMethodHandler()));
        if (o == null)
            o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
    } else {
        addAccessorNode(new MethodAccessor(method, (ExecutableStatement[]) es));
    }
    /**
     * return the response.
     */
    currType = toNonPrimitiveType(method.getReturnType());
    return o;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) FunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.FunctionAccessor) OptimizationFailure(org.mvel2.OptimizationFailure) PropertyAccessException(org.mvel2.PropertyAccessException) ArrayLength(org.mvel2.optimizers.impl.refl.nodes.ArrayLength) Method(java.lang.reflect.Method) MethodAccessorNH(org.mvel2.optimizers.impl.refl.nodes.MethodAccessorNH) FunctionInstance(org.mvel2.ast.FunctionInstance) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) MethodStub(org.mvel2.util.MethodStub) StringAppender(org.mvel2.util.StringAppender)

Example 9 with PropertyAccessException

use of org.mule.mvel2.PropertyAccessException 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 10 with PropertyAccessException

use of org.mule.mvel2.PropertyAccessException in project mvel by mvel.

the class ReflectiveAccessorOptimizer method optimizeSetAccessor.

public Accessor optimizeSetAccessor(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory, boolean rootThisRef, Object value, Class ingressType) {
    this.rootNode = this.currNode = null;
    this.expr = property;
    this.start = start;
    this.first = true;
    this.length = start + offset;
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    this.ingressType = ingressType;
    char[] root = null;
    int split = findLastUnion();
    PropertyVerifier verifier = new PropertyVerifier(property, this.pCtx = pCtx);
    if (split != -1) {
        root = subset(property, 0, split++);
        // todo: must use the property verifier.
        property = subset(property, split, property.length - split);
    }
    if (root != null) {
        this.length = end = (this.expr = root).length;
        compileGetChain();
        ctx = this.val;
    }
    if (ctx == null) {
        throw new PropertyAccessException("could not access property: " + new String(property, this.start, Math.min(length, property.length)) + "; parent is null: " + new String(expr), expr, this.start, pCtx);
    }
    try {
        this.length = end = (this.expr = property).length;
        int st;
        this.cursor = st = 0;
        skipWhitespace();
        if (collection) {
            st = cursor;
            if (cursor == end)
                throw new PropertyAccessException("unterminated '['", expr, this.start, pCtx);
            if (scanTo(']'))
                throw new PropertyAccessException("unterminated '['", expr, this.start, pCtx);
            String ex = new String(property, st, cursor - st);
            if (ctx instanceof Map) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Map.class)) {
                    propHandlerSet(ex, ctx, Map.class, value);
                } else {
                    // noinspection unchecked
                    ((Map) ctx).put(eval(ex, ctx, variableFactory), convert(value, returnType = verifier.analyze()));
                    addAccessorNode(new MapAccessorNest(ex, returnType));
                }
                return rootNode;
            } else if (ctx instanceof List) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(List.class)) {
                    propHandlerSet(ex, ctx, List.class, value);
                } else {
                    // noinspection unchecked
                    ((List) ctx).set(eval(ex, ctx, variableFactory, Integer.class), convert(value, returnType = verifier.analyze()));
                    addAccessorNode(new ListAccessorNest(ex, returnType));
                }
                return rootNode;
            } else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
                propHandlerSet(ex, ctx, ctx.getClass(), value);
                return rootNode;
            } else if (ctx.getClass().isArray()) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Array.class)) {
                    propHandlerSet(ex, ctx, Array.class, value);
                } else {
                    // noinspection unchecked
                    Array.set(ctx, eval(ex, ctx, variableFactory, Integer.class), convert(value, getBaseComponentType(ctx.getClass())));
                    addAccessorNode(new ArrayAccessorNest(ex));
                }
                return rootNode;
            } else {
                throw new PropertyAccessException("cannot bind to collection property: " + new String(property) + ": not a recognized collection type: " + ctx.getClass(), expr, this.st, pCtx);
            }
        } else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
            propHandlerSet(new String(property), ctx, ctx.getClass(), value);
            return rootNode;
        }
        String tk = new String(property, 0, length).trim();
        if (hasSetListeners()) {
            notifySetListeners(ctx, tk, variableFactory, value);
            addAccessorNode(new Notify(tk));
        }
        Member member = getFieldOrWriteAccessor(ctx.getClass(), tk, value == null ? null : ingressType);
        if (member instanceof Field) {
            Field fld = (Field) member;
            if (value != null && !fld.getType().isAssignableFrom(value.getClass())) {
                if (!canConvert(fld.getType(), value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + fld.getType(), this.expr, this.start);
                }
                fld.set(ctx, convert(value, fld.getType()));
                addAccessorNode(new DynamicFieldAccessor(fld));
            } else if (value == null && fld.getType().isPrimitive()) {
                fld.set(ctx, PropertyTools.getPrimitiveInitialValue(fld.getType()));
                addAccessorNode(new FieldAccessor(fld));
            } else {
                fld.set(ctx, value);
                addAccessorNode(new FieldAccessor(fld));
            }
        } else if (member != null) {
            Method meth = (Method) member;
            if (value != null && !meth.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
                if (!canConvert(meth.getParameterTypes()[0], value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + meth.getParameterTypes()[0], this.expr, this.start);
                }
                meth.invoke(ctx, convert(value, meth.getParameterTypes()[0]));
            } else if (value == null && meth.getParameterTypes()[0].isPrimitive()) {
                meth.invoke(ctx, PropertyTools.getPrimitiveInitialValue(meth.getParameterTypes()[0]));
            } else {
                meth.invoke(ctx, value);
            }
            addAccessorNode(new SetterAccessor(meth));
        } else if (ctx instanceof Map) {
            // noinspection unchecked
            ((Map) ctx).put(tk, value);
            addAccessorNode(new MapAccessor(tk));
        } else {
            throw new PropertyAccessException("could not access property (" + tk + ") in: " + ingressType.getName(), this.expr, this.start, pCtx);
        }
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException("could not access property: " + new String(property), this.expr, st, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException("could not access property: " + new String(property), this.expr, st, e, pCtx);
    } catch (IllegalArgumentException e) {
        throw new PropertyAccessException("error binding property: " + new String(property) + " (value <<" + value + ">>::" + (value == null ? "null" : value.getClass().getCanonicalName()) + ")", this.expr, st, e, pCtx);
    }
    return rootNode;
}
Also used : PropertyVerifier(org.mvel2.compiler.PropertyVerifier) Notify(org.mvel2.optimizers.impl.refl.nodes.Notify) ArrayAccessorNest(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessorNest) PropertyAccessException(org.mvel2.PropertyAccessException) Method(java.lang.reflect.Method) MapAccessorNest(org.mvel2.optimizers.impl.refl.nodes.MapAccessorNest) FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) DynamicFieldAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor) InvocationTargetException(java.lang.reflect.InvocationTargetException) Array(java.lang.reflect.Array) Field(java.lang.reflect.Field) SetterAccessor(org.mvel2.optimizers.impl.refl.nodes.SetterAccessor) ListAccessorNest(org.mvel2.optimizers.impl.refl.nodes.ListAccessorNest) CompileException(org.mvel2.CompileException) List(java.util.List) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) Map(java.util.Map) Member(java.lang.reflect.Member) DynamicFieldAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor)

Aggregations

PropertyAccessException (org.mvel2.PropertyAccessException)12 CompileException (org.mvel2.CompileException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 Method (java.lang.reflect.Method)5 Test (org.junit.Test)5 KieServices (org.kie.api.KieServices)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5 ReleaseId (org.kie.api.builder.ReleaseId)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieSession (org.kie.api.runtime.KieSession)5 Field (java.lang.reflect.Field)4 Member (java.lang.reflect.Member)4 Map (java.util.Map)4 IOException (java.io.IOException)3 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)3 Array (java.lang.reflect.Array)2 List (java.util.List)2 OptimizationFailure (org.mvel2.OptimizationFailure)2 Label (org.mvel2.asm.Label)2 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)2