Search in sources :

Example 96 with ParserContext

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

the class TypesAndInferenceTests method testAutoBoxing2.

public void testAutoBoxing2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);
    Serializable s = compileExpression("java.util.List list = new java.util.ArrayList(); " + "list.add( base.intValue ); list", ctx);
    Map vars = new HashMap();
    vars.put("base", new Base());
    List list = (List) executeExpression(s, vars);
    assertEquals(1, list.size());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) Base(org.mvel2.tests.core.res.Base)

Example 97 with ParserContext

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

the class TypesAndInferenceTests method testAnalyzer.

public void testAnalyzer() {
    ParserContext ctx = new ParserContext();
    MVEL.compileExpression("order.id == 10", ctx);
    for (String input : ctx.getInputs().keySet()) {
        System.out.println("input>" + input);
    }
    assertEquals(1, ctx.getInputs().size());
    assertTrue(ctx.getInputs().containsKey("order"));
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 98 with ParserContext

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

the class TypesAndInferenceTests method testDataConverterStrictMode.

public void testDataConverterStrictMode() throws Exception {
    OptimizerFactory.setDefaultOptimizer("ASM");
    DataConversion.addConversionHandler(Date.class, new MVELDateCoercion());
    ParserContext ctx = new ParserContext();
    ctx.addImport("Cheese", Cheese.class);
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    Locale.setDefault(Locale.US);
    Cheese expectedCheese = new Cheese();
    expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974"));
    ExpressionCompiler compiler = new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c", ctx);
    Cheese actualCheese = (Cheese) executeExpression(compiler.compile(), createTestMap());
    assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
}
Also used : Cheese(org.mvel2.tests.core.res.Cheese) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) SimpleDateFormat(java.text.SimpleDateFormat)

Example 99 with ParserContext

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

the class TypesAndInferenceTests method testStaticFieldAccessForInputs.

public void testStaticFieldAccessForInputs() {
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    ParserContext pCtx = ParserContext.create();
    MVEL.analysisCompile("java.math.BigDecimal.TEN", pCtx);
    assertFalse(pCtx.getInputs().containsKey("java"));
    assertEquals(0, pCtx.getInputs().size());
// MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
// 
// pCtx = ParserContext.create();
// MVEL.analysisCompile("java.math.BigDecimal.TEN", pCtx);
// 
// assertFalse(pCtx.getInputs().containsKey("java"));
// 
// assertEquals(0,
// pCtx.getInputs().size());
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 100 with ParserContext

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

the class TypesAndInferenceTests method testGenericInference.

public void testGenericInference() {
    String expression = "$result = person.footributes[0].name";
    ParserContext ctx;
    MVEL.analysisCompile(expression, ctx = ParserContext.create().stronglyTyped().withInput("person", Person.class));
    assertEquals(String.class, ctx.getVarOrInputTypeOrNull("$result"));
    Serializable s = MVEL.compileExpression(expression, ParserContext.create().stronglyTyped().withInput("person", Person.class));
    Map<String, Object> vars = new HashMap<String, Object>();
    Person p = new Person();
    p.setFootributes(new ArrayList<Foo>());
    p.getFootributes().add(new Foo());
    vars.put("person", p);
    assertEquals("dog", executeExpression(s, vars));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) ParserContext(org.mvel2.ParserContext)

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