Search in sources :

Example 11 with CompiledExpression

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

the class TypesAndInferenceTests method testDetermineEgressParametricType.

public final void testDetermineEgressParametricType() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("strings", List.class, new Class[] { String.class });
    final CompiledExpression expr = new ExpressionCompiler("strings").compile(parserContext);
    assertTrue(STRINGS.equals(executeExpression(expr, new A())));
    final Type[] typeParameters = expr.getParserContext().getLastTypeParameters();
    assertTrue(typeParameters != null);
    assertTrue(String.class.equals(typeParameters[0]));
}
Also used : Type(java.lang.reflect.Type) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 12 with CompiledExpression

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

the class DebuggerTests method testBreakpointsAcrossComments2.

public void testBreakpointsAcrossComments2() {
    ExpressionCompiler compiler = new ExpressionCompiler(// 1
    "// This is a comment\n" + // 2
    "//Second comment line\n" + // 3
    "//Third Comment Line\n" + // 4
    "\n" + // 5
    "//Test\n" + // 6
    "System.out.println('4');\n" + // 7
    "//System.out.println('5'); \n" + // 8
    "a = 0;\n" + // 9
    "b = 1;\n" + // 10
    " a + b");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    MVELRuntime.registerBreakpoint("test2.mv", 6);
    MVELRuntime.registerBreakpoint("test2.mv", 8);
    MVELRuntime.registerBreakpoint("test2.mv", 9);
    MVELRuntime.registerBreakpoint("test2.mv", 10);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals(1, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertEquals("did not break at expected lines", Make.Set.<Integer>$()._(6)._(8)._(9)._(10)._(), breaked);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) HashSet(java.util.HashSet)

Example 13 with CompiledExpression

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

the class FunctionsTest method testFunctionDefAndCall2.

public void testFunctionDefAndCall2() {
    ExpressionCompiler compiler = new ExpressionCompiler("function heyFoo() { return 'Foobar'; };\n" + "return heyFoo() + heyFoo();");
    Serializable s = compiler.compile();
    Map<String, Function> m = extractAllDeclaredFunctions((CompiledExpression) s);
    assertTrue(m.containsKey("heyFoo"));
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    OptimizerFactory.setDefaultOptimizer("dynamic");
}
Also used : Function(org.mvel2.ast.Function) Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 14 with CompiledExpression

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

the class WithTests method testInlineWith2.

public void testInlineWith2() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}}").compile();
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
    assertEquals("foobie", f.getBar().getName());
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 15 with CompiledExpression

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

the class TypesAndInferenceTests method testDetermineEgressParametricType2.

public final void testDetermineEgressParametricType2() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("strings", List.class, new Class[] { String.class });
    final CompiledExpression expr = new ExpressionCompiler("strings", parserContext).compile();
    assertTrue(STRINGS.equals(executeExpression(expr, new A())));
    final Type[] typeParameters = expr.getParserContext().getLastTypeParameters();
    assertTrue(null != typeParameters);
    assertTrue(String.class.equals(typeParameters[0]));
}
Also used : Type(java.lang.reflect.Type) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

CompiledExpression (org.mvel2.compiler.CompiledExpression)38 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)34 ParserContext (org.mvel2.ParserContext)21 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)10 Foo (org.mvel2.tests.core.res.Foo)10 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 ASTNode (org.mvel2.ast.ASTNode)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 Serializable (java.io.Serializable)3 Interceptor (org.mvel2.integration.Interceptor)3 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)3 Type (java.lang.reflect.Type)2 Macro (org.mvel2.Macro)2 WithNode (org.mvel2.ast.WithNode)2 Base (org.mvel2.tests.core.res.Base)2 ASTIterator (org.mvel2.util.ASTIterator)2 ASTLinkedList (org.mvel2.util.ASTLinkedList)2