SONARJAVA-5292 Create rule S7183: @InitBinder methods should have void return type (#4616)
This commit is contained in:
parent
74c4f4c8db
commit
1e62d9fef8
25
rules/S7183/java/metadata.json
Normal file
25
rules/S7183/java/metadata.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"title": "@InitBinder methods should have void return type",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"spring"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7183",
|
||||
"sqKey": "S7183",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM",
|
||||
"SECURITY": "LOW"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
55
rules/S7183/java/rule.adoc
Normal file
55
rules/S7183/java/rule.adoc
Normal file
@ -0,0 +1,55 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Spring provides the `@InitBinder` annotation to initialize a `WebDataBinder` instance for controllers.
|
||||
This is useful to bind request parameters to a model object, and to plug converters and formatters into this process.
|
||||
|
||||
Methods annotated with `@InitBinder` must not have a return value, otherwise the controller containing them will throw an exception when invoked.
|
||||
|
||||
This rule raises an issue when a method annotated with `@InitBinder` does not have a `void` return type
|
||||
|
||||
== How to fix it
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,java,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
@Controller
|
||||
public class MyController {
|
||||
|
||||
@InitBinder
|
||||
public String initBinder(WebDataBinder binder) { // Non compliant, make the @InitBinder method return void
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
dateFormat.setLenient(false);
|
||||
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
|
||||
return "OK";
|
||||
}
|
||||
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,java,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
@Controller
|
||||
public class MyController {
|
||||
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder binder) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
dateFormat.setLenient(false);
|
||||
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
|
||||
}
|
||||
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Spring api - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/InitBinder.html[@InitBinder api]
|
||||
* String documentation - https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-initbinder.html[@InitBinder docs]
|
2
rules/S7183/metadata.json
Normal file
2
rules/S7183/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user