使 Groovy 元类方法全局可用

发布于 2024-12-20 02:46:36 字数 216 浏览 2 评论 0原文

我有一个 groovy 脚本 Bootstrap.groovy,其中我在 String 类上定义了几个元类方法。我的测试用例位于另一个文件 Test.groovy 中。在操作 String 时,如何使元类方法在 Test.groovy 中可用? (Iow,如何使元类方法在其他脚本/程序中全局可用)?

(我确实搜索并找到了一些相关问题,但他们没有具体回答这个问题。我使用的是Groovy 1.8.4)

I have a groovy script, Bootstrap.groovy, where I have defined several metaclass methods on the String class. I have my test cases in another file Test.groovy. How do I make the metaclass methods available in the Test.groovy, when manipulating String? (Iow, how do I make the metaclass methods globally available in other scripts/programs)?

(I did search and find some related questions, but they did not answer this specifically. Im using Groovy 1.8.4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

阳光①夏 2024-12-27 02:46:36

使用 DelegatingMetaClass 使这些对 String 类的添加全局可见。

Use the DelegatingMetaClass to make these additions to the String class globally visible.

孤独患者 2024-12-27 02:46:36

我想我找到了一种方法来做到这一点:只需在 Test.groovy 中调用 evaluate(new File("ch8/Bootstrap.groovy")) 即可。之前我尝试过 new GroovyShell().evaluate(),但这不是必需的。我可以直接调用 evaluate() 方法。

I think I found a way to do this: just call evaluate(new File("ch8/Bootstrap.groovy")) in Test.groovy. Previously I was trying new GroovyShell().evaluate(), but thats not required. I could directly call the evaluate() method.

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