Search in sources :

Example 41 with ParserContext

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

the class CoreConfidenceTests method testEmpty.

public void testEmpty() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    Serializable s = compileExpression("list = new java.util.ArrayList(); list == empty", ctx);
    Map vars = new HashMap();
    Boolean x = (Boolean) executeExpression(s, vars);
    assertNotNull(x);
    assertTrue(x.booleanValue());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with ParserContext

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

the class CoreConfidenceTests method testNestedNumInMapKey.

public void testNestedNumInMapKey() {
    String str = "objectKeyMaptributes[Triangle.Foo.OBTUSE]";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport("Triangle", Triangle.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Person.class);
    pctx.setStrongTyping(true);
    Foo foo = new Foo();
    Person p = new Person();
    Map<Object, Foo> map = new HashMap<Object, Foo>();
    map.put(Triangle.Foo.OBTUSE, foo);
    p.setObjectKeyMaptributes(map);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Object o = MVEL.executeExpression(stmt, p, new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Foo(org.mvel2.tests.core.res.Foo) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 43 with ParserContext

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

the class CoreConfidenceTests method ctxJIRA170.

private ParserContext ctxJIRA170(boolean strictTypeEnforcement, boolean strongTyping) {
    ParserContext ctx = new ParserContext();
    // ctx.setStrictTypeEnforcement(strictTypeEnforcement);
    ctx.setStrongTyping(strongTyping);
    ctx.addInput("x", Collection.class, new Class[] { Integer.class });
    ctx.addInput("y", Integer.class);
    return ctx;
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 44 with ParserContext

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

the class CoreConfidenceTests method testMVEL190.

public void testMVEL190() {
    ParserContext context = new ParserContext();
    context.addImport(Ship.class);
    context.addImport(MapObject.class);
    context.addInput("obj", MapObject.class);
    Object compiled = MVEL.compileExpression("((Ship) obj).getName()", context);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("obj", new Ship());
    VariableResolverFactory varsResolver = new MapVariableResolverFactory(vars);
    System.out.println(executeExpression(compiled, varsResolver));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IndexedVariableResolverFactory(org.mvel2.integration.impl.IndexedVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapObject(org.mvel2.tests.core.res.MapObject) Ship(org.mvel2.tests.core.res.Ship) ParserContext(org.mvel2.ParserContext)

Example 45 with ParserContext

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

the class CoreConfidenceTests method testMethodCallWithSpaces.

public void testMethodCallWithSpaces() {
    String[] str = new String[] { "Foo f = new Foo(); f.setBoolTest( true )   ; f.isBoolTest()", "Foo f = new Foo(); f . setBoolTest( true ) ; f.isBoolTest()", "Foo f = new Foo(); f. setBoolTest( true )  ; f.isBoolTest()", "Foo f = new Foo(); f .setBoolTest( true )  ; f.isBoolTest()", "Foo f = new Foo(); f.boolTest = true   ; f.isBoolTest()", "Foo f = new Foo(); f . boolTest = true ; f.isBoolTest()", "Foo f = new Foo(); f. boolTest = true  ; f.isBoolTest()", "Foo f = new Foo(); f .boolTest = true  ; f.isBoolTest()" };
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", Bar.class);
    pctx.addImport(Foo.class);
    List<String> errors = new ArrayList<String>();
    for (String s : str) {
        try {
            ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(s, pctx);
            Bar ctx = new Bar();
            Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx, new HashMap());
            assertTrue(result);
        } catch (Exception e) {
            e.printStackTrace();
            errors.add("**** Error on expression: " + s + "\n" + e.getMessage());
        }
    }
    assertTrue(errors.toString(), errors.isEmpty());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Bar(org.mvel2.tests.core.res.Bar) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) IOException(java.io.IOException) 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