Search in sources :

Example 6 with MapAccessorNest

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

the class ReflectiveAccessorOptimizer method getCollectionPropertyAO.

private Object getCollectionPropertyAO(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanPropertyAO(ctx, prop);
    }
    currType = null;
    if (ctx == null)
        return null;
    int _start = ++cursor;
    skipWhitespace();
    if (cursor == end)
        throw new CompileException("unterminated '['", this.expr, this.start);
    String item;
    if (scanTo(']'))
        throw new CompileException("unterminated '['", this.expr, this.start);
    item = new String(expr, _start, cursor - _start);
    boolean itemSubExpr = true;
    Object idx = null;
    try {
        idx = parseInt(item);
        itemSubExpr = false;
    } catch (Exception e) {
    // not a number;
    }
    ExecutableStatement itemStmt = null;
    if (itemSubExpr) {
        idx = (itemStmt = (ExecutableStatement) subCompileExpression(item.toCharArray(), pCtx)).getValue(thisRef, thisRef, variableFactory);
    }
    ++cursor;
    if (ctx instanceof Map) {
        if (hasPropertyHandler(Map.class)) {
            return propHandler(item, ctx, Map.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new MapAccessorNest(itemStmt, null));
            } else {
                addAccessorNode(new MapAccessor(parseInt(item)));
            }
            return ((Map) ctx).get(idx);
        }
    } else if (ctx instanceof List) {
        if (hasPropertyHandler(List.class)) {
            return propHandler(item, ctx, List.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new ListAccessorNest(itemStmt, null));
            } else {
                addAccessorNode(new ListAccessor(parseInt(item)));
            }
            return ((List) ctx).get((Integer) idx);
        }
    } else if (ctx.getClass().isArray()) {
        if (hasPropertyHandler(Array.class)) {
            return propHandler(item, ctx, Array.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new ArrayAccessorNest(itemStmt));
            } else {
                addAccessorNode(new ArrayAccessor(parseInt(item)));
            }
            return Array.get(ctx, (Integer) idx);
        }
    } else if (ctx instanceof CharSequence) {
        if (hasPropertyHandler(CharSequence.class)) {
            return propHandler(item, ctx, CharSequence.class);
        } else {
            if (itemSubExpr) {
                addAccessorNode(new IndexedCharSeqAccessorNest(itemStmt));
            } else {
                addAccessorNode(new IndexedCharSeqAccessor(parseInt(item)));
            }
            return ((CharSequence) ctx).charAt((Integer) idx);
        }
    } else {
        TypeDescriptor tDescr = new TypeDescriptor(expr, this.start, end - this.start, 0);
        if (tDescr.isArray()) {
            Class cls = getClassReference((Class) ctx, tDescr, variableFactory, pCtx);
            rootNode = new StaticReferenceAccessor(cls);
            return cls;
        }
        throw new CompileException("illegal use of []: unknown type: " + ctx.getClass().getName(), this.expr, this.st);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ArrayAccessorNest(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessorNest) IndexedCharSeqAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessor) MapAccessorNest(org.mvel2.optimizers.impl.refl.nodes.MapAccessorNest) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) TypeDescriptor(org.mvel2.ast.TypeDescriptor) 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) IndexedCharSeqAccessorNest(org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessorNest)

Example 7 with MapAccessorNest

use of org.mule.mvel2.optimizers.impl.refl.nodes.MapAccessorNest 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

ArrayAccessorNest (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessorNest)5 ListAccessorNest (org.mvel2.optimizers.impl.refl.nodes.ListAccessorNest)5 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)5 MapAccessorNest (org.mvel2.optimizers.impl.refl.nodes.MapAccessorNest)5 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)4 ArrayAccessor (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor)4 ListAccessor (org.mvel2.optimizers.impl.refl.nodes.ListAccessor)4 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 Map (java.util.Map)3 CompileException (org.mvel2.CompileException)3 PropertyAccessException (org.mvel2.PropertyAccessException)3 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)3 Constructor (java.lang.reflect.Constructor)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ClassUtils.convertToPrimitiveType (org.drools.core.util.ClassUtils.convertToPrimitiveType)2 AccessorNode (org.mule.mvel2.compiler.AccessorNode)2