Search in sources :

Example 11 with ParserConfiguration

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

the class CoreConfidenceTests method testInstanceofWithPackageImport.

public void testInstanceofWithPackageImport() {
    ParserConfiguration conf = new ParserConfiguration();
    conf.addPackageImport("org.mvel2.tests.core");
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("value", Object.class);
    Map vars = new HashMap() {

        {
            put("value", new CoreConfidenceTests());
        }
    };
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof CoreConfidenceTests", pctx), vars));
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof " + CoreConfidenceTests.class.getCanonicalName(), 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 12 with ParserConfiguration

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

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

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

the class CoreConfidenceTests method testNestedEnumFromJar.

public void testNestedEnumFromJar() throws ClassNotFoundException, SecurityException, NoSuchFieldException {
    String expr = "EventRequest.Status.ACTIVE";
    // creating a classloader for the jar
    URL resource = getClass().getResource("/eventing-example.jar");
    assertNotNull(resource);
    URLClassLoader loader = new URLClassLoader(new URL[] { resource }, getClass().getClassLoader());
    // loading the class to prove it works
    Class<?> er = loader.loadClass("org.drools.examples.eventing.EventRequest");
    assertNotNull(er);
    assertEquals("org.drools.examples.eventing.EventRequest", er.getCanonicalName());
    // getting the value of the enum to prove it works:
    Class<?> st = er.getDeclaredClasses()[0];
    assertNotNull(st);
    Field active = st.getField("ACTIVE");
    assertNotNull(active);
    // now, trying with MVEL
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.setClassLoader(loader);
    pconf.addImport(er);
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    Serializable compiled = MVEL.compileExpression(expr, pctx);
    Object result = MVEL.executeExpression(compiled);
    assertNotNull(result);
}
Also used : Field(java.lang.reflect.Field) Serializable(java.io.Serializable) URLClassLoader(java.net.URLClassLoader) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) URL(java.net.URL) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 15 with ParserConfiguration

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

the class CoreConfidenceTests method testStaticMethodInvocation.

public void testStaticMethodInvocation() {
    ParserConfiguration conf = new ParserConfiguration();
    conf.addImport(ARef.class);
    conf.addImport(BRef.class);
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("value", String.class);
    Map vars = new HashMap() {

        {
            put("value", "1234");
        }
    };
    assertEquals(0, MVEL.executeExpression(MVEL.compileExpression("ARef.getSize(value)", pctx), vars));
    assertEquals(4, MVEL.executeExpression(MVEL.compileExpression("BRef.getSize(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)

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