Search in sources :

Example 71 with ParserContext

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

the class TypesAndInferenceTests method testStrongTyping2.

public void testStrongTyping2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("blah", String.class);
    try {
        new ExpressionCompiler("1-blah").compile(ctx);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    assertTrue(false);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 72 with ParserContext

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

the class TypesAndInferenceTests method testMVEL234.

public void testMVEL234() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("import java.text.SimpleDateFormat;");
    buffer.append("if (\"test\".matches(\"[0-9]\")) {");
    buffer.append("  return false;");
    buffer.append("}else{");
    buffer.append("  SimpleDateFormat sqf = new SimpleDateFormat(\"yyyyMMdd\");");
    buffer.append("}");
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    try {
        CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 73 with ParserContext

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

the class TypesAndInferenceTests method testMapWithStrictTyping.

public void testMapWithStrictTyping() {
    ExpressionCompiler compiler = new ExpressionCompiler("map['KEY1'] == $msg");
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addInput("$msg", String.class);
    ctx.addInput("map", Map.class);
    Serializable expr = compiler.compile(ctx);
    Map map = new HashMap();
    map.put("KEY1", "MSGONE");
    Map vars = new HashMap();
    vars.put("$msg", "MSGONE");
    vars.put("map", map);
    Boolean bool = (Boolean) executeExpression(expr, map, vars);
    assertEquals(Boolean.TRUE, bool);
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 74 with ParserContext

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

the class TypesAndInferenceTests method testMVEL228.

public void testMVEL228() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    HashMap<String, Class> params = new HashMap<String, Class>();
    params.put("helper", ScriptHelper228.class);
    params.put("person", Person228.class);
    ctx.setInputs(params);
    String script = "helper.methodB(2);\n" + "person.getName2();";
    try {
        CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(script, ctx);
    } catch (Exception e) {
        return;
    }
    fail("Should have thrown an exception");
}
Also used : CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 75 with ParserContext

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

the class TypesAndInferenceTests method testGenericMethods.

public void testGenericMethods() {
    String str = "Integer.parseInt( a.getMap().get(\"x\") )";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("a", AGenericTestClass.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    AGenericTestClass a = new AGenericTestClass();
    a.setMap(new HashMap<String, String>());
    a.getMap().put("x", "10");
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", a);
    Number result = (Number) MVEL.executeExpression(stmt, null, variables);
    assertEquals(10, result.intValue());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Aggregations

ParserContext (org.mvel2.ParserContext)372 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)190 HashMap (java.util.HashMap)140 Serializable (java.io.Serializable)100 ParserConfiguration (org.mvel2.ParserConfiguration)86 Map (java.util.Map)74 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)67 LinkedHashMap (java.util.LinkedHashMap)66 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 CompileException (org.mvel2.CompileException)53 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)31 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)24 MapObject (org.mvel2.tests.core.res.MapObject)23 ArrayList (java.util.ArrayList)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)19 IOException (java.io.IOException)16 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)16 Debugger (org.mvel2.debug.Debugger)16