Search in sources :

Example 16 with ParserConfiguration

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

the class ArithmeticTests method testMathCeilWithDoubleCast.

public void testMathCeilWithDoubleCast() {
    String str = "Math.ceil( (double) x / 3 )";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport("Math", Math.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("x", Integer.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));
}
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 17 with ParserConfiguration

use of org.mule.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 18 with ParserConfiguration

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

Example 19 with ParserConfiguration

use of org.mule.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 20 with ParserConfiguration

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

the class AsmOptimizerOsgiTest method testCollectionAccessWithInvalidThreadClassLoader.

public void testCollectionAccessWithInvalidThreadClassLoader() {
    String expression = "['A', 'B', 'C'] contains 'B'";
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.setClassLoader(MVEL.class.getClassLoader());
    Serializable compiledExpression = MVEL.compileExpression(expression, new ParserContext(parserConfiguration));
    ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(NO_MVEL_CL);
        for (int i = 0; i <= DynamicOptimizer.tenuringThreshold; i++) {
            Object result = MVEL.executeExpression(compiledExpression);
            assertEquals(Boolean.TRUE, result);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(currentCl);
    }
}
Also used : Serializable(java.io.Serializable) MVEL(org.mvel2.MVEL) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)74 ParserContext (org.mvel2.ParserContext)70 HashMap (java.util.HashMap)39 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)32 LinkedHashMap (java.util.LinkedHashMap)29 Map (java.util.Map)23 Serializable (java.io.Serializable)10 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)7 CompileException (org.mvel2.CompileException)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MapObject (org.mvel2.tests.core.res.MapObject)5 Method (java.lang.reflect.Method)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 CachedMapVariableResolverFactory (org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory)3 MVELExpressionExecutor (org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor)3 Bar (org.mvel2.tests.core.res.Bar)3