Search in sources :

Example 51 with ParserConfiguration

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

the class ArithmeticTests method testIntsWithDivision.

public void testIntsWithDivision() {
    String str = "0 == x - (y/2)";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("x", int.class);
    pctx.addInput("y", int.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map vars = new HashMap();
    vars.put("x", 50);
    vars.put("y", 100);
    Boolean result = (Boolean) MVEL.executeExpression(stmt, vars);
    assertTrue(result);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 52 with ParserConfiguration

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

the class ArithmeticTests method testStaticMathCeilWithJavaClassStyleLiterals.

public void testStaticMathCeilWithJavaClassStyleLiterals() {
    MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
    try {
        String str = "java.lang.Math.ceil( x/3.0 )";
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.setStrongTyping(true);
        pctx.addInput("x", int.class);
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
        Map vars = new HashMap();
        vars.put("x", 4);
        assertEquals(Math.ceil((double) 4 / 3), MVEL.executeExpression(stmt, vars));
    } finally {
        MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = false;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 53 with ParserConfiguration

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

the class TypesAndInferenceTests method testGenerics1.

public void testGenerics1() {
    String str = "addresses[0] == new Address(\"s1\") && addresses[0].street == new Address(\"s1\").street";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", PersonAddresses.class);
    pctx.addImport(Address.class);
    pctx.addImport(PersonAddresses.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    PersonAddresses ctx = new PersonAddresses();
    ctx.getAddresses().add(new Address("s1"));
    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 54 with ParserConfiguration

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

the class TypesAndInferenceTests method testStaticMethodsInInputsBug.

public void testStaticMethodsInInputsBug() {
    String text = " getList( java.util.Formatter )";
    ParserConfiguration pconf = new ParserConfiguration();
    for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) {
        if (Modifier.isStatic(m.getModifiers())) {
            pconf.addImport(m.getName(), m);
        }
    }
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(false);
    pctx.setStrongTyping(false);
    Map<String, Object> vars = new HashMap<String, Object>();
    Serializable expr = MVEL.compileExpression(text, pctx);
    List list = (List) MVEL.executeExpression(expr, null, vars);
    assertEquals(Formatter.class, list.get(0));
    assertEquals(0, pctx.getInputs().size());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 55 with ParserConfiguration

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

the class TypesAndInferenceTests method testStaticMethodCallThrowsException.

public void testStaticMethodCallThrowsException() {
    String text = " ( throwException( ) ) ";
    ParserConfiguration pconf = new ParserConfiguration();
    for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) {
        if (Modifier.isStatic(m.getModifiers())) {
            pconf.addImport(m.getName(), m);
        }
    }
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    Map<String, Object> vars = new HashMap<String, Object>();
    Serializable expr = MVEL.compileExpression(text, pctx);
    try {
        MVEL.executeExpression(expr);
        fail("this should throw an exception");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3