IDEA GDSL 如何向文件夹中的所有文件(类)添加方法定义?

发布于 2024-12-20 17:04:11 字数 551 浏览 5 评论 0原文

在我的 Grails 应用程序中,有一个文件夹 grails-app/mongoDomain。在这个文件夹中,有几个类也位于不同的包中。

我想为文件夹 grails-app/mongoDomain 内的所有类添加“save()”方法的 GDSL 定义。

我成功地能够将此方法添加到单个类中,但是可以在 grails-app/mongoDomain 中的所有类中添加任何方法吗? .
.
我尝试这样做,但它不起作用..

def mongoDomainContext = context(pathRegexp: /.*grails-app\/mongoDomain.*/)

contributor(mongoDomainContext) {
    method(name: 'save', type: 'void', params: [closure: { }])
}

但是上面的代码不起作用,正确的方法是什么?

.
.
问候 库沙尔

In my Grails Application, there a folder grails-app/mongoDomain. In this folder there are several classes that too in various packages.

I want to add a GDSL Defination for a method say "save()" to all classes inside the folder grails-app/mongoDomain.

I was successfully able to add this method to a single class, but any method to add in all classes in grails-app/mongoDomain??
.
.
I tried Doing this, but it did't worked..

def mongoDomainContext = context(pathRegexp: /.*grails-app\/mongoDomain.*/)

contributor(mongoDomainContext) {
    method(name: 'save', type: 'void', params: [closure: { }])
}

But the above code did't worked, What is the right method for doing it??

.
.
Regards
Kushal

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

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

发布评论

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

评论(2

枫以 2024-12-27 17:04:11

不幸的是,目前还没有这样的 GDSL 原语。在 Griffon 中,他们具有以下使用未记录功能的 GDSL 片段:

['Controller', 'Model', 'View', 'Service'].each { type ->
String artifactPath = type.toLowerCase() + 's'
contributor(ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*${type}/))) {
    def path = psiClass.containingFile.originalFile.virtualFile.path
    if (path =~ ".*/*griffon-app/${artifactPath}/.*") {
        delegatesTo(findClass("griffon.core.Griffon${type}"))

        if (type == 'View') {
            addNodeContributions(delegate)
        }
    }
}

}

它们在类名及其路径上都匹配,您只需要贡献者调用内的第二部分。

Unfortunately, there's no such GDSL primitive yet. In Griffon, they have the following GDSL fragment using undocumented features:

['Controller', 'Model', 'View', 'Service'].each { type ->
String artifactPath = type.toLowerCase() + 's'
contributor(ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*${type}/))) {
    def path = psiClass.containingFile.originalFile.virtualFile.path
    if (path =~ ".*/*griffon-app/${artifactPath}/.*") {
        delegatesTo(findClass("griffon.core.Griffon${type}"))

        if (type == 'View') {
            addNodeContributions(delegate)
        }
    }
}

}

They match on both class name and its path here, you need only the second part, inside the conributor call.

携余温的黄昏 2024-12-27 17:04:11

这是我的做法并且有效。非常感谢 Peter Gromov 提供提示。

def mongoContext = context(
        ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*/))
)

contributor(mongoContext) {
    def path = ""
    try {
        path = psiClass.containingFile.originalFile.virtualFile.path
    } catch (Exception e) {/*This is to prevent any non Class null matches*/}
    if (path =~ ".*/*grails-app/mongoDomain/.*")//Matches Directory 
    {
        //Code Here to add methods/Properties etc

    }
}

它就像一个魅力。感谢大家。

Here's how I did it and it works. Great thanks to Peter Gromov for providing the hint.

def mongoContext = context(
        ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*/))
)

contributor(mongoContext) {
    def path = ""
    try {
        path = psiClass.containingFile.originalFile.virtualFile.path
    } catch (Exception e) {/*This is to prevent any non Class null matches*/}
    if (path =~ ".*/*grails-app/mongoDomain/.*")//Matches Directory 
    {
        //Code Here to add methods/Properties etc

    }
}

It worked like a charm. Thanks to all.

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