Equivalent of eval() in Groovy

后端 未结 3 776
隐瞒了意图╮
隐瞒了意图╮ 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:28

    There are multiple ways of running Groovy (from both inside Java and Groovy):

    http://groovy-lang.org/integrating.html

    The quickest method however (for simple scripts), is probably to use the Eval class:

    http://groovy-lang.org/integrating.html#integ-eval

    Which would let you do something like:

    Eval.me( '2 + 2' )
    

    See this page from more examples:

    http://mrhaki.blogspot.com/2009/11/groovy-goodness-simple-evaluation-of.html

提交回复
热议问题