[ 
https://issues.apache.org/jira/browse/GROOVY-12521?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18132039#comment-18132039
 ] 

ASF GitHub Bot commented on GROOVY-12521:
-----------------------------------------

codeconsole opened a new pull request, #3047:
URL: https://github.com/apache/groovy/pull/3047

   When a superclass overloads a setter (`setX(int)` and `setX(String)`) and 
the class overrides one of the setters or declares the property, `super.x = v` 
in dynamic Groovy and under `@TypeChecked` called the class's own setter, also 
in a closure. It now chooses among the superclass's setters, like 
`super.setX(v)`. `@CompileStatic` already did.
   
   ```groovy
   class Base {
       void setX(int i) { println "Base int $i" }
       void setX(String s) { println "Base String $s" }
   }
   class Sub extends Base {
       void setX(String s) { println "Sub String $s" }
       void test() { super.x = 'a' }  // printed "Sub String a"; now "Base 
String a"
   }
   ```
   
   Split out of #3044.
   
   ## How
   
   A property with several setters is a `MultipleSetterProperty`, whose 
`setProperty` calls the setter by name with ordinary dispatch, so for `super` 
it reached the class's own setter. `MetaClassImpl.setProperty` now calls the 
setter as a super call.
   
   ## Tests
   
   `groovy.ThisAndSuperTest`, for a class that declares the property and one 
that overrides a setter, in all three modes, and in a closure in dynamic Groovy 
and under `@TypeChecked`. They fail without the change. 14,938 tests in the 
`groovy`, `gls`, `bugs`, `org.codehaus.groovy.classgen`, `transform`, 
`runtime`, `reflection`, `vmplugin`, `control` and `ast` packages and the stub 
generator tests pass. `groovy-contracts`, `groovy-typecheckers`, `groovy-ginq`, 
`groovy-macro-library`, `groovy-json`, `groovy-xml` and `groovy-templates` pass 
their tests, and every subproject compiles its main and test sources. The 
Checkstyle and CodeNarc gates pass.
   




> super.x = value calls the class's own setter when the superclass overloads 
> the setter
> -------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12521
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12521
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> When the superclass declares several setters for a property, for example 
> {{setX(int)}} and {{setX(String)}}, and the class overrides one of them or 
> declares the property itself, {{super.x = value}} in dynamic Groovy and under 
> {{@TypeChecked}} calls the class's own setter:
> {code:groovy}
> class Base {
>     List<String> log = []
>     String getX() { 'x:base' }
>     void setX(int i) { log << 'base:int:' + i }
>     void setX(String s) { log << 'base:string:' + s }
> }
> class Sub extends Base {
>     String x = 'x:sub'
>     List<String> test() {
>         super.x = 'a'
>         super.x = 1
>         log + [x]
>     }
> }
> assert new Sub().test() == ['base:string:a', 'base:int:1', 'x:sub'] // fails: 
> [base:int:1, a]
> {code}
> {{super.x = 'a'}} called {{Sub.setX(String)}}, the setter of {{Sub}}'s own 
> property. The same happens inside a closure. {{super.setX('a')}} calls 
> {{Base.setX(String)}}, and so does {{super.x = 'a'}} under {{@CompileStatic}} 
> or when {{Base}} has a single setter. Seen in 4.0.33, 5.0.8, 6.0.0 and master.
> For {{super}}, a property with several setters is a 
> {{MultipleSetterProperty}}, and its {{setProperty}} calls the setter by name 
> with ordinary dispatch.
> Expected: {{super.x = value}} chooses among the setters of the superclass, 
> like {{super.setX(value)}}.
> PR: https://github.com/apache/groovy/pull/3044



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to