Search in sources :

Example 36 with ParserContext

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

the class CoreConfidenceTests method testUnaryNegative.

public void testUnaryNegative() {
    ParserConfiguration conf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("value", int.class);
    Map vars = new HashMap() {

        {
            put("value", 42);
        }
    };
    assertEquals(-42, MVEL.executeExpression(MVEL.compileExpression("-value", pctx), vars));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 37 with ParserContext

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

the class CoreConfidenceTests method testMapAccessWithNestedProperty.

public void testMapAccessWithNestedProperty() {
    String str = "map[key] == \"one\"";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", POJO.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    POJO ctx = new POJO();
    ctx.getMap().put("1", "one");
    Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
    assertTrue(result);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 38 with ParserContext

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

the class CoreConfidenceTests method testJIRA155.

public void testJIRA155() {
    ParserContext pctx = new ParserContext();
    pctx.addImport("returnTrue", MVEL.getStaticMethod(CoreConfidenceTests.class, "returnTrue", new Class[0]));
    assertEquals(true, executeExpression(MVEL.compileExpression("!true || returnTrue()", pctx)));
}
Also used : TestClass(org.mvel2.tests.core.res.TestClass) PublicClass(org.mvel2.tests.core.res.res2.PublicClass) MyClass(org.mvel2.tests.core.res.MyClass) OverloadedClass(org.mvel2.tests.core.res.res2.OverloadedClass) ParserContext(org.mvel2.ParserContext)

Example 39 with ParserContext

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

the class CoreConfidenceTests method testComaProblemStrikesBack.

public void testComaProblemStrikesBack() {
    String ex = "a.explanation = \"There is a coma, in here\"";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
    Serializable s = compiler.compile();
    Base a = new Base();
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", a);
    executeExpression(s, variables);
    assertEquals("There is a coma, in here", a.data);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) RuleBase(org.mvel2.tests.core.res.RuleBase) Base(org.mvel2.tests.core.res.Base)

Example 40 with ParserContext

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

the class CoreConfidenceTests method testArray.

public void testArray() {
    String ex = " TestHelper.method(1, new String[]{\"a\", \"b\"});\n" + " TestHelper.method(2, new String[]{new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, new Fooz[]{new Fooz(\"a\"), new Fooz(\"b\")});";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(TestHelper.class);
    ctx.addImport(Fooz.class);
    ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
    OptimizerFactory.setDefaultOptimizer("ASM");
    CompiledExpression expr = compiler.compile();
    executeExpression(expr);
    OptimizerFactory.setDefaultOptimizer("reflective");
    expr = compiler.compile();
    executeExpression(expr);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

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