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);
2
  • 1
    wow, a question which is legitimately about [java] and [javascript] ;) Commented Feb 4, 2015 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: 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? Commented Feb 4, 2015 at 20:14

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can you elaborate? Are you suggesting that I evaluate the boolean before I define what user.group is?
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.
I think what I want is not possible, so I opted to use user.hasGroup(group1) explicitly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.