Search in sources :

Example 41 with ParserContext

use of org.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 42 with ParserContext

use of org.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 43 with ParserContext

use of org.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 44 with ParserContext

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

Example 45 with ParserContext

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

the class CoreConfidenceTests method testDeepMethodNameResolution.

public void testDeepMethodNameResolution() {
    String expression = "variables = [ \"symbol\" : \"RHT\" ]; \n" + "drools.getProcessManager().startProcess(\"id\", variables );";
    // third pass
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("drools", KnowledgeRuntimeHelper.class);
    Map vars = new HashMap();
    vars.put("drools", new KnowledgeRuntimeHelper());
    Serializable expr = MVEL.compileExpression(expression, ctx);
    executeExpression(expr, vars);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

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