Search in sources :

Example 86 with ParserContext

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

the class InlineCollectionsTests method testToListStrictMode.

@SuppressWarnings({ "UnnecessaryBoxing" })
public void testToListStrictMode() {
    String text = "misc.toList(foo.bar.name, 'hello', 42, ['key1' : 'value1'," + " c : [ foo.bar.age, 'car', 42 ]], [42, [c : 'value1']] )";
    ParserContext ctx = new ParserContext();
    ctx.addInput("misc", MiscTestClass.class);
    ctx.addInput("foo", Foo.class);
    ctx.addInput("c", String.class);
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler(text, ctx);
    List list = (List) executeExpression(compiler.compile(), createTestMap());
    assertSame("dog", list.get(0));
    assertEquals("hello", list.get(1));
    assertEquals(new Integer(42), list.get(2));
    Map map = (Map) list.get(3);
    assertEquals("value1", map.get("key1"));
    List nestedList = (List) map.get("cat");
    assertEquals(14, nestedList.get(0));
    assertEquals("car", nestedList.get(1));
    assertEquals(42, nestedList.get(2));
    nestedList = (List) list.get(4);
    assertEquals(42, nestedList.get(0));
    map = (Map) nestedList.get(1);
    assertEquals("value1", map.get("cat"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) List(java.util.List) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 87 with ParserContext

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

the class TemplateCompiler method captureOrbInternal.

private int captureOrbInternal() {
    try {
        ParserContext pCtx = new ParserContext();
        cursor = balancedCaptureWithLineAccounting(template, start = cursor, length, '{', pCtx);
        line += pCtx.getLineCount();
        int ret = start + 1;
        start = cursor + 1;
        return ret;
    } catch (CompileException e) {
        e.setLineNumber(line);
        e.setColumn((cursor - colStart) + 1);
        throw e;
    }
}
Also used : CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext)

Example 88 with ParserContext

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

the class VariableSpaceCompiler method compile.

public static SimpleVariableSpaceModel compile(String expr, ParserContext pCtx) {
    String[] varNames = pCtx.getIndexedVarNames();
    ParserContext analysisContext = ParserContext.create();
    analysisContext.setIndexAllocation(true);
    MVEL.analysisCompile(expr, analysisContext);
    Set<String> localNames = analysisContext.getVariables().keySet();
    pCtx.addIndexedLocals(localNames);
    String[] locals = localNames.toArray(new String[localNames.size()]);
    String[] allVars = new String[varNames.length + locals.length];
    System.arraycopy(varNames, 0, allVars, 0, varNames.length);
    System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
    return new SimpleVariableSpaceModel(allVars);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 89 with ParserContext

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

the class GenericsTypeInferenceTest method testInferLastTypeParametersFromPropertyMethod.

public final void testInferLastTypeParametersFromPropertyMethod() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("a", A.class);
    final CompiledExpression compiledExpression = new ExpressionCompiler("a.getFooMap()[\"key\"].someMethod()", context).compile();
    final Object val = MVEL.executeExpression(compiledExpression, new AWrapper());
    assertEquals("Expression did not evaluate correctly: ", "bar", val);
    assertNotNull("No type parameters detected", context.getLastTypeParameters());
    assertEquals("Wrong parametric type inferred", String.class, context.getLastTypeParameters()[0]);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 90 with ParserContext

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

the class ArithmeticTests method testMathCeilWithDoubleCast.

public void testMathCeilWithDoubleCast() {
    String str = "Math.ceil( (double) x / 3 )";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport("Math", Math.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("x", Integer.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map vars = new HashMap();
    vars.put("x", 4);
    assertEquals(Math.ceil((double) 4 / 3), MVEL.executeExpression(stmt, vars));
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10