1

Lets assume a, b are integers, and pets is of type HashMap<String, Integer>

When I pre-compile the expression below, the pets.containsKey(\"Dogs\") node is a null inside the CompiledExpression object.

CompiledExpression compiledExpression = new ExpressionCompiler("a > 0 && b > 0 && pets.containsKey(\"Dogs\")").compile();
return (boolean) MVEL.executeExpression(compiledExpression, params);

However, when I do something like

boolean res = (boolean) MVEL.eval("a > 0 && b > 0 && pets.containsKey(\"Dogs\")", params);

It works just fine and I get the appropriate response back.

Is there any way to precompile an expression that contains an object like a hashmap?

Ramie
  • 1,171
  • 2
  • 16
  • 35

1 Answers1

1

I implemented below both works the same way,

    Map<String, String> pets = new HashMap<>();
    pets.put("dog", "DOG");
    pets.put("cat", "CAT");
    Integer a = 10;
    Integer b = 20;

    Map<String, Object> params = new HashMap<>();
    params.put("$a", a);
    params.put("$b", b);
    params.put("$map", pets);
    params.put("$key", "dog");

    CompiledExpression expression = new ExpressionCompiler("$a > 0 && $b > 0 && $map.containsKey($key)").compile();
    System.out.println(MVEL.executeExpression(expression, params));
    System.out.println(MVEL.eval("$a > 0 && $b > 0 && $map.containsKey($key)", params));
Saravana
  • 12,647
  • 2
  • 39
  • 57