Skip to content

GROOVY-12521: super.x = value chooses among the setters of the superclass - #3047

Merged
paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:super-overloaded-setter
Oct 29, 2026
Merged

paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:super-overloaded-setter

Conversation

@codeconsole

@codeconsole codeconsole commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

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"
}

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.

…lass

A property with several setters is a MultipleSetterProperty, whose
setProperty calls the setter by name with ordinary dispatch. For super,
that reached the class's own setter when it overrides one of them or
declares the property: super.x = 'a' set the class's own x in dynamic
Groovy and under @TypeChecked, also in a closure and for super.x++ and
super.x += v. MetaClassImpl.setProperty now calls the setter as a super
call, like super.setX(value).
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 73.6070%. Comparing base (e301e08) to head (19441c9).

Files with missing lines Patch % Lines
src/main/java/groovy/lang/MetaClassImpl.java 66.6667% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3047        +/-   ##
==================================================
- Coverage     73.6073%   73.6070%   -0.0003%     
  Complexity      42312      42312                
==================================================
  Files            1657       1657                
  Lines          145620     145622         +2     
  Branches        27466      27467         +1     
==================================================
+ Hits           107187     107188         +1     
+ Misses          28469      28468         -1     
- Partials         9964       9966         +2     
Files with missing lines Coverage Δ
src/main/java/groovy/lang/MetaClassImpl.java 79.0620% <66.6667%> (-0.0325%) ⬇️

... and 16 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Oct 26, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 19441c9
▶️ Tests: 123103 executed
⚪️ Checks: 33/33 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me.

MultipleSetterProperty.setProperty always invokes the setter by name through the receiver's metaclass, so super.x = v could not see the superclass overloads. Routing the useSuper case through invokeMethod(..., true, ...) is the smallest fix that reuses ordinary super dispatch, and it matches super.setX(v) / @CompileStatic.

The tests (declare property vs override a setter, both overloads, closures, += / ++) are convincing. +1.

@paulk-asert
paulk-asert merged commit c9eb64e into apache:master Oct 29, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants