Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7797

Private trait method called from within a closure has the wrong "this" context

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.4.6
    • 2.4.10
    • None

    Description

      Calling a private trait method from within a closure results in a MethodMissing exception. It looks like within the closure, `this` is referring to the implementing Class and not the instance.

      This might be related to GROOVY-7373. The problem is that calling getDelegate() doesn't work as delegate also seems to have the wrong context.

      trait MyTrait {
          void greeter() {
              {-> doGreeting("hi")}.call()
          }
          private void doGreeting(String message) { println message }  
      }
      
      class MyClass implements MyTrait { }
      
      new MyClass().greeter()
      

      If you make the doGreeting method above `static`, it will work properly.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              longwa_uk Aaron Long
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: