SONARJAVA-3259 Implement new rule S5673#5780
Conversation
This rule detects classes annotated with @component whose names suggest they should use specialized Spring stereotype annotations (@service, @repository, @controller, or @RestController) instead.
053660b to
700dbe2
Compare
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fixed S1449 violations by adding Locale.ROOT to two toLowerCase() calls in SpringComponentSpecializationCheck.containsIgnoreCase() - Added missing autoscan diff files for new rule S5673 (hasTruePositives=false, falseNegatives=17, falsePositives=0) by creating diff_S5673.json and adding entry to autoscan-diff-by-rules.json - Fixed 2 S1449 code smells in SpringComponentSpecializationCheck.java: added Locale.ROOT to toLowerCase() calls in containsIgnoreCase method (already applied in commit df023db)
Replace containsIgnoreCase with endsWithIgnoreCase for the ServiceFacade suffix check, consistent with all other name-matching patterns in the rule. This avoids false positives on classes like ServiceFacadeBuilder or ServiceFacadeConfiguration. Remove the now-unused containsIgnoreCase helper.
Code Review ✅ Approved 1 resolved / 1 findingsImplements rule S5673 to identify ✅ 1 resolved✅ Edge Case: ServiceFacade matched with contains, inconsistent with other suffixes
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 |
rombirli
left a comment
There was a problem hiding this comment.
Very clear implementation
| String suggestedAnnotation = getSuggestedAnnotation(className); | ||
|
|
||
| if (suggestedAnnotation != null) { | ||
| reportIssue(componentAnnotation.get(), String.format("Use @%s instead of @Component", suggestedAnnotation)); |
There was a problem hiding this comment.
Optional: The issue message could also suggest renaming the class as an alternative way to fix
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.CLASS); |
There was a problem hiding this comment.
What about records, enums, interfaces, annotation_types? Just to double-check if it is intentional to not include them.
There was a problem hiding this comment.
I don't think this would make sense for records enum and annotations. But for interfaces I think it does, in particular repositories can be just interfaces. Let me check.
Extend the rule to cover interface declarations in addition to classes. This is relevant for Spring Data repository interfaces annotated with @component, where @repository would be the appropriate stereotype.
|
Code Review ✅ Approved 1 resolved / 1 findingsImplements rule S5673 to detect misplaced Spring stereotype annotations on classes and interfaces. The suffix matching logic for ServiceFacade was updated to be case-insensitive, resolving the identified inconsistency. ✅ 1 resolved✅ Edge Case: ServiceFacade matched with contains, inconsistent with other suffixes
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 |




This rule detects classes and interfaces annotated with
@Componentwhose names suggest they should use specialized Spring stereotype annotations (@Service,@Repository,@Controller, or@RestController) instead.The rule also covers interfaces, which is relevant for Spring Data repository interfaces — where Spring generates the implementation at runtime, but
@Repositoryis still the appropriate stereotype.