Search in sources :

Example 1 with IndexedCharSeqAccessorNest

use of org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessorNest in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method getCollectionProperty.

/**
 * Handle accessing a property embedded in a collections, map, or array
 *
 * @param ctx  -
 * @param prop -
 * @return -
 * @throws Exception -
 */
private Object getCollectionProperty(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanProperty(ctx, prop);
    }
    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) {
        try {
            idx = (itemStmt = (ExecutableStatement) subCompileExpression(item.toCharArray(), pCtx)).getValue(ctx, thisRef, variableFactory);
        } catch (CompileException e) {
            e.setExpr(this.expr);
            e.setCursor(start);
            throw e;
        }
    }
    ++cursor;
    if (ctx instanceof Map) {
        if (itemSubExpr) {
            addAccessorNode(new MapAccessorNest(itemStmt, null));
        } else {
            addAccessorNode(new MapAccessor(parseInt(item)));
        }
        return ((Map) ctx).get(idx);
    } else if (ctx instanceof List) {
        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 (itemSubExpr) {
            addAccessorNode(new ArrayAccessorNest(itemStmt));
        } else {
            addAccessorNode(new ArrayAccessor(parseInt(item)));
        }
        return Array.get(ctx, (Integer) idx);
    } else if (ctx instanceof CharSequence) {
        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, length, 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.start);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) TypeDescriptor(org.mvel2.ast.TypeDescriptor) List(java.util.List) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 2 with IndexedCharSeqAccessorNest

use of org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessorNest in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method getCollectionPropertyAO.

private Object getCollectionPropertyAO(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanPropertyAO(ctx, prop);
    }
    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(ctx, 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) TypeDescriptor(org.mvel2.ast.TypeDescriptor) List(java.util.List) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 3 with IndexedCharSeqAccessorNest

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

the class ReflectiveAccessorOptimizer method getCollectionProperty.

/**
 * Handle accessing a property embedded in a collections, map, or array
 *
 * @param ctx  -
 * @param prop -
 * @return -
 * @throws Exception -
 */
private Object getCollectionProperty(Object ctx, String prop) throws Exception {
    if (prop.length() > 0) {
        ctx = getBeanProperty(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) {
        try {
            idx = (itemStmt = (ExecutableStatement) subCompileExpression(item.toCharArray(), pCtx)).getValue(ctx, thisRef, variableFactory);
        } catch (CompileException e) {
            e.setExpr(this.expr);
            e.setCursor(start);
            throw e;
        }
    }
    ++cursor;
    if (ctx instanceof Map) {
        if (itemSubExpr) {
            addAccessorNode(new MapAccessorNest(itemStmt, null));
        } else {
            addAccessorNode(new MapAccessor(parseInt(item)));
        }
        return ((Map) ctx).get(idx);
    } else if (ctx instanceof List) {
        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 (itemSubExpr) {
            addAccessorNode(new ArrayAccessorNest(itemStmt));
        } else {
            addAccessorNode(new ArrayAccessor(parseInt(item)));
        }
        return Array.get(ctx, (Integer) idx);
    } else if (ctx instanceof CharSequence) {
        if (itemSubExpr) {
            addAccessorNode(new IndexedCharSeqAccessorNest(itemStmt));
        } else {
            addAccessorNode(new IndexedCharSeqAccessor(parseInt(item)));
        }
        return ((CharSequence) ctx).charAt((Integer) idx);
    } else if (ctx instanceof Class) {
        TypeDescriptor tDescr = new TypeDescriptor(expr, this.start, length, 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.start);
}
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 4 with IndexedCharSeqAccessorNest

use of org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessorNest 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(ctx, 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)

Aggregations

List (java.util.List)4 Map (java.util.Map)4 TypeDescriptor (org.mvel2.ast.TypeDescriptor)4 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 WeakHashMap (java.util.WeakHashMap)2 CompileException (org.mvel2.CompileException)2 PropertyAccessException (org.mvel2.PropertyAccessException)2 ArrayAccessor (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor)2 ArrayAccessorNest (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessorNest)2 IndexedCharSeqAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessor)2 IndexedCharSeqAccessorNest (org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessorNest)2 ListAccessor (org.mvel2.optimizers.impl.refl.nodes.ListAccessor)2 ListAccessorNest (org.mvel2.optimizers.impl.refl.nodes.ListAccessorNest)2 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)2 MapAccessorNest (org.mvel2.optimizers.impl.refl.nodes.MapAccessorNest)2 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)2