Search in sources :

Example 16 with Accessor

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

the class ASMAccessorOptimizer method optimizeCollection.

public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.expr = property;
    this.cursor = this.start = start;
    this.end = start + offset;
    this.length = offset;
    this.returnType = type;
    this.compiledInputs = new ArrayList<ExecutableStatement>();
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    this.pCtx = pCtx;
    _initJIT();
    literal = true;
    _getAccessor(o, type);
    _finishJIT();
    try {
        Accessor compiledAccessor = _initializeAccessor();
        if (property != null && length > start) {
            return new Union(pCtx, compiledAccessor, property, start, length);
        } else {
            return compiledAccessor;
        }
    } catch (Exception e) {
        throw new OptimizationFailure("could not optimize collection", e);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) OptimizationFailure(org.mvel2.OptimizationFailure) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) Accessor(org.mvel2.compiler.Accessor) Union(org.mvel2.optimizers.impl.refl.nodes.Union) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) CompileException(org.mvel2.CompileException)

Example 17 with Accessor

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

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(pCtx, 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 : FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) DynamicFieldAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFieldAccessor) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) SetterAccessor(org.mvel2.optimizers.impl.refl.nodes.SetterAccessor) Accessor(org.mvel2.compiler.Accessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) IndexedCharSeqAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedCharSeqAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor) FunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.FunctionAccessor) PropertyTools.getFieldOrWriteAccessor(org.mvel2.util.PropertyTools.getFieldOrWriteAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) WithAccessor(org.mvel2.optimizers.impl.refl.nodes.WithAccessor) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) PropertyTools.getFieldOrAccessor(org.mvel2.util.PropertyTools.getFieldOrAccessor) PropertyHandlerAccessor(org.mvel2.optimizers.impl.refl.nodes.PropertyHandlerAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) List(java.util.List) Map(java.util.Map) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) MapCreator(org.mvel2.optimizers.impl.refl.collection.MapCreator)

Example 18 with Accessor

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

the class AbstractVarAssignmentDataTypePropagatorTestCase method compileMelExpression.

private CompiledExpression compileMelExpression(String expression, PrivilegedEvent testEvent, PrivilegedEvent.Builder builder) {
    final ParserConfiguration parserConfiguration = MVELExpressionLanguage.createParserConfiguration(Collections.EMPTY_MAP);
    final MVELExpressionLanguageContext context = createMvelExpressionLanguageContext(testEvent, builder, parserConfiguration);
    CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(parserConfiguration));
    // Expression must be executed, otherwise the variable accessor is not properly configured
    MVEL.executeExpression(compiledExpression, context);
    return compiledExpression;
}
Also used : MVELExpressionLanguageContext(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext) ParserContext(org.mule.mvel2.ParserContext) CompiledExpression(org.mule.mvel2.compiler.CompiledExpression) ParserConfiguration(org.mule.mvel2.ParserConfiguration)

Example 19 with Accessor

use of org.mule.mvel2.compiler.Accessor 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

Accessor (org.mvel2.compiler.Accessor)16 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)9 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)9 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)9 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)9 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)7 ArrayAccessor (org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor)6 ConstructorAccessor (org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor)6 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)6 GetterAccessor (org.mvel2.optimizers.impl.refl.nodes.GetterAccessor)6 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)6 ListAccessor (org.mvel2.optimizers.impl.refl.nodes.ListAccessor)6 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)6 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)6 StaticVarAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor)6 ThisValueAccessor (org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor)6 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)6 AccessorNode (org.mvel2.compiler.AccessorNode)4 List (java.util.List)3