3

I am using the JavaScript Engine in Java to evaluate some strings. I want user.group == group1 in JavaScript to evaluate the Java method user.hasGroup(group1). User contains a list of group strings.

I know I can bind functions with the following, but I am not sure how to replace the equality operator and use the right side as a parameter. (jsEngine is my JavaScript engine)

Bindings b = jsEngine.createBindings();
b.put(String name, Object value);   
Engine.setBindings(Bindings bindings, int scope);
spooky655
  • 99
  • 2
  • 10
  • 1
    wow, a question which is legitimately about `[java]` and `[javascript]` ;) – Peter Lawrey Feb 04 '15 at 16:41
  • 1
    I don't believe this is possible. I think the best you could do, in some cases, would be something along these lines: http://stackoverflow.com/a/4700278/636009 but there's nothing you could translate `user.group` and `group1` to that would show them as equivalent under string comparison, is there? – David Conrad Feb 04 '15 at 20:14

1 Answers1

0

You can use jsEngine.eval("if(user.group == group1)"), catch the Boolean object and decide further.

  • Can you elaborate? Are you suggesting that I evaluate the boolean before I define what `user.group` is? – spooky655 Feb 04 '15 at 17:48
  • No, you create the binding with the user data first. I am only saying based on `jsEngine.eval("if(user.group == group1)")` , you take some actions in java or javascript, based on your implementation. – Aninda Bhattacharyya Feb 04 '15 at 18:03
  • I think what I want is not possible, so I opted to use `user.hasGroup(group1)` explicitly. – spooky655 Feb 09 '15 at 18:59