SONARJAVA-5286 Create rule S7184: "@Scheduled" annotation should only be applied to no-arg methods (#4617)
This commit is contained in:
parent
8940eee53e
commit
35c4205143
26
rules/S7184/java/metadata.json
Normal file
26
rules/S7184/java/metadata.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"title": "\"@Scheduled\" annotation should only be applied to no-arg methods",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"spring"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7184",
|
||||
"sqKey": "S7184",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "LOW",
|
||||
"RELIABILITY": "HIGH",
|
||||
"SECURITY": "LOW"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
40
rules/S7184/java/rule.adoc
Normal file
40
rules/S7184/java/rule.adoc
Normal file
@ -0,0 +1,40 @@
|
||||
== Why is this an issue?
|
||||
|
||||
According to Spring documentation, the `@Scheduled` annotation can only be applied to methods without arguments.
|
||||
Applying @Scheduled to a method with arguments will result in a runtime error.
|
||||
|
||||
== How to fix it
|
||||
|
||||
Transform method annotated with `@Scheduled` into a no-arg method.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,java,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
public class ExampleService {
|
||||
|
||||
@Scheduled(fixedRate = 5000)
|
||||
public void scheduledTask(String param) { // non compliant, method has an argument. It will raise a runtime error.
|
||||
// Task implementation
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,java,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
public class ExampleService {
|
||||
|
||||
@Scheduled(fixedRate = 5000)
|
||||
public void scheduledTask() { // compliant, no-arg method
|
||||
// Task implementation
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
* Spring - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html[scheduled]
|
2
rules/S7184/metadata.json
Normal file
2
rules/S7184/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user