Search in sources :

Example 16 with CompiledExpression

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

the class TypesAndInferenceTests method testMVEL236.

public void testMVEL236() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("MyInterface2 var2 = (MyInterface2)var1;");
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("var1", MyInterface3.class);
    ctx.addImport(MyInterface2.class);
    ctx.addImport(MyInterface3.class);
    try {
        CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 17 with CompiledExpression

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

the class CoreConfidenceTests method testJIRA168.

public void testJIRA168() {
    boolean before = MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL;
    try {
        Map<String, Object> st = new HashMap<String, Object>();
        st.put("__fact__", new ArrayList());
        st.put("__expected__", 0);
        String expressionNaked = "__fact__.size == __expected__";
        String expressionNonNaked = "__fact__.size() == __expected__";
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        // the following works fine
        ParserContext ctx = new ParserContext();
        for (Map.Entry<String, Object> entry : st.entrySet()) {
            ctx.addInput(entry.getKey(), entry.getValue().getClass());
        }
        CompiledExpression expr = new ExpressionCompiler(expressionNaked).compile(ctx);
        Boolean result = (Boolean) executeExpression(expr, st);
        assertTrue(result);
        // the following works fine
        result = (Boolean) MVEL.eval(expressionNonNaked, st);
        assertTrue(result);
        // the following fails
        result = (Boolean) MVEL.eval(expressionNaked, st);
        assertTrue(result);
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = before;
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 18 with CompiledExpression

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

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);
    OptimizerFactory.setDefaultOptimizer("ASM");
    CompiledExpression expr = compiler.compile(ctx);
    executeExpression(expr);
    OptimizerFactory.setDefaultOptimizer("reflective");
    expr = compiler.compile(ctx);
    executeExpression(expr);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 19 with CompiledExpression

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

the class DebuggerTests method testBreakpoints4.

public void testBreakpoints4() {
    String expression = "System.out.println('foo');\n" + "a = new Foo244();\n" + "update (a) { name = 'bar' };\n" + "System.out.println('name:' + a.name);\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    class TestResult {

        boolean firedBefore;

        boolean firedAfter;
    }
    final TestResult result = new TestResult();
    interceptors.put("Update", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            System.out.println("fired update interceptor -- before");
            result.firedBefore = true;
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            System.out.println("fired update interceptor -- after");
            result.firedAfter = true;
            return 0;
        }
    });
    macros.put("update", new Macro() {

        public String doMacro() {
            return "@Update with";
        }
    });
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setDebugSymbols(true);
    ctx.setSourceFile("test2.mv");
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    MVELRuntime.registerBreakpoint("test2.mv", 3);
    MVELRuntime.registerBreakpoint("test2.mv", 4);
    MVELRuntime.registerBreakpoint("test2.mv", 5);
    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("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not fire before", result.firedBefore);
    assertTrue("did not fire after", result.firedAfter);
    assertEquals("did not break at expected points", Make.Set.<Integer>$()._(3)._(4)._(5)._(), breaked);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor) HashSet(java.util.HashSet)

Example 20 with CompiledExpression

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

the class GenericsTypeInferenceTest method testInferLastTypeParametersFromMethod.

public final void testInferLastTypeParametersFromMethod() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("a", A.class);
    final CompiledExpression compiledExpression = new ExpressionCompiler("a.values()").compile(context);
    final Object val = MVEL.executeExpression(compiledExpression, new AWrapper());
    assertTrue("Expression did not evaluate correctly: " + val, STRINGS.equals(val));
    assertTrue("No type parameters detected", null != context.getLastTypeParameters());
    assertTrue("Wrong parametric type inferred", String.class.equals(context.getLastTypeParameters()[0]));
}
Also used : ParserContext(org.mvel2.ParserContext)

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