Search in sources :

Example 1 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class TypesAndInferenceTests method testStrongTyping3.

public void testStrongTyping3() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    try {
        new ExpressionCompiler("foo.toUC(100.5").compile(ctx);
    } catch (Exception e) {
        // should fail.
        return;
    }
    assertTrue(false);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 2 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler 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 3 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler 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 4 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class TypesAndInferenceTests method testMapWithStrictTyping.

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

Example 5 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class WithTests method testInlineWith3.

public void testInlineWith3() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}, toUC('doopy')}").compile();
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
    assertEquals("foobie", f.getBar().getName());
    assertEquals("doopy", f.register);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)110 CompiledExpression (org.mvel2.compiler.CompiledExpression)33 ParserContext (org.mvel2.ParserContext)31 Serializable (java.io.Serializable)15 Foo (org.mvel2.tests.core.res.Foo)12 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)11 HashMap (java.util.HashMap)9 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)9 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashSet (java.util.HashSet)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)5 List (java.util.List)4 Interceptor (org.mvel2.integration.Interceptor)4 Map (java.util.Map)3 ASTNode (org.mvel2.ast.ASTNode)3 WithNode (org.mvel2.ast.WithNode)3 Type (java.lang.reflect.Type)2 LinkedHashMap (java.util.LinkedHashMap)2 Macro (org.mvel2.Macro)2