Search in sources :

Example 1 with Union

use of org.mvel2.ast.Union in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNode.

private Expression analyzeNode(ASTNode node) {
    node = analyzeRegEx(analyzeSubstatement(node));
    if (node instanceof LiteralNode) {
        LiteralNode literalNode = (LiteralNode) node;
        return new FixedExpression(literalNode.getEgressType(), literalNode.getLiteralValue());
    }
    if (node instanceof BinaryOperation) {
        BinaryOperation op = (BinaryOperation) node;
        return new AritmeticExpression(analyzeNode(op.getLeft()), AritmeticOperator.fromMvelOpCode(op.getOperation()), analyzeNode(op.getRight()));
    }
    if (node instanceof TypeCast) {
        ExecutableStatement statement = ((TypeCast) node).getStatement();
        if (statement instanceof ExecutableAccessor) {
            ExecutableAccessor accessor = (ExecutableAccessor) statement;
            return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
        } else {
            ExecutableLiteral literal = (ExecutableLiteral) statement;
            return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
        }
    }
    if (node instanceof Union) {
        ASTNode main = ((Union) node).getMain();
        Accessor accessor = node.getAccessor();
        EvaluatedExpression expression = new EvaluatedExpression();
        expression.firstExpression = analyzeNode(main);
        if (accessor instanceof DynamicGetAccessor) {
            AccessorNode accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
            expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
        } else if (accessor instanceof AccessorNode) {
            AccessorNode accessorNode = (AccessorNode) accessor;
            while (accessorNode != null) {
                expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            throw new RuntimeException("Unexpected accessor: " + accessor);
        }
        return expression;
    }
    if (node instanceof Sign) {
        ExecutableStatement statement = getFieldValue(Sign.class, "stmt", (Sign) node);
        if (statement instanceof ExecutableAccessor) {
            ExecutableAccessor accessor = (ExecutableAccessor) statement;
            return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, analyzeNode(accessor.getNode()));
        } else {
            ExecutableLiteral literal = (ExecutableLiteral) statement;
            return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, new FixedExpression(literal.getLiteral()));
        }
    }
    Accessor accessor = node.getAccessor();
    if (accessor instanceof IndexedVariableAccessor) {
        String variableName = node.getName();
        int dot = variableName.indexOf('.');
        if (dot > 0) {
            variableName = variableName.substring(0, dot);
        }
        Class<?> variableType = getVariableType(variableName);
        return new VariableExpression(variableName, analyzeExpressionNode(((AccessorNode) accessor).getNextNode(), node, variableType), variableType != null ? variableType : node.getEgressType());
    }
    if (accessor == null && node instanceof NewObjectNode) {
        accessor = ((NewObjectNode) node).getNewObjectOptimizer();
    }
    if (accessor instanceof VariableAccessor) {
        VariableAccessor variableAccessor = (VariableAccessor) accessor;
        AccessorNode accessorNode = variableAccessor.getNextNode();
        if (accessorNode == null || !isStaticAccessor(accessorNode)) {
            String variableName = (String) (variableAccessor.getProperty());
            Class<?> variableType = getVariableType(variableName);
            if (variableType != null) {
                return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType);
            } else {
                if (node.getLiteralValue() instanceof ParserContext) {
                    ParserContext pCtx = (ParserContext) node.getLiteralValue();
                    // it's not a variable but a method invocation on this
                    Class<?> thisClass = pCtx.getInputs().get("this");
                    try {
                        return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName)));
                    } catch (NoSuchMethodException e) {
                        if (node.getEgressType() == Class.class) {
                            // there's no method on this with the given name, check if it is a class literal
                            Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName);
                            if (classLiteral != null) {
                                return new FixedExpression(Class.class, classLiteral);
                            }
                        }
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    if (accessor == null) {
        throw new RuntimeException("Null accessor on node: " + node);
    }
    return analyzeNodeAccessor(accessor, node);
}
Also used : BinaryOperation(org.mvel2.ast.BinaryOperation) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) Accessor(org.mvel2.compiler.Accessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) Union(org.mvel2.ast.Union) AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral) ASTNode(org.mvel2.ast.ASTNode) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) LiteralNode(org.mvel2.ast.LiteralNode) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) NewObjectNode(org.mvel2.ast.NewObjectNode) Sign(org.mvel2.ast.Sign) TypeCast(org.mvel2.ast.TypeCast) ParserContext(org.mvel2.ParserContext)

Example 2 with Union

use of org.mvel2.ast.Union in project mvel by mvel.

the class ExpressionCompiler method verify.

protected ASTNode verify(ParserContext pCtx, ASTNode tk) {
    if (tk.isOperator() && (tk.getOperator().equals(Operator.AND) || tk.getOperator().equals(Operator.OR))) {
        secondPassOptimization = true;
    }
    if (tk.isDiscard() || tk.isOperator()) {
        return tk;
    } else if (tk.isLiteral()) {
        /**
         * Convert literal values from the default ASTNode to the more-efficient LiteralNode.
         */
        if ((fields & COMPILE_IMMEDIATE) != 0 && tk.getClass() == ASTNode.class) {
            return new LiteralNode(tk.getLiteralValue(), pCtx);
        } else {
            return tk;
        }
    }
    if (verifying) {
        if (tk.isIdentifier()) {
            PropertyVerifier propVerifier = new PropertyVerifier(expr, tk.getStart(), tk.getOffset(), pCtx);
            if (tk instanceof Union) {
                propVerifier.setCtx(((Union) tk).getLeftEgressType());
                tk.setEgressType(returnType = propVerifier.analyze());
            } else {
                tk.setEgressType(returnType = propVerifier.analyze());
                if (propVerifier.isFqcn()) {
                    tk.setAsFQCNReference();
                }
                if (propVerifier.isClassLiteral()) {
                    return new LiteralNode(returnType, pCtx);
                }
                if (propVerifier.isInput()) {
                    pCtx.addInput(tk.getAbsoluteName(), propVerifier.isDeepProperty() ? Object.class : returnType);
                }
                if (!propVerifier.isMethodCall() && !returnType.isEnum() && !pCtx.isOptimizerNotified() && pCtx.isStrongTyping() && !pCtx.isVariableVisible(tk.getAbsoluteName()) && !tk.isFQCN()) {
                    throw new CompileException("no such identifier: " + tk.getAbsoluteName(), expr, tk.getStart());
                }
            }
        } else if (tk.isAssignment()) {
            Assignment a = (Assignment) tk;
            if (a.getAssignmentVar() != null) {
                // pCtx.makeVisible(a.getAssignmentVar());
                PropertyVerifier propVerifier = new PropertyVerifier(a.getAssignmentVar(), pCtx);
                tk.setEgressType(returnType = propVerifier.analyze());
                if (!a.isNewDeclaration() && propVerifier.isResolvedExternally()) {
                    pCtx.addInput(tk.getAbsoluteName(), returnType);
                }
                ExecutableStatement c = (ExecutableStatement) subCompileExpression(expr, tk.getStart(), tk.getOffset(), pCtx);
                if (pCtx.isStrictTypeEnforcement()) {
                    /**
                     * If we're using strict type enforcement, we need to see if this coercion can be done now,
                     * or fail epicly.
                     */
                    if (!returnType.isAssignableFrom(c.getKnownEgressType()) && c.isLiteralOnly()) {
                        if (canConvert(c.getKnownEgressType(), returnType)) {
                            /**
                             * We convert the literal to the proper type.
                             */
                            try {
                                a.setValueStatement(new ExecutableLiteral(convert(c.getValue(null, null), returnType)));
                                return tk;
                            } catch (Exception e) {
                            // fall through.
                            }
                        } else if (returnType.isPrimitive() && unboxPrimitive(c.getKnownEgressType()).equals(returnType)) {
                            /**
                             * We ignore boxed primitive cases, since MVEL does not recognize primitives.
                             */
                            return tk;
                        }
                        throw new CompileException("cannot assign type " + c.getKnownEgressType().getName() + " to " + returnType.getName(), expr, st);
                    }
                }
            }
        } else if (tk instanceof NewObjectNode) {
            // this is a bit of a hack for now.
            NewObjectNode n = (NewObjectNode) tk;
            List<char[]> parms = ParseTools.parseMethodOrConstructor(tk.getNameAsArray());
            if (parms != null) {
                for (char[] p : parms) {
                    MVEL.analyze(p, pCtx);
                }
            }
        }
        returnType = tk.getEgressType();
    }
    if (!tk.isLiteral() && tk.getClass() == ASTNode.class && (tk.getFields() & ASTNode.ARRAY_TYPE_LITERAL) == 0) {
        if (pCtx.isStrongTyping())
            tk.strongTyping();
        tk.storePctx();
        tk.storeInLiteralRegister(pCtx);
    }
    return tk;
}
Also used : Assignment(org.mvel2.ast.Assignment) LiteralNode(org.mvel2.ast.LiteralNode) NewObjectNode(org.mvel2.ast.NewObjectNode) CompileException(org.mvel2.CompileException) Union(org.mvel2.ast.Union) CompileException(org.mvel2.CompileException)

Example 3 with Union

use of org.mvel2.ast.Union in project mvel by mvel.

the class ASMAccessorOptimizer method optimizeObjectCreation.

public Accessor optimizeObjectCreation(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    _initJIT();
    compiledInputs = new ArrayList<ExecutableStatement>();
    this.start = cursor = start;
    this.end = start + offset;
    this.length = this.end - this.start;
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    this.pCtx = pCtx;
    String[] cnsRes = captureContructorAndResidual(property, start, offset);
    List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
    try {
        if (constructorParms != null) {
            for (char[] constructorParm : constructorParms) {
                compiledInputs.add((ExecutableStatement) subCompileExpression(constructorParm, pCtx));
            }
            Class cls = findClass(factory, new String(subset(property, 0, findFirst('(', start, length, property))), pCtx);
            assert debug("NEW " + getInternalName(cls));
            mv.visitTypeInsn(NEW, getInternalName(cls));
            assert debug("DUP");
            mv.visitInsn(DUP);
            Object[] parms = new Object[constructorParms.size()];
            int i = 0;
            for (ExecutableStatement es : compiledInputs) {
                parms[i++] = es.getValue(ctx, factory);
            }
            Constructor cns = getBestConstructorCandidate(parms, cls, pCtx.isStrongTyping());
            if (cns == null) {
                StringBuilder error = new StringBuilder();
                for (int x = 0; x < parms.length; x++) {
                    error.append(parms[x].getClass().getName());
                    if (x + 1 < parms.length)
                        error.append(", ");
                }
                throw new CompileException("unable to find constructor: " + cls.getName() + "(" + error.toString() + ")", expr, st);
            }
            this.returnType = cns.getDeclaringClass();
            Class tg;
            for (i = 0; i < constructorParms.size(); i++) {
                assert debug("ALOAD 0");
                mv.visitVarInsn(ALOAD, 0);
                assert debug("GETFIELD p" + i);
                mv.visitFieldInsn(GETFIELD, className, "p" + i, "L" + NAMESPACE + "compiler/ExecutableStatement;");
                assert debug("ALOAD 2");
                mv.visitVarInsn(ALOAD, 2);
                assert debug("ALOAD 3");
                mv.visitVarInsn(ALOAD, 3);
                assert debug("INVOKEINTERFACE " + NAMESPACE + "compiler/ExecutableStatement.getValue");
                mv.visitMethodInsn(INVOKEINTERFACE, "" + NAMESPACE + "compiler/ExecutableStatement", "getValue", "(Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;");
                tg = cns.getParameterTypes()[i].isPrimitive() ? getWrapperClass(cns.getParameterTypes()[i]) : cns.getParameterTypes()[i];
                if (parms[i] != null && !parms[i].getClass().isAssignableFrom(cns.getParameterTypes()[i])) {
                    ldcClassConstant(tg);
                    assert debug("INVOKESTATIC " + NAMESPACE + "DataConversion.convert");
                    mv.visitMethodInsn(INVOKESTATIC, "" + NAMESPACE + "DataConversion", "convert", "(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;");
                    if (cns.getParameterTypes()[i].isPrimitive()) {
                        unwrapPrimitive(cns.getParameterTypes()[i]);
                    } else {
                        assert debug("CHECKCAST " + getInternalName(tg));
                        mv.visitTypeInsn(CHECKCAST, getInternalName(tg));
                    }
                } else {
                    assert debug("CHECKCAST " + getInternalName(cns.getParameterTypes()[i]));
                    mv.visitTypeInsn(CHECKCAST, getInternalName(cns.getParameterTypes()[i]));
                }
            }
            assert debug("INVOKESPECIAL " + getInternalName(cls) + ".<init> : " + getConstructorDescriptor(cns));
            mv.visitMethodInsn(INVOKESPECIAL, getInternalName(cls), "<init>", getConstructorDescriptor(cns));
            _finishJIT();
            Accessor acc = _initializeAccessor();
            if (cnsRes.length > 1 && cnsRes[1] != null && !cnsRes[1].trim().equals("")) {
                return new Union(pCtx, acc, cnsRes[1].toCharArray(), 0, cnsRes[1].length());
            }
            return acc;
        } else {
            Class cls = findClass(factory, new String(property), pCtx);
            assert debug("NEW " + getInternalName(cls));
            mv.visitTypeInsn(NEW, getInternalName(cls));
            assert debug("DUP");
            mv.visitInsn(DUP);
            Constructor cns = cls.getConstructor(EMPTYCLS);
            assert debug("INVOKESPECIAL <init>");
            mv.visitMethodInsn(INVOKESPECIAL, getInternalName(cls), "<init>", getConstructorDescriptor(cns));
            _finishJIT();
            Accessor acc = _initializeAccessor();
            if (cnsRes.length > 1 && cnsRes[1] != null && !cnsRes[1].trim().equals("")) {
                return new Union(pCtx, acc, cnsRes[1].toCharArray(), 0, cnsRes[1].length());
            }
            return acc;
        }
    } catch (ClassNotFoundException e) {
        throw new CompileException("class or class reference not found: " + new String(property), property, st);
    } catch (Exception e) {
        throw new OptimizationFailure("could not optimize construtor: " + new String(property), e);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Constructor(java.lang.reflect.Constructor) OptimizationFailure(org.mvel2.OptimizationFailure) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) Accessor(org.mvel2.compiler.Accessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Union(org.mvel2.optimizers.impl.refl.nodes.Union) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) CompileException(org.mvel2.CompileException)

Example 4 with Union

use of org.mvel2.ast.Union in project mvel by mvel.

the class ReflectiveAccessorOptimizer method optimizeCollection.

public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.start = this.cursor = start;
    this.length = start + offset;
    this.returnType = type;
    this.ctx = ctx;
    this.variableFactory = factory;
    this.pCtx = pCtx;
    Accessor root = _getAccessor(o, returnType);
    if (property != null && length > start) {
        return new Union(pCtx, root, property, cursor, offset);
    } else {
        return root;
    }
}
Also used : FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) DynamicFieldAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) SetterAccessor(org.mvel2.optimizers.impl.refl.nodes.SetterAccessor) Accessor(org.mvel2.compiler.Accessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) IndexedCharSeqAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor) FunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.FunctionAccessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) WithAccessor(org.mvel2.optimizers.impl.refl.nodes.WithAccessor) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) PropertyHandlerAccessor(org.mvel2.optimizers.impl.refl.nodes.PropertyHandlerAccessor) Union(org.mvel2.optimizers.impl.refl.nodes.Union)

Example 5 with Union

use of org.mvel2.ast.Union in project mvel by mikebrock.

the class ASMAccessorOptimizer method optimizeCollection.

public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.expr = property;
    this.cursor = this.start = start;
    this.end = start + offset;
    this.length = offset;
    this.returnType = type;
    this.compiledInputs = new ArrayList<ExecutableStatement>();
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    _initJIT();
    literal = true;
    _getAccessor(o, type);
    _finishJIT();
    try {
        Accessor compiledAccessor = _initializeAccessor();
        if (property != null && length > start) {
            return new Union(compiledAccessor, property, start, length);
        } else {
            return compiledAccessor;
        }
    } catch (Exception e) {
        throw new OptimizationFailure("could not optimize collection", e);
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Union(org.mvel2.optimizers.impl.refl.nodes.Union) IOException(java.io.IOException)

Aggregations

CompileException (org.mvel2.CompileException)7 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)6 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)6 Accessor (org.mvel2.compiler.Accessor)5 IOException (java.io.IOException)4 Union (org.mvel2.ast.Union)4 Union (org.mvel2.optimizers.impl.refl.nodes.Union)4 LiteralNode (org.mvel2.ast.LiteralNode)3 NewObjectNode (org.mvel2.ast.NewObjectNode)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 OptimizationFailure (org.mvel2.OptimizationFailure)2 PropertyAccessException (org.mvel2.PropertyAccessException)2 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)2 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)2 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)2 ArrayAccessor (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor)2 ConstructorAccessor (org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)2 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)2 GetterAccessor (org.mvel2.optimizers.impl.refl.nodes.GetterAccessor)2