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

inconsistent access of methods in outer class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.7
    • 2.4.8
    • None
    • None

    Description

      Groovy handles access to outer instance and static methods from inner classes but not nested (i.e. static) ones. The following code illustrates the problem:

      class Outer {
          static Integer fooCount = 0
          Integer barCount = 0
          static void incFoo() { fooCount++ }
          void incBar() { barCount++ }
          static class Nested {
              static void nestedIncFoo() { incFoo() }
          }
          Inner innerFactory() { new Inner() }
          class Inner { }
      }
      
      Outer.incFoo()
      //Outer.Nested.nestedIncFoo() // => MME
      assert Outer.fooCount == 1 // but should be 2 if no MME
      
      new Outer().with {
          incBar()
          incFoo()
          innerFactory().with {
              incBar()
              incFoo()
          }
          assert barCount == 2
          assert fooCount == 3 // but should be 4 if no MME
      }
      

      After the fix, the following is expected to work:

      class Outer {
          static Integer fooCount = 0
          Integer barCount = 0
          static void incFoo() { fooCount++ }
          void incBar() { barCount++ }
          static class Nested {
              static void nestedIncFoo() { incFoo() }
          }
          Inner innerFactory() { new Inner() }
          class Inner { }
      }
      
      Outer.incFoo()
      Outer.Nested.nestedIncFoo()
      assert Outer.fooCount == 2
      
      new Outer().with {
          incBar()
          incFoo()
          innerFactory().with {
              incBar()
              incFoo()
          }
          assert barCount == 2
          assert fooCount == 4
      }
      

      Basically the commented out line should work.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              paulk Paul King
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: