Search in sources :

Example 1 with AccessorNode

use of org.mule.mvel2.compiler.AccessorNode in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNodeAccessor.

private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
    AccessorNode accessorNode;
    if (accessor instanceof DynamicGetAccessor) {
        accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
    } else if (accessor instanceof AccessorNode) {
        accessorNode = (AccessorNode) accessor;
    } else if (accessor instanceof CompiledExpression) {
        return analyzeNode(((CompiledExpression) accessor).getFirstNode());
    } else if (accessor instanceof ListCreator) {
        return analyzeListCreation(((ListCreator) accessor));
    } else if (accessor instanceof ArrayCreator) {
        return analyzeArrayCreation(((ArrayCreator) accessor));
    } else {
        throw new RuntimeException("Unknown accessor type: " + accessor);
    }
    if (accessorNode instanceof VariableAccessor) {
        if (isStaticAccessor(accessorNode)) {
            while (accessorNode instanceof VariableAccessor) {
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            return analyzeNodeAccessor(accessorNode, node);
        }
    }
    while (accessorNode instanceof StaticReferenceAccessor) {
        StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
        Object literal = staticReferenceAccessor.getLiteral();
        accessorNode = accessorNode.getNextNode();
        if (accessorNode == null) {
            return new FixedExpression(literal.getClass(), literal);
        }
    }
    return analyzeExpressionNode(accessorNode, node, null);
}
Also used : AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) CompiledExpression(org.mvel2.compiler.CompiledExpression) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)

Example 2 with AccessorNode

use of org.mule.mvel2.compiler.AccessorNode 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 3 with AccessorNode

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

the class NullSafe method getValue.

public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
    if (ctx == null)
        return null;
    if (nextNode == null) {
        final Accessor a = OptimizerFactory.getAccessorCompiler(OptimizerFactory.SAFE_REFLECTIVE).optimizeAccessor(pCtx, expr, start, offset, ctx, elCtx, variableFactory, true, ctx.getClass());
        nextNode = new AccessorNode() {

            public AccessorNode getNextNode() {
                return null;
            }

            public AccessorNode setNextNode(AccessorNode accessorNode) {
                return null;
            }

            public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
                return a.getValue(ctx, elCtx, variableFactory);
            }

            public Object setValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory, Object value) {
                return a.setValue(ctx, elCtx, variableFactory, value);
            }

            public Class getKnownEgressType() {
                return a.getKnownEgressType();
            }
        };
    }
    // else {
    return nextNode.getValue(ctx, elCtx, variableFactory);
// }
}
Also used : AccessorNode(org.mvel2.compiler.AccessorNode) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) Accessor(org.mvel2.compiler.Accessor)

Example 4 with AccessorNode

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

the class ReflectiveAccessorOptimizer method compileConstructor.

@SuppressWarnings({ "WeakerAccess" })
private AccessorNode compileConstructor(char[] expression, Object ctx, VariableResolverFactory vars) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException {
    String[] cnsRes = captureContructorAndResidual(expression, start, length);
    List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
    if (constructorParms != null) {
        String s = new String(subset(expression, 0, ArrayTools.findFirst('(', start, length, expression)));
        Class cls = ParseTools.findClass(vars, s, pCtx);
        ExecutableStatement[] cStmts = new ExecutableStatement[constructorParms.size()];
        for (int i = 0; i < constructorParms.size(); i++) {
            cStmts[i] = (ExecutableStatement) subCompileExpression(constructorParms.get(i), pCtx);
        }
        Object[] parms = new Object[constructorParms.size()];
        for (int i = 0; i < constructorParms.size(); i++) {
            parms[i] = cStmts[i].getValue(ctx, vars);
        }
        Constructor cns = getBestConstructorCandidate(parms, cls, pCtx.isStrongTyping());
        if (cns == null) {
            StringBuilder error = new StringBuilder();
            for (int i = 0; i < parms.length; i++) {
                error.append(parms[i].getClass().getName());
                if (i + 1 < parms.length)
                    error.append(", ");
            }
            throw new CompileException("unable to find constructor: " + cls.getName() + "(" + error.toString() + ")", this.expr, this.start);
        }
        for (int i = 0; i < parms.length; i++) {
            // noinspection unchecked
            parms[i] = convert(parms[i], paramTypeVarArgsSafe(cns.getParameterTypes(), i, cns.isVarArgs()));
        }
        parms = normalizeArgsForVarArgs(cns.getParameterTypes(), parms, cns.isVarArgs());
        AccessorNode ca = new ConstructorAccessor(cns, cStmts);
        if (cnsRes.length > 1) {
            ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(pCtx, cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(parms), ctx, vars);
            compiledOptimizer.ingressType = cns.getDeclaringClass();
            compiledOptimizer.setRootNode(ca);
            compiledOptimizer.compileGetChain();
            ca = compiledOptimizer.getRootNode();
            this.val = compiledOptimizer.getResultOptPass();
        }
        return ca;
    } else {
        ClassLoader classLoader = pCtx != null ? pCtx.getClassLoader() : currentThread().getContextClassLoader();
        Constructor<?> cns = Class.forName(new String(expression), true, classLoader).getConstructor(EMPTYCLS);
        AccessorNode ca = new ConstructorAccessor(cns, null);
        if (cnsRes.length > 1) {
            // noinspection NullArgumentToVariableArgMethod
            ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(pCtx, cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(null), ctx, vars);
            compiledOptimizer.setRootNode(ca);
            compiledOptimizer.compileGetChain();
            ca = compiledOptimizer.getRootNode();
            this.val = compiledOptimizer.getResultOptPass();
        }
        return ca;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Constructor(java.lang.reflect.Constructor) AccessorNode(org.mvel2.compiler.AccessorNode) CompileException(org.mvel2.CompileException) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)

Example 5 with AccessorNode

use of org.mule.mvel2.compiler.AccessorNode in project mule by mulesoft.

the class AbstractVariableEnricherDataTypePropagator method doPropagate.

@Override
protected boolean doPropagate(PrivilegedEvent event, PrivilegedEvent.Builder builder, TypedValue typedValue, ASTNode node) {
    if (isAssignmentNode(node)) {
        CompiledAccExpression compiledAccExpression = getCompiledAccExpression(node);
        if (compiledAccExpression.getAccessor() instanceof VariableAccessor) {
            VariableAccessor variableAccessor = (VariableAccessor) compiledAccExpression.getAccessor();
            if (variableAccessor.getProperty().equals(propertyName)) {
                final AccessorNode nextNode = variableAccessor.getNextNode();
                if (nextNode.getNextNode() == null) {
                    String propertyName = null;
                    if (nextNode instanceof MapAccessorNest) {
                        final MapAccessorNest mapAccesorNest = (MapAccessorNest) nextNode;
                        if (mapAccesorNest.getProperty().isLiteralOnly()) {
                            propertyName = (String) ((ExecutableLiteral) mapAccesorNest.getProperty()).getLiteral();
                        }
                    } else if (nextNode instanceof MapAccessor) {
                        propertyName = (String) ((MapAccessor) nextNode).getProperty();
                    }
                    if (propertyName != null && containsVariable(event, propertyName)) {
                        propertyName = getUnescapedPropertyName(propertyName);
                        addVariable(event, builder, typedValue, propertyName);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : AccessorNode(org.mule.mvel2.compiler.AccessorNode) VariableAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ExecutableLiteral(org.mule.mvel2.compiler.ExecutableLiteral) CompiledAccExpression(org.mule.mvel2.compiler.CompiledAccExpression) MapAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessor) MapAccessorNest(org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessorNest)

Aggregations

AccessorNode (org.mvel2.compiler.AccessorNode)8 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)4 Accessor (org.mvel2.compiler.Accessor)3 AccessorNode (org.mule.mvel2.compiler.AccessorNode)2 ExecutableLiteral (org.mule.mvel2.compiler.ExecutableLiteral)2 MapAccessor (org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessor)2 MapAccessorNest (org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessorNest)2 VariableAccessor (org.mule.mvel2.optimizers.impl.refl.nodes.VariableAccessor)2 CompileException (org.mvel2.CompileException)2 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)2 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)2 DynamicGetAccessor (org.mvel2.optimizers.dynamic.DynamicGetAccessor)2 ConstructorAccessor (org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)2 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)2 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)2 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)2 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Array (java.lang.reflect.Array)1