我正在创建一个接受 Groovy 闭包作为标记的构建器。但是,我无法使用嵌套闭包捕获方法调用。
Closure nested = {
foo () //will throw missingMethod exception
}
Closure root = {
foo () //prints 'missing foo []'
inline_nested {
foo () //prints 'missing foo []'
}
nested ()
}
builder.execute (root)
// ...
class MyBuilder {
void execute (Closure closure) {
def clone = closure.clone()
clone.delegate = this
clone()
}
def missingMethod (String name, args) {
println "missing ${name} ${args}"
}
}
有什么方法可以为嵌套闭包设置委托属性?