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

Closure in parent class can't access private method in same class when called from a child class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • None
    • None
    • None
    • None

    Description

      Private method of parent class can't be accessed by closure when called from a child class:

      class Child extends BaseClass {
          static void main(String... args) {
              println new Child().doStuff([1,2])
          }
      }
      
      class BaseClass {
          private int helper(int a) {
              a + 1
          }
          
          def doStuff(List<Integer> numbers) {
              numbers.collect {
               	helper(it)   
              }
          }
      } 
      

      WebConsole

      org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: Child.helper() is applicable for argument types: (Integer) values: [1]
      Possible solutions: sleep(long), every(), grep(), every(groovy.lang.Closure), sleep(long, groovy.lang.Closure)
      	at BaseClass.invokeMethod(Script1.groovy)
      Caused by: groovy.lang.MissingMethodException: No signature of method: Child.helper() is applicable for argument types: (Integer) values: [1]
      Possible solutions: sleep(long), every(), grep(), every(groovy.lang.Closure), sleep(long, groovy.lang.Closure)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
      

      When using a classical foreach loop it works

      class Child extends BaseClass {
          static void main(String... args) {
              println new Child().doStuff([1,2])
          }
      }
      
      class BaseClass {
          private int helper(int a) {
              a + 1
          }
          
          def doStuff(List<Integer> numbers) {
              def result = []
              for (int it in numbers){
      			result << helper(it)
              }
              result
          }
      }
      

      WebConsole

      Removing the private modifier also fixes this problem.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              leonard84 Leonard Brünings
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: