如何/何时填充 Groovy MetaClassRegistry?

发布于 2025-01-18 09:37:20 字数 112 浏览 2 评论 0原文

我读到,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2025-01-25 09:37:20

我读到 Groovy 中使用的每个 POJO 都会获得一个与
它。

仅供参考...这不仅仅是 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) 来操作注册表。

如果每个 POJO 都有自己的元类,是否存在某种模板
用于生成这些元类?

docs.groovy-lang.org/latest/html/ api/groovy/lang/MetaClass 是最接近模板的东西,但除了满足必须实现所有抽象方法的标准编译时间限制之外,元类中还可能存在任意逻辑。

I read that every POJO used in Groovy gets a MetaClass associated with
it.

FYI... It isn't just POJO. Every class has a meta class associate with it.

These meta-classes are stored in the application wide metaclass
registry.

That registry is represented in groovy.lang.MetaClassRegistry. You can get a reference to the registry from GroovySystem.html#getMetaClassRegistry().

How are these meta-classes generated and placed in the meta-class
registry?

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.

If each POJO gets its own meta-class, is there some sort of template
that is used to generate these meta-classes?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文