Equivalent of eval() in Groovy

后端 未结 3 746
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 08:51

In python, I can use eval() to execute user entered code in my program. Is there anything similar I can do in Groovy? I want to have a SWING UI textbox where the user enters

3条回答
  •  伪装坚强ぢ
    2021-01-04 09:29

    Yes, it is possible to dynamically evaluate code in Groovy by using Eval.x, Eval.xy, Eval.xyz or Eval.me. See the API doc for more details about these methods.

    For example, you use Eval.me like this:

    def a = "hello"
    def b = "world" 
    Eval.me(""" println "$a $b" """)
    --> hello world
    

    Also, see this blog post for some eval examples

提交回复
热议问题