在课堂上时,Groovy闭合方法不优先

发布于 2025-02-10 09:25:00 字数 1643 浏览 2 评论 0原文

我只是不明白如何按照我的期望来关闭工作。例如,假设我在clos> CLOSURE中委派bar时,我有一个课程

class Bar {
    public void greeting(String name) {
        println "Hello: ${name}"
    }
}

class Foo {

    void helloBob(){
        bar {
            greeting("Bob")
        }
    }

    def bar(@DelegatesTo(value = Bar, strategy = Closure.DELEGATE_ONLY) Closure cl) {
        cl.rehydrate(new Bar(), this, this)
        cl.call()
    }

    static void main(String[] args) {
        new Foo().helloBob()
    }
    
}

在运行时得到堆栈:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Foo.greeting() is applicable for argument types: (String) values: [Bob]
Possible solutions: getAt(java.lang.String)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:76)
    at 
    at ...

: .greeting()对我没有意义(value = bar 。为什么关闭不参考bar.greeting?我该如何做到这一点?我的IDE(Intellij)似乎认为它是bar。问候是我想要的,但是当我运行时,我会得到一个

堆栈 奇怪的是,如果我删除了一大堆类型的信息,那么它似乎可以

    Bar bar(closure) {
        def bar = new Bar()
        closure.delegate = bar
//        closure.rehydrate(bar, this, this) // this causes error why?
        closure.call()
        return bar
    }

与IDE一起工作。我也不明白为什么我不能使用补水似乎会导致错误,而是手动设置委托是可以的。

I just cannot understand how to get closures to work as I would expect them. For example let's say I have a class

class Bar {
    public void greeting(String name) {
        println "Hello: ${name}"
    }
}

When I delegate Bar in a Closure as such:

class Foo {

    void helloBob(){
        bar {
            greeting("Bob")
        }
    }

    def bar(@DelegatesTo(value = Bar, strategy = Closure.DELEGATE_ONLY) Closure cl) {
        cl.rehydrate(new Bar(), this, this)
        cl.call()
    }

    static void main(String[] args) {
        new Foo().helloBob()
    }
    
}

I get the stacktrace when running:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Foo.greeting() is applicable for argument types: (String) values: [Bob]
Possible solutions: getAt(java.lang.String)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:76)
    at 
    at ...

The: No signature of method: Foo.greeting() makes no sense to me because it should be calling Bar.greeting() as it is in a Closure which has @DelegatesTo(value = Bar. Why is the closure not referring to Bar.greeting? How do I get it to do that? My IDE (IntelliJ) seems to think it's Bar.greeting which is what I want, but when I run it I get a stacktrace.

EDIT
Weirdly enough if I remove a whole bunch of type information it then seems to work with this:

    Bar bar(closure) {
        def bar = new Bar()
        closure.delegate = bar
//        closure.rehydrate(bar, this, this) // this causes error why?
        closure.call()
        return bar
    }

With the IDE happy and all. I also don't understand why I cannot use rehydrate it seems to cause an error, yet setting the delegate manually is fine.

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

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

发布评论

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

评论(1

久随 2025-02-17 09:25:00

补水不会修改关闭 - 它返回了它的新副本。因此,请致电新的关闭。

cl.rehydrate(new Bar(), this, this).call()

不要忘记将分辨率设置为您在注释中所主张的内容。

Rehydrate doesn't modify the closure - it returns a new copy of it. So call the new closure.

cl.rehydrate(new Bar(), this, this).call()

Don't forget to set the resolveStrategy to what you are claiming in the annotation.

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