Search in sources :

Example 46 with Type

use of org.mvel2.asm.Type in project mvel by mikebrock.

the class CompiledForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.executeExpression(ce[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else if (o instanceof Integer) {
            iters[i] = new CountIterator((Integer) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.executeExpression(cSepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : CountIterator(org.mvel2.templates.util.CountIterator) HashMap(java.util.HashMap) TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) CountIterator(org.mvel2.templates.util.CountIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 47 with Type

use of org.mvel2.asm.Type 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 48 with Type

use of org.mvel2.asm.Type 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 49 with Type

use of org.mvel2.asm.Type in project mvel by mikebrock.

the class CoreConfidenceTests method testContextFieldNotFound.

public void testContextFieldNotFound() {
    String str = "'stilton'.equals( type );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Cheese.class);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    MVEL.executeExpression(stmt, new Cheese(), new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 50 with Type

use of org.mvel2.asm.Type in project mvel by mikebrock.

the class CoreConfidenceTests method testStringConcatenation3.

public void testStringConcatenation3() {
    // BUG: return type of the string concatenation is inferred as double instead of String
    String ex = "services.log($av + \"Drop +5%: \"+$sb+\" avg: $\"+percent($av)+\" price: $\"+$pr );";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    ctx.addInput("$sb", String.class);
    ctx.addInput("$av", double.class);
    ctx.addInput("$pr", double.class);
    ctx.addInput("services", Services.class);
    ctx.addImport("percent", MVEL.getStaticMethod(String.class, "valueOf", new Class[] { double.class }));
    try {
        Serializable compiledExpression = MVEL.compileExpression(ex, ctx);
        Services services = new Services() {

            public void log(String text) {
            }
        };
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("services", services);
        vars.put("$sb", "RHT");
        vars.put("$av", 15.0);
        vars.put("$pr", 10.0);
        MVEL.executeExpression(compiledExpression, vars);
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Should not raise exception: " + e.getMessage());
    }
}
Also used : Serializable(java.io.Serializable) PublicClass(org.mvel2.tests.core.res.res2.PublicClass)

Aggregations

MethodVisitor (org.mvel2.asm.MethodVisitor)19 Map (java.util.Map)15 Label (org.mvel2.asm.Label)13 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)12 List (java.util.List)11 Type (org.mvel2.asm.Type)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CompileException (org.mvel2.CompileException)7 ParserContext (org.mvel2.ParserContext)7 HashMap (java.util.HashMap)6 TypeDescriptor (org.mvel2.ast.TypeDescriptor)6 WeakHashMap (java.util.WeakHashMap)4 WorkingMemory (org.drools.core.WorkingMemory)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Tuple (org.drools.core.spi.Tuple)4 FieldVisitor (org.mvel2.asm.FieldVisitor)4 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)4 LeftTuple (org.drools.core.reteoo.LeftTuple)3 DeclarationMatcher (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher)3