Search in sources :

Example 6 with ExecutableLiteral

use of org.mvel2.compiler.ExecutableLiteral 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)

Example 7 with ExecutableLiteral

use of org.mvel2.compiler.ExecutableLiteral in project drools by kiegroup.

the class ConditionAnalyzer method analyzeAccessorInvocation.

private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
    if (accessorNode instanceof GetterAccessor) {
        return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
    }
    if (accessorNode instanceof MethodAccessor) {
        MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
        Method method = methodAccessor.getMethod();
        MethodInvocation invocation = new MethodInvocation(method);
        boolean isVarArgs = method.isVarArgs();
        readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
        return invocation;
    }
    if (accessorNode instanceof ConstructorAccessor) {
        ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
        Constructor constructor = constructorAccessor.getConstructor();
        ConstructorInvocation invocation = new ConstructorInvocation(constructor);
        readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
        return invocation;
    }
    if (accessorNode instanceof ArrayAccessor) {
        ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
        return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
    }
    if (accessorNode instanceof ArrayAccessorNest) {
        ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
        ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
        return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
    }
    if (accessorNode instanceof ArrayLength) {
        return new ArrayLengthInvocation();
    }
    if (accessorNode instanceof ListAccessor) {
        Class<?> listType = getListType(formerInvocation);
        ListAccessor listAccessor = (ListAccessor) accessorNode;
        return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
    }
    if (accessorNode instanceof ListAccessorNest) {
        Class<?> listType = getListType(formerInvocation);
        ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
        ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
        return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
    }
    if (accessorNode instanceof MapAccessor) {
        MapAccessor mapAccessor = (MapAccessor) accessorNode;
        return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
    }
    if (accessorNode instanceof MapAccessorNest) {
        Class<?> keyType = Object.class;
        Class<?> valueType = Object.class;
        Type[] generics = getGenerics(formerInvocation);
        if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
            keyType = (Class<?>) generics[0];
            if (generics[1] instanceof Class)
                valueType = (Class<?>) generics[1];
        }
        MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
        ExecutableStatement statement = mapAccessor.getProperty();
        if (statement instanceof ExecutableLiteral) {
            return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
        } else {
            return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
        }
    }
    if (accessorNode instanceof FieldAccessor) {
        return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
    }
    if (accessorNode instanceof StaticVarAccessor) {
        Field field = ((StaticVarAccessor) accessorNode).getField();
        return new FieldAccessInvocation(field);
    }
    if (accessorNode instanceof ThisValueAccessor) {
        return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
    }
    throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
Also used : ArrayAccessorNest(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessorNest) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) Field(java.lang.reflect.Field) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) Constructor(java.lang.reflect.Constructor) ArrayLength(org.mvel2.optimizers.impl.refl.nodes.ArrayLength) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) Method(java.lang.reflect.Method) MapAccessorNest(org.mvel2.optimizers.impl.refl.nodes.MapAccessorNest) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) ClassUtils.convertToPrimitiveType(org.drools.core.util.ClassUtils.convertToPrimitiveType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) ListAccessorNest(org.mvel2.optimizers.impl.refl.nodes.ListAccessorNest) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)

Example 8 with ExecutableLiteral

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

the class AbstractVariableExpressionDataTypeResolver method getDataType.

@Override
protected DataType getDataType(PrivilegedEvent event, ASTNode node) {
    final Accessor accessor = node.getAccessor();
    if (accessor instanceof VariableAccessor) {
        VariableAccessor variableAccessor = (VariableAccessor) accessor;
        if (variableAccessor.getProperty().equals(propertyName)) {
            final AccessorNode nextNode = variableAccessor.getNextNode();
            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) {
                return getVariableDataType(event, propertyName);
            }
        }
    }
    return null;
}
Also used : AccessorNode(org.mule.mvel2.compiler.AccessorNode) VariableAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ExecutableLiteral(org.mule.mvel2.compiler.ExecutableLiteral) MapAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessor) MapAccessorNest(org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessorNest) MapAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessor) Accessor(org.mule.mvel2.compiler.Accessor) VariableAccessor(org.mule.mvel2.optimizers.impl.refl.nodes.VariableAccessor)

Aggregations

ExecutableLiteral (org.mvel2.compiler.ExecutableLiteral)5 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)4 CompileException (org.mvel2.CompileException)3 LiteralNode (org.mvel2.ast.LiteralNode)3 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)3 Method (java.lang.reflect.Method)2 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 OptimizationFailure (org.mvel2.OptimizationFailure)2 NewObjectNode (org.mvel2.ast.NewObjectNode)2 Union (org.mvel2.ast.Union)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 ListAccessor (org.mvel2.optimizers.impl.refl.nodes.ListAccessor)2 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)2