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

Unable to access inherited private fields from a closure within an inherited method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 5.0.0-alpha-1
    • groovy-runtime
    • None

    Description

      class B {
          String data
          B(arg) {
              arg.each() { this.@data = it }
          }
      }
      class C extends B {
          C(arg) {
              super(arg)
          }
      }
      new C(["test"])
      

      The previous unexpectedly produce: Exception thrown: groovy.lang.MissingFieldException: No such field: data for class: C

      class B {
          private String data
          public String getData() { this.@data }
          private setData(String value) { this.@data = value }
          B(arg) {
              arg.each() { data = it }
          }
      }
      class C extends B {
          C(arg) {
              super(arg)
          }
      }
      
      new C(["test"])
      

      And this one produce a similar issue: Exception thrown: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: data for class: C

      Both is related to the fact that the closure is routed throught C and not B which is the expectation. In the second sample, providing a protected setter is a workaround.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              softec Denis Gervalle
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: