Search in sources :

Example 26 with ExecutableStatement

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

the class CoreConfidenceTests method testContextObjMethodCall.

public void testContextObjMethodCall() {
    String str = "getName() == \"bob\"";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", Bar.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Bar ctx = new Bar();
    ctx.setName("bob");
    Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
    assertTrue(result);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 27 with ExecutableStatement

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

the class ArithmeticTests method testModExpr.

public void testModExpr() {
    String str = "$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("$y", int.class);
    Map<String, Object> vars = new HashMap<String, Object>();
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    for (int i = 0; i < 500; i++) {
        int y = i;
        boolean expected = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
        vars.put("$y", y);
        assertEquals(expected, MVEL.eval(str, vars));
        assertEquals(expected, ((Boolean) MVEL.executeExpression(stmt, null, vars)).booleanValue());
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 28 with ExecutableStatement

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

the class CoreConfidenceTests method testStrTriangleEqualsEquals.

public void testStrTriangleEqualsEquals() {
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    try {
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.addInput("this", Triangle.class);
        pctx.setStrongTyping(true);
        String str = "this.strLabel == this";
        try {
            ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
            fail("should have failed");
        } catch (CompileException e) {
            System.out.println();
            return;
        }
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 29 with ExecutableStatement

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

the class CoreConfidenceTests method testStrictModeAddAll.

public void testStrictModeAddAll() {
    String str = "list.addAll( o );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("o", Object.class);
    pctx.addInput("list", ArrayList.class);
    try {
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
        fail("This should not compileShared, as o is not of a type Collection");
    } catch (Exception e) {
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) IOException(java.io.IOException)

Example 30 with ExecutableStatement

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

the class TypesAndInferenceTests method testForLoopTypeCoercion.

public void testForLoopTypeCoercion() {
    ParserContext pCtx = ParserContext.create();
    pCtx.setStrongTyping(true);
    pCtx.addInput("$type", String.class);
    pCtx.addInput("l", List.class);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("$type", "pc!!");
    List list = new ArrayList();
    vars.put("l", list);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("for (byte bt:$type.getBytes()) {l.add( bt);}", pCtx);
    MVEL.executeExpression(stmt, null, vars);
    byte[] exp = "pc!!".getBytes();
    for (int i = 0; i < exp.length; i++) {
        assertEquals(exp[i], list.get(i));
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Aggregations

ExecutableStatement (org.mvel2.compiler.ExecutableStatement)25 IOException (java.io.IOException)6 ParserContext (org.mvel2.ParserContext)6 List (java.util.List)5 Map (java.util.Map)5 CompileException (org.mvel2.CompileException)4 TypeDescriptor (org.mvel2.ast.TypeDescriptor)4 ArrayList (java.util.ArrayList)3 ParserConfiguration (org.mvel2.ParserConfiguration)3 WeakHashMap (java.util.WeakHashMap)2 Union (org.mvel2.optimizers.impl.refl.nodes.Union)2 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)2 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Label (org.mvel2.asm.Label)1 MethodVisitor (org.mvel2.asm.MethodVisitor)1 EndOfStatement (org.mvel2.ast.EndOfStatement)1 Function (org.mvel2.ast.Function)1 Proto (org.mvel2.ast.Proto)1