SONARJAVA-5286 Create rule S7184: "@Scheduled" annotation should only be applied to no-arg methods (#4617)

This commit is contained in:
github-actions[bot] 2025-01-28 11:00:17 +01:00 committed by GitHub
parent 8940eee53e
commit 35c4205143
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 68 additions and 0 deletions

View 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"
}
}

View 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]

View File

@ -0,0 +1,2 @@
{
}