Debugging 如何获取有关在groovy中执行脚本的详细信息
我找到了一个我想要的很好的例子: 基本上,我希望能够将字符串作为带有表达式的groovy脚本来执行,但是如果条件为false,我想显示有关为什么将其计算为false的详细信息 编辑 我想要一个实用方法,它的工作原理如下:Debugging 如何获取有关在groovy中执行脚本的详细信息,debugging,groovy,expression,Debugging,Groovy,Expression,我找到了一个我想要的很好的例子: 基本上,我希望能够将字符串作为带有表达式的groovy脚本来执行,但是如果条件为false,我想显示有关为什么将其计算为false的详细信息 编辑 我想要一个实用方法,它的工作原理如下: def expression = "model.book.title == \"The Shining\"" def output = magicMethod(expression) // output.result: the exact result of executi
def expression = "model.book.title == \"The Shining\""
def output = magicMethod(expression)
// output.result: the exact result of executing expression
// output.detail: could be a string telling me why this expression returns true or false, similar to de image
我认为它可能是
Eval.me
+assert
和捕获异常以获取详细信息的组合是的,它与assert一起工作,感谢@Justin Piper的想法
以下是片段:
def model = [model:[book:[title:"The Shinning"]]]
def magicMethod= { String exp ->
def out = [:]
out.result = Eval.x(model,"x.with{${exp}}")
try{
if(out.result){
Eval.x(model,"x.with{!assert ${exp}}")
}else{
Eval.x(model,"x.with{assert ${exp}}")
}
}catch(Throwable e){
out.detail = e.getMessage()
}
return out
}
def expression = "model.book.title == \"The Shining\""
def output = magicMethod(expression)
println "result: ${output.result}"
println "detail: ${output.detail}"
是的,它和assert一起工作,谢谢你的主意@Justin Piper 以下是片段:
def model = [model:[book:[title:"The Shinning"]]]
def magicMethod= { String exp ->
def out = [:]
out.result = Eval.x(model,"x.with{${exp}}")
try{
if(out.result){
Eval.x(model,"x.with{!assert ${exp}}")
}else{
Eval.x(model,"x.with{assert ${exp}}")
}
}catch(Throwable e){
out.detail = e.getMessage()
}
return out
}
def expression = "model.book.title == \"The Shining\""
def output = magicMethod(expression)
println "result: ${output.result}"
println "detail: ${output.detail}"
因此,您希望通过
assert
关键字生成消息,但不能仅使用assert
?因此您希望通过assert
关键字生成消息,但不能仅使用assert
?