如果我将一个元方法添加到一个类中,我希望它会出现在Class.metaClass.metaMethods中。但事实似乎并非如此。特别是,如果我这样做:
class Example {
def realFoo() { "foo" }
}
Example.metaClass.metaFoo = { -> "foo" }
def reals = Example.metaClass.methods*.name.grep{it.contains("Foo")}
def metas = Example.metaClass.metaMethods*.name.grep{it.contains("Foo")}
println "reals = $reals, metas = $metas"我期望得到reals = [realFoo], metas = [metaFoo]的输出,但实际上我得到的是reals = [realFoo, metaFoo], metas = []。
看起来新的元方法存储在方法中,而不是metaMethods中。那么,metaClass.methods和metaClass.metaMethods之间的区别是什么呢
发布于 2011-02-04 06:48:44
MetaMethods包含那些由Groovy类修饰的方法,但实际上不是类或其继承结构的直接部分,也不是通过metaClass手动插入到类中的方法。
这些是在DefaultGroovyMethods类中定义的。
根据要实例化的对象的类型,它主要是each、collect、find等迭代器。
对代码的这一修改显示了仅元、仅“真实”和共享的方法:
class Example {
def realFoo() { "foo" }
}
Example.metaClass.metaFoo = { -> "foo" }
def reals = Example.metaClass.methods.name.sort().unique()
def metas = Example.metaClass.metaMethods.name.sort().unique()
def metaOnly = metas - reals
def realOnly = reals - metas
def shared = reals.findAll { metas.contains(it) }
println """
metaOnly = $metaOnly
realOnly = $realOnly
shared = $shared
"""结果:
metaOnly = [addShutdownHook, any, asBoolean, asType, collect, dump, each, eachWithIndex, every, find, findAll, findIndexOf, findIndexValues, findLastIndexOf, findResult, getAt, getMetaPropertyValues, getProperties, grep, hasProperty, identity, inject, inspect, is, isCase, iterator, metaClass, print, printf, println, putAt, respondsTo, sleep, split, sprintf, use, with]
realOnly = [equals, getClass, getProperty, hashCode, metaFoo, notify, notifyAll, realFoo, setProperty, wait]
shared = [getMetaClass, invokeMethod, setMetaClass, toString]所有的metaOnly和共享方法都是用DefaultGroovyMethods编写的。所有“真正”的方法都在类本身,或者它的父类(本例中是Object)上,再加上几个与metaClass直接相关的groovy方法来获取/设置metaClass,以及允许您覆盖方法行为的getProperty/setProperty和invokeMethod。
如果你想搜索所有的方法,看看有什么存在的,我使用类似这样的方法:
def allMethods = (Example.metaClass.methods + Example.metaClass.metaMethods).name.sort().unique() https://stackoverflow.com/questions/4890136
复制相似问题