Groovy 2.4.3 中的闭包解析策略在特征上下文中不起作用
Closures resolving strategy not working in trait context in Groovy 2.4.3
我有以下代码:
trait ContainingClosure {
def method() {
def delegateClass = new DelegateClass()
def closure = {
methodFromDelegate()
}
closure.delegate = delegateClass
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()
}
}
class DelegateClass {
def methodFromDelegate() {
println 'methodFromDelegate called'
}
}
class Main implements ContainingClosure {}
new Main().method()
问题是我运行 call()
方法时找不到methodFromDelegate()
,抛出如下异常:
groovy.lang.MissingMethodException: No signature of method: Main.methodFromDelegate() is applicable for argument types: () values: []
是否有任何合理的解释为什么此代码段在 Grails 2.5.0 (Groovy 2.4.3) 中不起作用?似乎以某种方式忽略了闭包的委托,方法查找是在 Main class 范围内完成的,而不是委托本身。
将 trait
更改为 class
并将特征实现为继承使此代码再次运行。
找到了,就是这个bug
https://issues.apache.org/jira/browse/GROOVY-7456
已在 groovy 2.4.4 中修复,因此升级 grails 应该会修复它:-)
我有以下代码:
trait ContainingClosure {
def method() {
def delegateClass = new DelegateClass()
def closure = {
methodFromDelegate()
}
closure.delegate = delegateClass
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()
}
}
class DelegateClass {
def methodFromDelegate() {
println 'methodFromDelegate called'
}
}
class Main implements ContainingClosure {}
new Main().method()
问题是我运行 call()
方法时找不到methodFromDelegate()
,抛出如下异常:
groovy.lang.MissingMethodException: No signature of method: Main.methodFromDelegate() is applicable for argument types: () values: []
是否有任何合理的解释为什么此代码段在 Grails 2.5.0 (Groovy 2.4.3) 中不起作用?似乎以某种方式忽略了闭包的委托,方法查找是在 Main class 范围内完成的,而不是委托本身。
将 trait
更改为 class
并将特征实现为继承使此代码再次运行。
找到了,就是这个bug
https://issues.apache.org/jira/browse/GROOVY-7456
已在 groovy 2.4.4 中修复,因此升级 grails 应该会修复它:-)