-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-3259 Implement new rule S5673 #5780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
700dbe2
Implement new rule S5673
romainbrenguier df023db
Fix S1449: use Locale.ROOT in toLowerCase() calls
romainbrenguier 76ef640
Fix CI failures
romainbrenguier 3cf87e0
S5673: use endsWithIgnoreCase for ServiceFacade match
romainbrenguier 1d30c80
S5673: also apply to interfaces
romainbrenguier ffd168d
S5673: suggest renaming as alternative fix in issue message
romainbrenguier f102561
Update autoscan results
romainbrenguier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S5673.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S5673", | ||
| "hasTruePositives": false, | ||
| "falseNegatives": 20, | ||
| "falsePositives": 0 | ||
| } |
155 changes: 155 additions & 0 deletions
155
...sources/default/src/main/java/checks/spring/SpringComponentSpecializationCheckSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package checks.spring; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.stereotype.Repository; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| public class SpringComponentSpecializationCheckSample { | ||
|
|
||
| // Service patterns | ||
|
|
||
| @Component // Noncompliant {{Use @Service instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class CustomerServiceImpl { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Service instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class OrderService { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Service instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class PaymentServiceFacade { | ||
| } | ||
|
|
||
| // Repository patterns | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class ProductRepository { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class UserRepositoryImpl { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class OrderDao { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class CustomerDao { | ||
| } | ||
|
|
||
| // RestController patterns | ||
|
|
||
| @Component // Noncompliant {{Use @RestController instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class FooBarRestController { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @RestController instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class ApiRestController { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @RestController instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class UserRestControllerImpl { | ||
| } | ||
|
|
||
| // Controller patterns | ||
|
|
||
| @Component // Noncompliant {{Use @Controller instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class HomeController { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Controller instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class LoginControllerImpl { | ||
| } | ||
|
|
||
| // Compliant - Correct annotations used | ||
|
|
||
| @Service | ||
| public class CustomerServiceImplCorrect { | ||
| } | ||
|
|
||
| @Repository | ||
| public class ProductRepositoryCorrect { | ||
| } | ||
|
|
||
| @RestController | ||
| public class FooBarRestControllerCorrect { | ||
| } | ||
|
|
||
| @Controller | ||
| public class HomeControllerCorrect { | ||
| } | ||
|
|
||
| // Generic component names | ||
|
|
||
| @Component | ||
| public class SomeOtherComponent { | ||
| } | ||
|
|
||
| @Component | ||
| public class UtilityHelper { | ||
| } | ||
|
|
||
| @Component | ||
| public class CacheManager { | ||
| } | ||
|
|
||
| @Component | ||
| public class DataProcessor { | ||
| } | ||
|
|
||
| // No annotation | ||
|
|
||
| public class PlainClass { | ||
| } | ||
|
|
||
| // Case variations | ||
|
|
||
| @Component // Noncompliant {{Use @Service instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class userservice { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class USERREPOSITORY { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Controller instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class maincontroller { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @RestController instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public class apirestcontroller { | ||
| } | ||
|
|
||
| // Interface patterns | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public interface UserRepository { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Service instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public interface PaymentService { | ||
| } | ||
|
|
||
| @Component // Noncompliant {{Use @Repository instead of @Component, or rename this type if the @Component annotation is intentional}} | ||
| public interface ProductDao { | ||
| } | ||
|
|
||
| // Compliant interfaces - correct annotations used | ||
|
|
||
| @Repository | ||
| public interface CategoryRepository { | ||
| } | ||
|
|
||
| @Service | ||
| public interface NotificationService { | ||
| } | ||
|
|
||
| // Compliant interfaces - generic names | ||
|
|
||
| @Component | ||
| public interface EventListener { | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...checks/src/main/java/org/sonar/java/checks/spring/SpringComponentSpecializationCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.spring; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import javax.annotation.CheckForNull; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.java.checks.helpers.SpringUtils; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.AnnotationTree; | ||
| import org.sonar.plugins.java.api.tree.ClassTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| @Rule(key = "S5673") | ||
| public class SpringComponentSpecializationCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.CLASS, Tree.Kind.INTERFACE); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| var classTree = (ClassTree) tree; | ||
|
|
||
| Optional<AnnotationTree> componentAnnotation = classTree.modifiers().annotations().stream() | ||
| .filter(a -> SpringUtils.COMPONENT_ANNOTATION.equals(a.annotationType().symbolType().fullyQualifiedName())) | ||
| .findFirst(); | ||
|
|
||
| if (componentAnnotation.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| String className = classTree.simpleName().name(); | ||
| String suggestedAnnotation = getSuggestedAnnotation(className); | ||
|
|
||
| if (suggestedAnnotation != null) { | ||
| reportIssue(componentAnnotation.get(), String.format("Use @%s instead of @Component, or rename this type if the @Component annotation is intentional", suggestedAnnotation)); | ||
| } | ||
| } | ||
|
|
||
| @CheckForNull | ||
| private static String getSuggestedAnnotation(String className) { | ||
| // Check RestController first to avoid false matches with Controller | ||
| if (endsWithIgnoreCase(className, "RestController") || endsWithIgnoreCase(className, "RestControllerImpl")) { | ||
| return "RestController"; | ||
| } | ||
|
|
||
| if (endsWithIgnoreCase(className, "Controller") || endsWithIgnoreCase(className, "ControllerImpl")) { | ||
| return "Controller"; | ||
| } | ||
|
|
||
| if (endsWithIgnoreCase(className, "Service") || | ||
| endsWithIgnoreCase(className, "ServiceImpl") || | ||
| endsWithIgnoreCase(className, "ServiceFacade")) { | ||
| return "Service"; | ||
| } | ||
|
|
||
| if (endsWithIgnoreCase(className, "Repository") || | ||
| endsWithIgnoreCase(className, "RepositoryImpl") || | ||
| endsWithIgnoreCase(className, "Dao")) { | ||
| return "Repository"; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static boolean endsWithIgnoreCase(String str, String suffix) { | ||
| if (str.length() < suffix.length()) { | ||
| return false; | ||
| } | ||
| return str.substring(str.length() - suffix.length()).equalsIgnoreCase(suffix); | ||
| } | ||
|
|
||
| } | ||
33 changes: 33 additions & 0 deletions
33
...ks/src/test/java/org/sonar/java/checks/spring/SpringComponentSpecializationCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.spring; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; | ||
|
|
||
| class SpringComponentSpecializationCheckTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/spring/SpringComponentSpecializationCheckSample.java")) | ||
| .withCheck(new SpringComponentSpecializationCheck()) | ||
| .verifyIssues(); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5673.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <p>The Spring Framework provides several specializations of the generic <code>@Component</code> stereotype annotation which better express the programmer's intent. Using them should be preferred.</p> | ||
| <h2>Why is this an issue?</h2> | ||
| <p>Spring provides specialized stereotype annotations (<code>@Service</code>, <code>@Repository</code>, <code>@Controller</code>, and <code>@RestController</code>) that make code more expressive and provide additional semantics. When a class name suggests it should use one of these specialized annotations but uses <code>@Component</code> instead, it reduces code clarity.</p> | ||
| <h2>How to fix it</h2> | ||
| <p>Replace <code>@Component</code> with the appropriate specialized annotation based on the class's role in the application.</p> | ||
| <h3>Code examples</h3> | ||
| <h4>Noncompliant code example</h4> | ||
| <pre data-diff-id="1" data-diff-type="noncompliant"> | ||
| @Component // Noncompliant | ||
| public class CustomerServiceImpl { | ||
| // ... | ||
| } | ||
|
|
||
| @Component // Noncompliant | ||
| public class ProductRepository { | ||
| // ... | ||
| } | ||
|
|
||
| @Component // Noncompliant | ||
| public class FooBarRestController { | ||
| // ... | ||
| } | ||
| </pre> | ||
| <h4>Compliant solution</h4> | ||
| <pre data-diff-id="1" data-diff-type="compliant"> | ||
| @Service // Compliant | ||
| public class CustomerServiceImpl { | ||
| // ... | ||
| } | ||
|
|
||
| @Repository // Compliant | ||
| public class ProductRepository { | ||
| // ... | ||
| } | ||
|
|
||
| @RestController // Compliant | ||
| public class FooBarRestController { | ||
| // ... | ||
| } | ||
| </pre> | ||
| <h2>Resources</h2> | ||
| <h3>Documentation</h3> | ||
| <ul> | ||
| <li><a href="https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-stereotype-annotations">Spring documentation - @Component and Further Stereotype Annotations</a></li> | ||
| </ul> |
23 changes: 23 additions & 0 deletions
23
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5673.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "title": "Spring components should use specialized annotations", | ||
| "type": "CODE_SMELL", | ||
| "code": { | ||
| "impacts": { | ||
| "MAINTAINABILITY": "LOW" | ||
| }, | ||
| "attribute": "CLEAR" | ||
| }, | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant/Issue", | ||
| "constantCost": "2min" | ||
| }, | ||
| "tags": [ | ||
| "spring" | ||
| ], | ||
| "defaultSeverity": "Major", | ||
| "ruleSpecification": "RSPEC-5673", | ||
| "sqKey": "S5673", | ||
| "scope": "All", | ||
| "quickfix": "unknown" | ||
| } |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.