Search in sources :

Example 61 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class CoreConfidenceTests method testStaticNestedWithMethodCall.

public void testStaticNestedWithMethodCall() {
    String expr = "item = new Item( \"Some Item\"); $msg.addItem( item ); return $msg";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(false);
    context.addImport("Message", Message.class);
    context.addImport("Item", Item.class);
    // Serializable compiledExpression = compiler.compileShared(context);
    Map vars = new HashMap();
    vars.put("$msg", new Message());
    Message msg = (Message) executeExpression(compiler.compile(context), vars);
    Item item = (Item) msg.getItems().get(0);
    assertEquals("Some Item", item.getName());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 62 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class TypesAndInferenceTests method testStrictTypingCompilation2.

public void testStrictTypingCompilation2() throws Exception {
    ParserContext ctx = new ParserContext();
    // noinspection RedundantArrayCreation
    ctx.addImport("getRuntime", new MethodStub(Runtime.class.getMethod("getRuntime", new Class[] {})));
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler("getRuntime()");
    StaticMethodImportResolverFactory si = new StaticMethodImportResolverFactory(ctx);
    Serializable expression = compiler.compile(ctx);
    serializationTest(expression);
    assertTrue(executeExpression(expression, si) instanceof Runtime);
}
Also used : Serializable(java.io.Serializable) MethodStub(org.mvel2.util.MethodStub) StaticMethodImportResolverFactory(org.mvel2.integration.impl.StaticMethodImportResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 63 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class GenericsTypeInferenceTest method testTypeByMethod.

public final void testTypeByMethod() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("a", A.class);
    CompiledExpression compiledExpression = new ExpressionCompiler("!a.show").compile(context);
    assertEquals(Boolean.class, compiledExpression.getKnownEgressType());
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 64 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class CoreConfidenceTests method testIncrementAndAssignWithInputs.

public void testIncrementAndAssignWithInputs() {
    ExpressionCompiler compiler = new ExpressionCompiler("total += cheese");
    compiler.compile();
    assertTrue(compiler.getParserContextState().getInputs().containsKey("total"));
    assertTrue(compiler.getParserContextState().getInputs().containsKey("cheese"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 65 with ExpressionCompiler

use of org.mvel2.compiler.ExpressionCompiler in project mvel by mikebrock.

the class CoreConfidenceTests method testCompileWithNewInsideMethodCall.

public void testCompileWithNewInsideMethodCall() {
    String expr = "     p.name = \"goober\";\n" + "     System.out.println(p.name);\n" + "     drools.insert(new Address(\"Latona\"));\n";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(false);
    context.addImport("Person", Person.class);
    context.addImport("Address", Address.class);
    context.addInput("p", Person.class);
    context.addInput("drools", Drools.class);
    compiler.compile(context);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)110 CompiledExpression (org.mvel2.compiler.CompiledExpression)33 ParserContext (org.mvel2.ParserContext)31 Serializable (java.io.Serializable)15 Foo (org.mvel2.tests.core.res.Foo)12 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)11 HashMap (java.util.HashMap)9 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)9 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashSet (java.util.HashSet)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)5 List (java.util.List)4 Interceptor (org.mvel2.integration.Interceptor)4 Map (java.util.Map)3 ASTNode (org.mvel2.ast.ASTNode)3 WithNode (org.mvel2.ast.WithNode)3 Type (java.lang.reflect.Type)2 LinkedHashMap (java.util.LinkedHashMap)2 Macro (org.mvel2.Macro)2