gpt4 book ai didi

java - 是否可以通过 Groovy 中的类别添加 methodMissing Hook ?

转载 作者:行者123 更新时间:2023-11-30 02:18:05 25 4
gpt4 key购买 nike

我想将 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: Nosignature of method: MyClass.nonexisting() is apply for argument types: ()values: []

是否可以向类添加临时 methodMissing

我试图避免弄乱元类,因为该更改将是全局且不可逆的。

最佳答案

根据 2015 年发布的 Groovy in Action,第二版:

Category method names can well take the form of property accessors (pretending property access), operator methods, and GroovyObject methods. MOP hook methods (propertyMissing, methodMissing) cannot be added through a category class. This is a restriction as of Groovy 2.4. The feature may become available in later versions.

所以在2.4.12中不可能做到这一点,

release notes for Groovy 2.5 (2017-11 仍在开发中)不要提及任何与此相关的更改。

release notes for Groovy 2.6也别提了。

最后不幸的是 release notes for Groovy 3.0methodMissing 只字不提

关于java - 是否可以通过 Groovy 中的类别添加 methodMissing Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47680881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com