-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4683 Implement S6837: Superfluous @responsebody annotations… (
#4512) * SONARJAVA-4683 Implement S6837: Superfluous @responsebody annotations should be removed
- Loading branch information
1 parent
32f404b
commit ee37634
Showing
9 changed files
with
250 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
...est-sources/src/main/java/checks/spring/SuperfluousResponseBodyAnnotationCheckSample.java
This file contains 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,61 @@ | ||
package checks.spring; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class SuperfluousResponseBodyAnnotationCheckSample { | ||
@ResponseBody // Noncompliant [[sc=3;ec=16]] {{Remove this superfluous "@ResponseBody" annotation.}} | ||
@GetMapping("foo") | ||
public String get() { | ||
return "Hello world!"; | ||
} | ||
|
||
@ResponseBody // Noncompliant | ||
@GetMapping("bar") | ||
private String get2() { | ||
return "Hello world!"; | ||
} | ||
|
||
@GetMapping("baz") | ||
public String get3() { | ||
return "Hello world!"; | ||
} | ||
|
||
class InnerClass { | ||
@ResponseBody // Compliant | ||
@GetMapping("foo") | ||
public String get() { | ||
return "Hello world!"; | ||
} | ||
} | ||
} | ||
|
||
@Controller | ||
class RegularController { | ||
@ResponseBody // Compliant | ||
@GetMapping("foo") | ||
public String get() { | ||
return "Hello world!"; | ||
} | ||
|
||
@GetMapping("baz") | ||
public String get3() { | ||
return "Hello world!"; | ||
} | ||
} | ||
|
||
class NotAController { | ||
@ResponseBody // Compliant | ||
@GetMapping("foo") | ||
public String get() { | ||
return "Hello world!"; | ||
} | ||
|
||
@GetMapping("baz") | ||
public String get3() { | ||
return "Hello world!"; | ||
} | ||
} |
This file contains 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
51 changes: 51 additions & 0 deletions
51
...ks/src/main/java/org/sonar/java/checks/spring/SuperfluousResponseBodyAnnotationCheck.java
This file contains 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,51 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* 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 GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.java.checks.spring; | ||
|
||
import java.util.List; | ||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.tree.ClassTree; | ||
import org.sonar.plugins.java.api.tree.MethodTree; | ||
import org.sonar.plugins.java.api.tree.Tree; | ||
|
||
@Rule(key = "S6837") | ||
public class SuperfluousResponseBodyAnnotationCheck extends IssuableSubscriptionVisitor { | ||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return List.of(Tree.Kind.CLASS); | ||
} | ||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
var ct = (ClassTree) tree; | ||
if (!ct.symbol().metadata().isAnnotatedWith("org.springframework.web.bind.annotation.RestController")) { | ||
return; | ||
} | ||
|
||
ct.members().stream().filter(member -> member.is(Tree.Kind.METHOD)).forEach(member -> { | ||
var mt = (MethodTree) member; | ||
mt.modifiers().annotations().stream() | ||
.filter(annotationTree -> annotationTree.symbolType().is("org.springframework.web.bind.annotation.ResponseBody")) | ||
.findFirst() | ||
.ifPresent(annotationTree -> reportIssue(annotationTree, "Remove this superfluous \"@ResponseBody\" annotation.")); | ||
}); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6837.html
This file contains 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,58 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>The Spring framework’s <code>@RestController</code> annotation is equivalent to using the <code>@Controller</code> and <code>@ResponseBody</code> | ||
annotations together. As such, it is redundant to add a <code>@ResponseBody</code> annotation when the class is already annotated with | ||
<code>@RestController</code>.</p> | ||
<h2>How to fix it</h2> | ||
<p>Remove the <code>@ResponseBody</code> annotation from the class or method.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
@RestController | ||
public class MyController { | ||
@ResponseBody // Noncompliant, the @RestController annotation already implies @ResponseBody | ||
@RequestMapping("/hello") | ||
public String hello() { | ||
return "Hello World!"; | ||
} | ||
} | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
@RestController | ||
public class MyController { | ||
@RequestMapping("/hello") | ||
public String hello() { | ||
return "Hello World!"; | ||
} | ||
} | ||
</pre> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="2" data-diff-type="noncompliant"> | ||
@RestController | ||
@ResponseBody // Noncompliant, the @RestController annotation already implies @ResponseBody | ||
public class MyController { | ||
@RequestMapping("/hello") | ||
public String hello() { | ||
return "Hello World!"; | ||
} | ||
} | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="2" data-diff-type="compliant"> | ||
@RestController | ||
public class MyController { | ||
@RequestMapping("/hello") | ||
public String hello() { | ||
return "Hello World!"; | ||
} | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Articles & blog posts</h3> | ||
<ul> | ||
<li> Spring Guides - <a href="https://spring.io/guides/gs/rest-service/">Building a RESTful Web Service</a> </li> | ||
<li> Baeldung - <a href="https://www.baeldung.com/spring-controller-vs-restcontroller">The Spring @Controller and @RestController Annotations</a> | ||
</li> | ||
<li> Baeldung - <a href="https://www.baeldung.com/spring-request-response-body">Spring’s RequestBody and ResponseBody Annotations</a> </li> | ||
</ul> | ||
|
23 changes: 23 additions & 0 deletions
23
java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6837.json
This file contains 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": "Superfluous \"@ResponseBody\" annotations should be removed", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"spring" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6837", | ||
"sqKey": "S6837", | ||
"scope": "All", | ||
"quickfix": "targeted", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW" | ||
}, | ||
"attribute": "CLEAR" | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -487,6 +487,7 @@ | |
"S6813", | ||
"S6814", | ||
"S6818", | ||
"S6829" | ||
"S6829", | ||
"S6837" | ||
] | ||
} |
44 changes: 44 additions & 0 deletions
44
...rc/test/java/org/sonar/java/checks/spring/SuperfluousResponseBodyAnnotationCheckTest.java
This file contains 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,44 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* 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 GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
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 SuperfluousResponseBodyAnnotationCheckTest { | ||
@Test | ||
void test() { | ||
CheckVerifier.newVerifier() | ||
.onFile(mainCodeSourcesPath("checks/spring/SuperfluousResponseBodyAnnotationCheckSample.java")) | ||
.withCheck(new SuperfluousResponseBodyAnnotationCheck()) | ||
.verifyIssues(); | ||
} | ||
|
||
@Test | ||
void test_no_semantics() { | ||
CheckVerifier.newVerifier() | ||
.onFile(mainCodeSourcesPath("checks/spring/SuperfluousResponseBodyAnnotationCheckSample.java")) | ||
.withCheck(new SuperfluousResponseBodyAnnotationCheck()) | ||
.withoutSemantic() | ||
.verifyNoIssues(); | ||
} | ||
} |