【发布时间】:2017-12-06 18:13:30
【问题描述】:
我想将methodMissing() 添加到我没有源代码控制权的类中。我试过了:
class MyClass {} // class I'm not allowed to edit
class MyCategory {
static def methodMissing(MyClass self, String name, Object args) {
"I was hoping this was called on a.nonexisting()"
}
static def test(MyClass self) {
"test works"
}
}
def a = new MyClass()
use(MyCategory) {
println a.test()
println a.nonexisting()
}
但我仍然收到groovy.lang.MissingMethodException: No signature of method: MyClass.nonexisting() is applicable for argument types: () values: []
是否可以在课程中添加临时 methodMissing?
我试图避免与元类混淆,因为该更改将是全局且不可逆的。
【问题讨论】: