Search in sources :

Example 16 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class InlineCollectionNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (accessor != null) {
        return accessor.getValue(ctx, thisValue, factory);
    } else {
        try {
            AccessorOptimizer ao = OptimizerFactory.getThreadAccessorOptimizer();
            if (collectionGraph == null)
                parseGraph(true, null, null);
            accessor = ao.optimizeCollection(AbstractParser.getCurrentThreadParserContext(), collectionGraph, egressType, expr, trailingStart, trailingOffset, ctx, thisValue, factory);
            egressType = ao.getEgressType();
            return accessor.getValue(ctx, thisValue, factory);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer)

Example 17 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method _getAccessor.

private Accessor _getAccessor(Object o, Class type) {
    if (o instanceof List) {
        Accessor[] a = new Accessor[((List) o).size()];
        int i = 0;
        for (Object item : (List) o) {
            a[i++] = _getAccessor(item, type);
        }
        returnType = List.class;
        return new ListCreator(a);
    } else if (o instanceof Map) {
        Accessor[] k = new Accessor[((Map) o).size()];
        Accessor[] v = new Accessor[k.length];
        int i = 0;
        for (Object item : ((Map) o).keySet()) {
            // key
            k[i] = _getAccessor(item, type);
            // value
            v[i++] = _getAccessor(((Map) o).get(item), type);
        }
        returnType = Map.class;
        return new MapCreator(k, v);
    } else if (o instanceof Object[]) {
        Accessor[] a = new Accessor[((Object[]) o).length];
        int i = 0;
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        try {
            Class base = getBaseComponentType(type);
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + base.getName() + ";", pCtx) : type;
            for (Object item : (Object[]) o) {
                expectType(a[i++] = _getAccessor(item, cls), base, true);
            }
            return new ArrayCreator(a, getSubComponentType(type));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
    } else {
        if (returnType == null)
            returnType = Object.class;
        if (type.isArray()) {
            return new ExprValueAccessor((String) o, type, ctx, variableFactory, pCtx);
        } else {
            return new ExprValueAccessor((String) o, Object.class, ctx, variableFactory, pCtx);
        }
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) List(java.util.List) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) MapCreator(org.mvel2.optimizers.impl.refl.collection.MapCreator)

Example 18 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

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(root, property, cursor, offset);
    } else {
        return root;
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Example 19 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method get.

public static Object get(String expression, Object ctx) {
    int hash = createSignatureHash(expression, ctx);
    Accessor accessor = REFLECTIVE_ACCESSOR_CACHE.get(hash);
    if (accessor != null) {
        return accessor.getValue(ctx, null, null);
    } else {
        REFLECTIVE_ACCESSOR_CACHE.put(hash, accessor = new ReflectiveAccessorOptimizer().optimizeAccessor(getCurrentThreadParserContext(), expression.toCharArray(), 0, expression.length(), ctx, null, null, false, null));
        return accessor.getValue(ctx, null, null);
    }
}
Also used : PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor)

Aggregations

IOException (java.io.IOException)5 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)5 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)5 Map (java.util.Map)4 Accessor (org.mvel2.compiler.Accessor)4 AccessorOptimizer (org.mvel2.optimizers.AccessorOptimizer)4 List (java.util.List)3 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)3 WeakHashMap (java.util.WeakHashMap)2 ClassWriter (org.mvel2.asm.ClassWriter)2 Label (org.mvel2.asm.Label)2 MethodVisitor (org.mvel2.asm.MethodVisitor)2 PropertyHandler (org.mvel2.integration.PropertyHandler)2 OptimizationNotSupported (org.mvel2.optimizers.OptimizationNotSupported)2 Union (org.mvel2.optimizers.impl.refl.nodes.Union)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CompileException (org.mvel2.CompileException)1 ParserConfiguration (org.mvel2.ParserConfiguration)1 ParserContext (org.mvel2.ParserContext)1