propertyMissing内部类的行为

时间:2016-06-27 14:41:04

标签: groovy

我有这个设施

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方法?

0 个答案:

没有答案