Search in sources :

Example 76 with ExpressionCompiler

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

the class FunctionsTest method testFunctionDefAndCall2.

public void testFunctionDefAndCall2() {
    ExpressionCompiler compiler = new ExpressionCompiler("function heyFoo() { return 'Foobar'; };\n" + "return heyFoo() + heyFoo();");
    Serializable s = compiler.compile();
    Map<String, Function> m = extractAllDeclaredFunctions((CompiledExpression) s);
    assertTrue(m.containsKey("heyFoo"));
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    OptimizerFactory.setDefaultOptimizer("dynamic");
}
Also used : Function(org.mvel2.ast.Function) Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 77 with ExpressionCompiler

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

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

the class TypesAndInferenceTests method testStrongTyping3.

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

Example 79 with ExpressionCompiler

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

the class TypesAndInferenceTests method testAnalysisCompile.

public void testAnalysisCompile() {
    ParserContext pCtx = new ParserContext();
    ExpressionCompiler e = new ExpressionCompiler("foo.aValue = 'bar'", pCtx);
    e.setVerifyOnly(true);
    e.compile();
    assertTrue(pCtx.getInputs().keySet().contains("foo"));
    assertEquals(1, pCtx.getInputs().size());
    assertEquals(0, pCtx.getVariables().size());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 80 with ExpressionCompiler

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

the class TypesAndInferenceTests method testStrictTypingCompilation3.

public void testStrictTypingCompilation3() throws NoSuchMethodException {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler("message='Hello';b=7;\nSystem.out.println(message + ';' + b);\n" + "System.out.println(message + ';' + b); b", ctx);
    assertEquals(7, executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory()));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)219 ParserContext (org.mvel2.ParserContext)119 CompiledExpression (org.mvel2.compiler.CompiledExpression)65 HashMap (java.util.HashMap)39 Serializable (java.io.Serializable)30 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)22 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)19 Map (java.util.Map)18 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 LinkedHashMap (java.util.LinkedHashMap)15 HashSet (java.util.HashSet)10 List (java.util.List)10 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ArrayList (java.util.ArrayList)8 CompileException (org.mvel2.CompileException)8 Interceptor (org.mvel2.integration.Interceptor)8 Macro (org.mvel2.Macro)6 ASTNode (org.mvel2.ast.ASTNode)6