开发者

Resolving tokens in Beanshell

I want to use beanshell (bsh) to evaluate user defined expressions. I want to allow those expressions to have a kind of macro substitution for example:

boolean isTrue = i.eval("@something == 5");

Is there a way to have bsh call back to my java for substitution of variables when it parses the above example before evaluation?

EDIT My intent is to allow a user to specify the expression (e.g. @something == 5) where @something is a key and requires a substitution to the actual value. I want开发者_Go百科 to be able to pass in the whole evaluation string as stated by the user and would like beanshell to call back to a 'resolver' type handler during the parse.

Thanks in advance -- Frank


No, there is no way to achive this in beanshell. You could post a feature request for this at http://code.google.com/p/beanshell2

But perhaps you give some more insights in what you try to achive..?


A really old question, but here's my answer:

  1. Set your resolver as a varible on the Interpreter

Interpreter i = new Interpreter(); i.set("resolver", myResolver);

  1. Process tokens

Pre-process user input for the pattern @something into a regex, or whatever, in order to render your script token as:

resolver.resolve("@something") == 5
  1. Execute script (I left out \" for clarity since this will be a variable)

i.eval("resolver.resolve("@something") == 5")

The resolve method can return an object; Beanshell loose-typing and auto-boxing should take care of stuff. The issue I see is things like String.equals(), for example, since that requires more than just token subsitution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜