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

inconsistent access of methods in outer class

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 2.4.8
    • Component/s: None
    • Labels:
      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

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              paulk Paul King

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment