Dycu
Dycu

Reputation: 33

Closures resolving strategy not working in trait context in Groovy 2.4.3

I've got the following code:

 trait ContainingClosure {

  def method() {
      def delegateClass = new DelegateClass()

      def closure = {
          methodFromDelegate()
      }

      closure.delegate = delegateClass
      closure.resolveStrategy = Closure.DELEGATE_FIRST

      closure.call()
  }

}

class DelegateClass {
    def methodFromDelegate() {
      println 'methodFromDelegate called'
    }
}

class Main implements ContainingClosure {}
new Main().method()

The problem is that methodFromDelegate() cannot be found when I run call() method and the following exception is thrown:

groovy.lang.MissingMethodException: No signature of method: Main.methodFromDelegate() is applicable for argument types: () values: []

Is there any reasonable explanation why this snippet is not working in Grails 2.5.0 (Groovy 2.4.3)? It seems that the delegate of the closure is somehow ignored and method lookup is done in Main class scope not the delegate itself.

Changing trait into class and implementation of a trait into inheritance makes this code working again.

Upvotes: 2

Views: 372

Answers (1)

tim_yates
tim_yates

Reputation: 171114

Found it, it's this bug

https://issues.apache.org/jira/browse/GROOVY-7456

Which is fixed in groovy 2.4.4, so an upgrade of grails should fix it :-)

Upvotes: 1

Related Questions