Repository navigation
SONARJAVA-7242: Improve S9149 to not raise when the parent class design requires redeclaring static methods - #6400
romainbrenguier wants to merge 1 commit into
Conversation
…gn requires redeclaring static methods Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Ruling Diff SummaryDetected changes in 2 rule files: 39 issues removed, 0 issues added. S9149 (
|
Code Review 👍 Approved with suggestions 0 closed / 1 findings🟡 Medium risk · The S9149 rule now suppresses covariant-return and JUnit 3 suite hiding cases, which could conceal unintended static-method hiding. Refines S9149 so it no longer raises when a static method hides a superclass method with a covariant return type (such as Guava's 💡 Quality: "Unrelated return type" test actually uses identical return types📄 java-checks-test-sources/default/src/main/java/checks/StaticMethodHidingCheckSample.java:370-380
Rename the fixture so it describes what it tests🤖 Prompt for agentsVerification Rules 2/2 met Knowledge ✅ 1 Repos 4 inspectedFunctional validation No results Rules 2 passed 1 not applicable
Broader impact Repos 4 inspectedCross-repo 4 repositories inspected 0 open impacts
Auto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
| static class UnrelatedReturnParent { | ||
| static Integer value() { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| static class UnrelatedReturnChild extends UnrelatedReturnParent { | ||
| static Integer value() { // Noncompliant {{Rename this method; it hides "value" in "UnrelatedReturnParent".}} | ||
| return 2; | ||
| } | ||
| } |
There was a problem hiding this comment.
💡 Quality: "Unrelated return type" test actually uses identical return types
UnrelatedReturnParent.value() and UnrelatedReturnChild.value() both return Integer, so this fixture repeats the same-return-type case that ImmutableSetLike.create() already covers (Object vs Object). The PR description says hiding with an "unrelated return type" is still reported, but no test checks that. Java (JLS 8.4.8.3) also rejects hiding with a return type that is not return-type-substitutable, so that case can only happen in non-compiling code. Either rename the fixture to say it covers the same return type, or move a real unrelated-return case (e.g. Integer vs String) into files/non-compiling/checks/StaticMethodHidingCheckSample.java so the stated behavior is actually tested.
Rename the fixture so it describes what it tests:
static class SameReturnParent {
static Integer value() {
return 1;
}
}
static class SameReturnChild extends SameReturnParent {
static Integer value() { // Noncompliant {{Rename this method; it hides "value" in "SameReturnParent".}}
return 2;
}
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
|
|
/pvf S9149 |
|
📊 Performance Validation complete |




Summary
S9149 (static methods should not hide methods from superclasses) raised on hiding that is required by the design of the parent class. The rule no longer raises in two such cases:
ImmutableBiMap.of()/copyOf()/builder()hidingImmutableMap's).public static Test suite(): the JUnit 3 runner looks this method up on each test class, so every subclass has to redeclare it.Hiding with the same or an unrelated return type, including unknown types, is still reported.
Ruling / autoscan
suite()issues in test classes removed; theLocaleBeanUtilsissues remain.junit.framework.Testcannot be resolved without bytecode. This is a prediction and not verified locally.Not addressed
The ticket also suggests not raising when the hidden method comes from an external library. This was not done: the semantic model has no reliable way to tell library classes apart from project classes compiled in other files. Those issues are still reported (e.g. hiding
Thread.sleep).🤖 Generated with Claude Code
Agent workflow
Tool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier
PR created using
uv run create_with_claude.py SONARJAVA-7242.txt