我正在創建一個接受Groovy閉包作爲標記的構建器。不過,我無法捕捉帶嵌套閉包的方法調用。通過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}"
}
}
有沒有什麼辦法可以設置嵌套閉包的委託屬性?
您的權利只需要一個特殊的「包含」方法調用就容易得多。 – Akusete 2011-05-15 09:47:45