【发布时间】:2011-05-12 05:45:31
【问题描述】:
我正在创建一个接受 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}"
}
}
有什么方法可以为嵌套闭包设置委托属性?
【问题讨论】:
标签: groovy metaprogramming expandometaclass