Search in sources :

Example 81 with ParserContext

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

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");
    Cheese actualCheese = (Cheese) executeExpression(compiler.compile(ctx), createTestMap());
    assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) SimpleDateFormat(java.text.SimpleDateFormat)

Example 82 with ParserContext

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

the class TypesAndInferenceTests method testGetCorrectInputs.

public void testGetCorrectInputs() {
    String str = "total = total + $cheese.price";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("total", int.class);
    pctx.addInput("$cheese", Cheese.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 83 with ParserContext

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

the class TypesAndInferenceTests method testMultiTypeVarDeclr3.

public void testMultiTypeVarDeclr3() {
    String ex = "int a = 52 * 3, b = 8, c = 16;";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    Serializable s = compiler.compile(ctx);
    assertNotNull(ctx.getVariables());
    assertEquals(3, ctx.getVariables().entrySet().size());
    for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
        assertEquals(Integer.class, entry.getValue());
    }
    Map vars = new HashMap();
    executeExpression(s, vars);
    assertEquals(52 * 3, vars.get("a"));
    assertEquals(8, vars.get("b"));
    assertEquals(16, vars.get("c"));
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 84 with ParserContext

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

the class TypesAndInferenceTests method testCompileTimeCoercion.

public void testCompileTimeCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    assertEquals(true, executeExpression(new ExpressionCompiler("foo.bar.woof == 'true'").compile(ctx), createTestMap()));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 85 with ParserContext

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

the class TypesAndInferenceTests method testMapAsContextWithStrictTyping.

public void testMapAsContextWithStrictTyping() {
    ExpressionCompiler compiler = new ExpressionCompiler("this['KEY1'] == $msg");
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addInput("$msg", String.class);
    ctx.addInput("this", Map.class);
    Serializable expr = compiler.compile(ctx);
    Map map = new HashMap();
    map.put("KEY1", "MSGONE");
    Map vars = new HashMap();
    vars.put("$msg", "MSGONE");
    Boolean bool = (Boolean) executeExpression(expr, map, vars);
    assertEquals(Boolean.TRUE, bool);
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

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