评估Groovy中的赋值表达式 [英] evaluate an assignment expression in Groovy
本文介绍了评估Groovy中的赋值表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表达式,例如:
pre $ def $ exp $ ='anything =something'
在我的情况中,上面的字符串来自Web服务,但它可能来自任何地方。我想对此进行评估,以便最终得到具有某些值的变量anything,例如:
assert anything ==something
到目前为止我试过:
def anything
Eval.me(expr)//`anything`结束为空
我从SO上读到的东西:
def what
Eval.me(expr.inspect())//同样的东西:`anything`结束null
还试过:
def anything
expr.execute()//抱怨:Executable name has嵌入式报价,分割参数
这可以做到吗?
<您可以使用 GroovyShell ,看一看:
解决方案
/ b>
def expr ='anything =something'
def binding = new Binding()
def groovyShell = new G roovyShell(绑定)
groovyShell.evaluate(expr)
assert binding.getVariable('anything')==something
I have an expression such as:
def expr = 'anything="something"'
In my case the above string comes from a web service, but it could be from anywhere. I would like to evaluate this so that I end up with the variable "anything" that has a value of "something", such that:
assert anything=="something"
So far I tried:
def anything
Eval.me(expr) // `anything` ends up null
From stuff I read on SO:
def anything
Eval.me(expr.inspect()) // same thing: `anything` ends up null
Also tried:
def anything
expr.execute() // complains: "Executable name has embedded quote, split the arguments"
Can this be done?
解决方案
You can use GroovyShell, take a look:
def expr = 'anything = "something"'
def binding = new Binding()
def groovyShell = new GroovyShell(binding)
groovyShell.evaluate(expr)
assert binding.getVariable('anything') == "something"
这篇关于评估Groovy中的赋值表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文