在课堂上时,Groovy闭合方法不优先
我只是不明白如何按照我的期望来关闭工作。例如,假设我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
补水不会修改关闭 - 它返回了它的新副本。因此,请致电新的关闭。
不要忘记将分辨率设置为您在注释中所主张的内容。
Rehydrate doesn't modify the closure - it returns a new copy of it. So call the new closure.
Don't forget to set the resolveStrategy to what you are claiming in the annotation.