Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2503 MOP 2.0 design inflluencing issues
  3. GROOVY-5357

The accessor pair can't be provided from different sources

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0.3, 4.0.0
    • groovy-runtime
    • None

    Description

      When one of the accessor pair is added through a mixin or metaclass, Groovy behaves as if the other one did not exist.

      Explanation kindly provided by Paul: "MetaClassImpl finds a MetaBeanProperty containing just the getter. When only a getter is found there is an incorrect assumption that a setter doesn't exist in the underlying class."

      class Foo {
        void setVal(v) { println "setVal: $v" }
      }
      @Category(Foo) class Getter {
       def getVal() { println "getVal" }
      }
      class Test {
        static def main(av) {
          Foo.mixin Getter
          def f = new Foo()
          f.setVal('foo')
          f.val = null // => groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: val for class: Foo
        }
      }
      

      or

      class Foo {
        void setVal(v) { println "setVal: $v" }
      }
      class Test {
        static def main(av) {
          Foo.metaClass.getVal={-> println "getval" }
          def f = new Foo()
          f.setVal('foo')
          f.val = null // => groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: val for class: Foo
        }
      }
      

      or

      class Foo {
        def getVal() { println "getVal" }
      }
      class Test {
        static def main(av) {
          Foo.metaClass.setVal={v-> }
          def f = new Foo()
          f.getVal()
          f.val
        }
      }
      

      etc.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            oc OC
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: