Search in sources :

Example 61 with ParserContext

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

Example 62 with ParserContext

use of org.mule.mvel2.ParserContext 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 63 with ParserContext

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

the class CoreConfidenceTests method testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression.

/**
 * Provided by: Phillipe Ombredanne
 */
public void testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression() {
    String expr = // 
    "		System.out.println( message );\n" + // 
    "m.setMessage( \"Goodbye cruel world\" );\n" + // 
    "System.out.println(m.getStatus());\n" + "m.setStatus( Message.GOODBYE );\n";
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(false);
    context.addImport("Message", Message.class);
    context.addInput("System", void.class);
    context.addInput("message", Object.class);
    context.addInput("m", Object.class);
    ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
    compiler.compile();
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 64 with ParserContext

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

the class CoreConfidenceTests method testMapsAndLists.

public void testMapsAndLists() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(HashMap.class);
    ctx.addImport(ArrayList.class);
    ctx.addInput("list", List.class);
    String expression = "m = new HashMap();\n" + "l = new ArrayList();\n" + "l.add(\"first\");\n" + "m.put(\"content\", l);\n" + "list.add(((ArrayList)m[\"content\"])[0]);";
    Serializable s = compileExpression(expression, ctx);
    Map vars = new HashMap();
    List list = new ArrayList();
    vars.put("list", list);
    Boolean result = (Boolean) executeExpression(s, vars);
    assertNotNull(result);
    assertTrue(result);
    assertEquals(1, list.size());
    assertEquals("first", list.get(0));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 65 with ParserContext

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

the class CoreConfidenceTests method testWhileUsingImports.

public void testWhileUsingImports() {
    Map<String, Object> imports = new HashMap<String, Object>();
    imports.put("ArrayList", java.util.ArrayList.class);
    imports.put("List", java.util.List.class);
    ParserContext context = new ParserContext(imports, null, "testfile");
    ExpressionCompiler compiler = new ExpressionCompiler("List list = new ArrayList(); return (list == empty)", context);
    assertTrue((Boolean) executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory()));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) MapObject(org.mvel2.tests.core.res.MapObject) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10