Details
-
Sub-task
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
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.