我有这个设施
def application = new Application()
application {
onClose {
dispose
}
}
类似于此的分辨率
class Application {
def call(Closure cl) {
cl.delegate = this
cl.setResolveStrategy Closure.DELEGATE_ONLY
cl()
}
def onClose(Closure cl) {
def closeActions = new CloseActions()
cl.delegate = closeActions
cl.setResolveStrategy Closure.DELEGATE_ONLY
cl()
}
class CloseActions {
}
}
当我运行此代码时,我得到MissingPropertyException
groovy.lang.MissingPropertyException: No such property: dispose for class: Application
找不到类Application
内的属性!虽然我在CloseActions
内部找不到它,因为我使用onClose
属性将CloseActions
闭包的内容委托给delegate
类。
当我将属性dispose
添加到CloseActions
类
class CloseActions {
def dispose
}
它不会抛出异常。
当我将propertyMissing
添加到CloseActions
类
class CloseActions {
def propertyMissing(String name) {
println "Missing property: $name"
}
}
它仍然会抛出
groovy.lang.MissingPropertyException: No such property: dispose for class: Application
但是当我将CloseActions
课程移到Application
之外时 - 它运作正常!
为什么它不为内部类调用propertyMissing
方法?