Search in sources :

Example 86 with ParserContext

use of org.mvel2.ParserContext in project mvel by mikebrock.

the class TypesAndInferenceTests method testMVEL236.

public void testMVEL236() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("MyInterface2 var2 = (MyInterface2)var1;");
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("var1", MyInterface3.class);
    ctx.addImport(MyInterface2.class);
    ctx.addImport(MyInterface3.class);
    try {
        CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 87 with ParserContext

use of org.mvel2.ParserContext in project mvel by mikebrock.

the class TypesAndInferenceTests method testParameterizedTypeInStrictMode2.

public void testParameterizedTypeInStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("ctx", Object.class);
    ExpressionCompiler compiler = new ExpressionCompiler("org.mvel2.DataConversion.convert(ctx, String).toUpperCase()");
    assertEquals(String.class, compiler.compile(ctx).getKnownEgressType());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 88 with ParserContext

use of org.mvel2.ParserContext in project mvel by mikebrock.

the class TypesAndInferenceTests method testTypeVarDeclr.

public void testTypeVarDeclr() {
    String ex = "String a;";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    compiler.compile(ctx);
    assertNotNull(ctx.getVariables());
    assertEquals(1, ctx.getVariables().entrySet().size());
    for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
        assertEquals(String.class, entry.getValue());
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 89 with ParserContext

use of org.mvel2.ParserContext in project mvel by mikebrock.

the class TemplateTests method testMVEL244.

public void testMVEL244() {
    Foo244 foo = new Foo244("plop");
    String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
    CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("foo", foo);
    System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 90 with ParserContext

use of org.mvel2.ParserContext in project mvel by mikebrock.

the class ForNode method buildForEach.

private boolean buildForEach(char[] condition, int start, int offset, int blockStart, int blockEnd, int fields, ParserContext pCtx) {
    int end = start + offset;
    int cursor = nextCondPart(condition, start, end, false);
    boolean varsEscape = false;
    try {
        ParserContext spCtx = pCtx;
        if (pCtx != null) {
            spCtx = pCtx.createSubcontext().createColoringSubcontext();
        } else {
            spCtx = new ParserContext();
        }
        this.initializer = (ExecutableStatement) subCompileExpression(condition, start, cursor - start - 1, spCtx);
        if (pCtx != null) {
            pCtx.pushVariableScope();
        }
        try {
            expectType(this.condition = (ExecutableStatement) subCompileExpression(condition, start = cursor, (cursor = nextCondPart(condition, start, end, false)) - start - 1, spCtx), Boolean.class, ((fields & COMPILE_IMMEDIATE) != 0));
        } catch (CompileException e) {
            if (e.getExpr().length == 0) {
                e.setExpr(expr);
                while (start < expr.length && ParseTools.isWhitespace(expr[start])) {
                    start++;
                }
                e.setCursor(start);
            }
            throw e;
        }
        this.after = (ExecutableStatement) subCompileExpression(condition, start = cursor, (nextCondPart(condition, start, end, true)) - start, spCtx);
        if (spCtx != null && (fields & COMPILE_IMMEDIATE) != 0 && spCtx.isVariablesEscape()) {
            if (pCtx != spCtx)
                pCtx.addVariables(spCtx.getVariables());
            varsEscape = true;
        } else if (spCtx != null && pCtx != null) {
            pCtx.addVariables(spCtx.getVariables());
        }
        this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockEnd, spCtx);
    } catch (NegativeArraySizeException e) {
        throw new CompileException("wrong syntax; did you mean to use 'foreach'?", expr, start);
    }
    return varsEscape;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)372 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)190 HashMap (java.util.HashMap)140 Serializable (java.io.Serializable)100 ParserConfiguration (org.mvel2.ParserConfiguration)86 Map (java.util.Map)74 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)67 LinkedHashMap (java.util.LinkedHashMap)66 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 CompileException (org.mvel2.CompileException)53 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)31 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)24 MapObject (org.mvel2.tests.core.res.MapObject)23 ArrayList (java.util.ArrayList)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)19 IOException (java.io.IOException)16 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)16 Debugger (org.mvel2.debug.Debugger)16