Hi Groovy users,
I’m currently building a DSL in groovy and found some strange behaviour
I have no explanation for: If a method pointer with name "myMethod" is
imported as static import and a closure has a delegate with a method
"myMethod" and it's delegation strategy is set to "DELEGATION_ONLY", the
imported method is always called inside the closure - the delegate
property seems to be ignored.
Code sample (with the same explanation at the lines where the "strange"
behaviour occurs):
package mailinglist
// This static import "overrides" the delegation to myMethod inside the
// closure specified in ClosureDelegate: The call myMethod("GoodMorning") is
// dispatched to SomeOtherClass.myMethod instead of ClosureDelegate.myMethod
import static mailinglist.SomeOtherClass.myMethod
class MainClass {
static def closureStuff(Closure someClosure) {
ClosureDelegate spec = new ClosureDelegate()
def rehydratedClosure = someClosure.rehydrate(spec, spec, spec)
rehydratedClosure.resolveStrategy = Closure.DELEGATE_ONLY
rehydratedClosure()
}
public static void main(String[] args) {
// dispatched to SomeOtherClass.myMethod; correct
myMethod "Hello"
closureStuff {
// dispatched to SomeOtherClass.myMethod, but I expected that
// it's dispatched to ClosureDelegate.myMethod instead
myMethod "Good Morning"
}
}
}
class ClosureDelegate {
def myMethod(def param){
println("Inside ClosureDelegate. Called myMethod with '$param'")
}
}
// ======================================================================
package mailinglist
class SomeOtherClass {
public static def myMethod = SomeOtherClass.&myBackingMethodThatsNotPublic
private static myBackingMethodThatsNotPublic(def args) {
println("Inside myBackingMethodThatsNotPublic. Called with argument
'$args'")
}
}
Am I missing something or is this a bug? Thanks for your help in advance!
Regards
Johannes Herrendorf