Search in sources :

Example 46 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class IndexedPreFixIncNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    VariableResolver vResolver = factory.getIndexedVariableResolver(register);
    vResolver.setValue(ctx = MathProcessor.doOperations(vResolver.getValue(), Operator.ADD, DataTypes.INTEGER, 1));
    return ctx;
}
Also used : VariableResolver(org.mvel2.integration.VariableResolver)

Example 47 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory 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 48 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class InlineCollectionNode method execGraph.

private Object execGraph(Object o, Class type, Object ctx, VariableResolverFactory factory) {
    if (o instanceof List) {
        ArrayList list = new ArrayList(((List) o).size());
        for (Object item : (List) o) {
            list.add(execGraph(item, type, ctx, factory));
        }
        return list;
    } else if (o instanceof Map) {
        HashMap map = new HashMap();
        for (Object item : ((Map) o).keySet()) {
            map.put(execGraph(item, type, ctx, factory), execGraph(((Map) o).get(item), type, ctx, factory));
        }
        return map;
    } else if (o instanceof Object[]) {
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        Object newArray = Array.newInstance(getSubComponentType(type), ((Object[]) o).length);
        try {
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + getBaseComponentType(type).getName() + ";", AbstractParser.getCurrentThreadParserContext()) : type;
            int c = 0;
            for (Object item : (Object[]) o) {
                Array.set(newArray, c++, execGraph(item, cls, ctx, factory));
            }
            return newArray;
        } catch (IllegalArgumentException e) {
            throw new CompileException("type mismatch in array", expr, start, e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
    } else {
        if (type.isArray()) {
            return MVEL.eval((String) o, ctx, factory, getBaseComponentType(type));
        } else {
            return MVEL.eval((String) o, ctx, factory);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class NewObjectNode method getReducedValue.

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    try {
        if (typeDescr.isArray()) {
            Class cls = findClass(factory, typeDescr.getClassName(), null);
            int[] s = new int[typeDescr.getArrayLength()];
            ArraySize[] arraySize = typeDescr.getArraySize();
            for (int i = 0; i < s.length; i++) {
                s[i] = convert(eval(arraySize[i].value, ctx, factory), Integer.class);
            }
            return newInstance(cls, s);
        } else {
            String[] cnsRes = captureContructorAndResidual(name, 0, name.length);
            List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
            if (constructorParms != null) {
                Class cls = findClass(factory, new String(subset(name, 0, findFirst('(', 0, name.length, name))).trim(), null);
                Object[] parms = new Object[constructorParms.size()];
                for (int i = 0; i < constructorParms.size(); i++) {
                    parms[i] = eval(constructorParms.get(i), ctx, factory);
                }
                Constructor cns = getBestConstructorCandidate(parms, cls, false);
                if (cns == null)
                    throw new CompileException("unable to find constructor for: " + cls.getName(), expr, start);
                for (int i = 0; i < parms.length; i++) {
                    //noinspection unchecked
                    parms[i] = convert(parms[i], cns.getParameterTypes()[i]);
                }
                if (cnsRes.length > 1) {
                    return PropertyAccessor.get(cnsRes[1], cns.newInstance(parms), factory, thisValue);
                } else {
                    return cns.newInstance(parms);
                }
            } else {
                Constructor<?> cns = Class.forName(typeDescr.getClassName(), true, currentThread().getContextClassLoader()).getConstructor(EMPTYCLS);
                if (cnsRes.length > 1) {
                    return PropertyAccessor.get(cnsRes[1], cns.newInstance(), factory, thisValue);
                } else {
                    return cns.newInstance();
                }
            }
        }
    } catch (CompileException e) {
        throw e;
    } catch (ClassNotFoundException e) {
        throw new CompileException("unable to resolve class: " + e.getMessage(), expr, start, e);
    } catch (NoSuchMethodException e) {
        throw new CompileException("cannot resolve constructor: " + e.getMessage(), expr, start, e);
    } catch (Exception e) {
        throw new CompileException("could not instantiate class: " + e.getMessage(), expr, start, e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) CompileException(org.mvel2.CompileException) CompileException(org.mvel2.CompileException)

Example 50 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class NewObjectNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (newObjectOptimizer == null) {
        if (egressType == null) {
            if (factory != null && factory.isResolveable(typeDescr.getClassName())) {
                try {
                    egressType = (Class) factory.getVariableResolver(typeDescr.getClassName()).getValue();
                    rewriteClassReferenceToFQCN(COMPILE_IMMEDIATE);
                    if (typeDescr.isArray()) {
                        try {
                            egressType = findClass(factory, repeatChar('[', typeDescr.getArrayLength()) + "L" + egressType.getName() + ";", null);
                        } catch (Exception e) {
                        // for now, don't handle this.
                        }
                    }
                } catch (ClassCastException e) {
                    throw new CompileException("cannot construct object: " + typeDescr.getClassName() + " is not a class reference", expr, start, e);
                }
            }
        }
        if (typeDescr.isArray()) {
            return (newObjectOptimizer = new NewObjectArray(getBaseComponentType(egressType.getComponentType()), typeDescr.getCompiledArraySize())).getValue(ctx, thisValue, factory);
        }
        try {
            AccessorOptimizer optimizer = getThreadAccessorOptimizer();
            ParserContext pCtx = new ParserContext();
            pCtx.getParserConfiguration().setAllImports(getInjectedImports(factory));
            newObjectOptimizer = optimizer.optimizeObjectCreation(pCtx, name, 0, name.length, ctx, thisValue, factory);
            /**
         * Check to see if the optimizer actually produced the object during optimization.  If so,
         * we return that value now.
         */
            if (optimizer.getResultOptPass() != null) {
                egressType = optimizer.getEgressType();
                return optimizer.getResultOptPass();
            }
        } catch (CompileException e) {
            throw ErrorUtil.rewriteIfNeeded(e, expr, start);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
    return newObjectOptimizer.getValue(ctx, thisValue, factory);
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) OptimizerFactory.getThreadAccessorOptimizer(org.mvel2.optimizers.OptimizerFactory.getThreadAccessorOptimizer) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException)

Aggregations

VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)16 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)15 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)15 VariableResolver (org.mvel2.integration.VariableResolver)14 HashMap (java.util.HashMap)10 CompileException (org.mvel2.CompileException)7 ParserContext (org.mvel2.ParserContext)7 AccessorOptimizer (org.mvel2.optimizers.AccessorOptimizer)7 List (java.util.List)6 Map (java.util.Map)6 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)6 Serializable (java.io.Serializable)5 ArrayList (java.util.ArrayList)5 ASTNode (org.mvel2.ast.ASTNode)5 CompiledExpression (org.mvel2.compiler.CompiledExpression)5 Foo (org.mvel2.tests.core.res.Foo)5 IOException (java.io.IOException)4 Debugger (org.mvel2.debug.Debugger)4 Frame (org.mvel2.debug.Frame)4 Interceptor (org.mvel2.integration.Interceptor)4