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

SC: super property is not accessible by a subclass that defines a getter and setter

    XMLWordPrintableJSON

Details

    Description

      If a class defines a default-visibility property and a subclass declares a getter and a setter for that property, the subclass cannot access super's property.

      import groovy.transform.CompileStatic
      import spock.lang.Specification
      
      class Foo {
          Object myThing
      }
      
      @CompileStatic  // everything works without CompileStatic
      class Bar extends Foo {
          void setMyThing(Object list) {
              super.myThing = list
          }
          
          Object getMyThing() {
              return super.myThing as List  // groovy.lang.MissingPropertyException: No such property: myThing for class: Bar
      //        return super.@myThing as List  // [Static type checking] - The field Foo.myThing is not accessible
          }
      }
      
      class BarTest extends Specification {
          def "myThing"() {
              when:
                  Bar bar = new Bar()
                  List list = []
                  bar.myThing = list
              then:
                  bar.myThing.is(list)
          }
      }

      Live Demo

      A workaround is to declare a getter and setter in the superclass.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              jasongarrett Jason Garrett
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: