IntelliJ 中使用 GroovyDSL 进行动态方法定义

发布于 2024-12-02 21:26:22 字数 1079 浏览 0 评论 0原文

我试图使用 IntelliJ 中的 GroovyDSL 脚本功能定义动态方法。入门指南“用于 DSL 感知的脚本 IDE” 给出了您对如何开始使用这个有一个好主意。虽然我的 DSL 中的动态方法定义比示例更复杂,但我不太确定如何构建它。您可以想象它的工作方式类似于 动态查找器方法,不同之处在于您可以按任意顺序将任意数量的条件与布尔 And 关键字组合起来。所有关键字均未在我调用该方法的类中定义,但可以在 GDSL 文件中定义。该方法始终以 submitTransactionWith 开头。这是一个示例:

clientDSL.submitTransactionWithCriteria1AndCriteria2AndCriteria3(arg1, arg2, arg3)

我一开始就这样做,但这只适用于一个条件,并且没有考虑到您可以按任何顺序组合多个条件。

def ctx = context(ctype: "my.client.ClientDSL")

contributor(ctx) {
    ['Criteria1', 'Criteria2', 'Criteria3'].each {
        method name: "submitTransactionWith${it}",
               type: 'java.util.Map', 
               params: [args: 'java.util.Collection']
    }
}

我想知道是否有对这种动态方法的特殊支持。我还对如何在 IntelliJ 内部对 Grails 的 DSL 进行建模感兴趣。

I was trying to define a dynamic method using the GroovyDSL scripting capabilities in IntelliJ. The starting guide "Scripting IDE for DSL awareness" gives you a good idea on how to get started with this. The dynamic method definition in my DSL is more complex than the example though and I am not quite sure how to build this. You can imagine it working like dynamic finder methods in Grails except that you can combine an arbitrary number of criteria with a boolean And keyword in any order. All keywords are not defined in the class that I am invoking the method on but could be defined in the GDSL file. The method always starts with submitTransactionWith. Here's an example:

clientDSL.submitTransactionWithCriteria1AndCriteria2AndCriteria3(arg1, arg2, arg3)

I started out with this but that only works for one criteria and doesn't take into account that you can combined multiple criteria in any order.

def ctx = context(ctype: "my.client.ClientDSL")

contributor(ctx) {
    ['Criteria1', 'Criteria2', 'Criteria3'].each {
        method name: "submitTransactionWith${it}",
               type: 'java.util.Map', 
               params: [args: 'java.util.Collection']
    }
}

I was wondering if there's special support for that kind of dynamic method. I'd also be interested in how the DSL for Grails is modeled internally in IntelliJ.

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

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

发布评论

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

评论(1

苍风燃霜 2024-12-09 21:26:22

Grails DSL 位于 ${idea.home}/plugins/GrailsGriffon/lib/standardDsls

它可能会帮助您解决问题。他们提前创建所有方法名称组合的字符串数组,然后在其贡献者中迭代它们,创建使用名称字符串数组的方法。

The Grails DSL in located in ${idea.home}/plugins/GrailsGriffon/lib/standardDsls

It would probably help you for your problem. They create string arrays of all the method name combinations ahead of time, and then just iterate through them in their contributor creating a method using the arrays of strings for names.

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