Converting inserted string to a compilable expression

后端 未结 5 1811
醉梦人生
醉梦人生 2021-01-23 07:45

I\'m somehow new to Java so my question can seem trivial, however i cannot find an answer to it anywhere in my books.

I want to initiatie a dialog with a user in which h

5条回答
  •  [愿得一人]
    2021-01-23 08:07

    There is no native facility in Java for evaluating strings as expressions. You must therefore do the work yourself.

    If you just want a quick and dirty solution, Javassist includes a snippet compiler allowing you to define a new class with your expression, and invoke it to get your result, after which you can discard it (i.e. run it in a separate classloader). It will require some work though :)

    http://www.jboss.org/javassist/

提交回复
热议问题