Search in sources :

Example 21 with ParserContext

use of org.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 22 with ParserContext

use of org.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 23 with ParserContext

use of org.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 24 with ParserContext

use of org.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)

Example 25 with ParserContext

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

the class CoreConfidenceTests method testSetterViaMapNotation.

public void testSetterViaMapNotation() {
    TestClass tc = new TestClass();
    tc.getExtra().put("test", "value");
    ParserContext ctx = new ParserContext();
    ctx.withInput("this", TestClass.class);
    ctx.setStrongTyping(true);
    String expression = "extra[\"test\"]";
    Serializable compiled = MVEL.compileSetExpression(expression, tc.getClass(), ctx);
    MVEL.executeSetExpression(compiled, tc, "value3");
    assertEquals("value3", tc.getExtra().get("test"));
}
Also used : Serializable(java.io.Serializable) TestClass(org.mvel2.tests.core.res.TestClass) ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)340 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)189 HashMap (java.util.HashMap)132 Serializable (java.io.Serializable)92 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)68 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 LinkedHashMap (java.util.LinkedHashMap)62 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)60 CompileException (org.mvel2.CompileException)51 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)28 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 ArrayList (java.util.ArrayList)22 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)21 MapObject (org.mvel2.tests.core.res.MapObject)19 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)14 IOException (java.io.IOException)13