如何/何时填充 Groovy MetaClassRegistry?
我读到,在Groovy中使用的每个Pojo都会获得与之相关的元素。这些元类存储在应用程序宽元注册表中。这些元类是如何生成并放置在元级注册表中的?如果每个POJO都有自己的元级级,是否有某种模板用于生成这些元级?
I read that every POJO used in Groovy gets a MetaClass associated with it. These meta-classes are stored in the application wide metaclass registry. How are these meta-classes generated and placed in the meta-class registry? If each POJO gets its own meta-class, is there some sort of template that is used to generate these meta-classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考...这不仅仅是 POJO。每个类都有一个与之关联的元类。
该注册表在 groovy.lang.MetaClassRegistry 中表示。您可以从 GroovySystem.html#getMetaClassRegistry()。
没有一种方法。您可以编写自己的自定义元类并注册它,Groovy 注册默认元类,您可以暂时替换注册表中的元类(例如,在单元测试运行时),然后将其替换为原始元类。
在运行时,您可以使用类似 MetaClassRegistry.html#getMetaClass(java.lang.Class), MetaClassRegistry.html#setMetaClass(java.lang.Class,groovy.lang.MetaClass) 和 MetaClassRegistry.html #removeMetaClass(java.lang.Class) 来操作注册表。
docs.groovy-lang.org/latest/html/ api/groovy/lang/MetaClass 是最接近模板的东西,但除了满足必须实现所有抽象方法的标准编译时间限制之外,元类中还可能存在任意逻辑。
FYI... It isn't just POJO. Every class has a meta class associate with it.
That registry is represented in groovy.lang.MetaClassRegistry. You can get a reference to the registry from GroovySystem.html#getMetaClassRegistry().
There is no one way. You can write your own custom meta class and register it, Groovy registers default meta classes, you could replace a metaClass in the registry momentarily (while a unit test runs, for example) and then replace it with the original one after.
At runtime you could use methods like MetaClassRegistry.html#getMetaClass(java.lang.Class), MetaClassRegistry.html#setMetaClass(java.lang.Class,groovy.lang.MetaClass), and MetaClassRegistry.html#removeMetaClass(java.lang.Class) to manipulate the registry.
docs.groovy-lang.org/latest/html/api/groovy/lang/MetaClass is the closest thing to a template but aside from satisfying standard compile time restrictions of having to implement all the abstract methods, there could be any arbitrary logic in a meta class one might write.