From 0f496290c7b876020a6c9128d7743e0bcd3b476c Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Fri, 24 Oct 2025 11:50:26 +0000 Subject: [PATCH 1/3] Create rule S8229 --- rules/S8229/java/metadata.json | 25 +++++++++++++++++++ rules/S8229/java/rule.adoc | 44 ++++++++++++++++++++++++++++++++++ rules/S8229/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S8229/java/metadata.json create mode 100644 rules/S8229/java/rule.adoc create mode 100644 rules/S8229/metadata.json diff --git a/rules/S8229/java/metadata.json b/rules/S8229/java/metadata.json new file mode 100644 index 00000000000..63412245b7a --- /dev/null +++ b/rules/S8229/java/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-8229", + "sqKey": "S8229", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8229/java/rule.adoc b/rules/S8229/java/rule.adoc new file mode 100644 index 00000000000..4172043c9d3 --- /dev/null +++ b/rules/S8229/java/rule.adoc @@ -0,0 +1,44 @@ +FIXME: add a description + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Why is this an issue? + +FIXME: remove the unused optional headers (that are commented out) + +//=== What is the potential impact? + +== How to fix it +//== How to fix it in FRAMEWORK NAME + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +FIXME +---- + +//=== How does this work? + +//=== Pitfalls + +//=== Going the extra mile + + +//== Resources +//=== Documentation +//=== Articles & blog posts +//=== Conference presentations +//=== Standards +//=== External coding guidelines +//=== Benchmarks diff --git a/rules/S8229/metadata.json b/rules/S8229/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8229/metadata.json @@ -0,0 +1,2 @@ +{ +} From 20603e5e223277a7546c02fd9ca45540bfb22c09 Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Fri, 24 Oct 2025 14:18:37 +0200 Subject: [PATCH 2/3] Update rules/S8229/java/rule.adoc in PR #5795 --- rules/S8229/java/rule.adoc | 51 +++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/rules/S8229/java/rule.adoc b/rules/S8229/java/rule.adoc index 4172043c9d3..dac038eb695 100644 --- a/rules/S8229/java/rule.adoc +++ b/rules/S8229/java/rule.adoc @@ -1,16 +1,22 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] +This rule raises an issue when numeric primitives like `long` or `int` are used to represent temporal concepts such as timeouts, durations, delays, or timestamps. == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +Using numeric primitives to represent date and time concepts creates problems that make code harder to understand and maintain. + +When you see a method parameter like `setTimeout(long timeout)`, it's unclear what unit the timeout represents. Is it milliseconds? Seconds? Minutes? This ambiguity leads to confusion and bugs. + +Numeric primitives also lack type safety. You can accidentally pass a duration where a timestamp is expected, or mix up different time units without any compiler warnings. + +The Java 8+ `java.time` API provides purpose-built types like `Duration`, `Instant`, and `LocalDateTime` that carry semantic meaning, prevent unit confusion, and make your code self-documenting. + +=== What is the potential impact? -//=== What is the potential impact? +Using numeric primitives for temporal values can lead to subtle bugs, especially when different parts of the code assume different time units. This can cause timeouts to be too short or too long, leading to performance issues or system failures. The lack of type safety also makes refactoring more dangerous, as the compiler cannot catch unit mismatches. == How to fix it -//== How to fix it in FRAMEWORK NAME + +Replace numeric primitive parameters with appropriate `java.time` types. Use `Duration` for time spans, `Instant` for timestamps, and other temporal types as needed. === Code examples @@ -18,27 +24,32 @@ FIXME: remove the unused optional headers (that are commented out) [source,java,diff-id=1,diff-type=noncompliant] ---- -FIXME +public void setTimeout(long timeout) { // Noncompliant + this.timeout = timeout; +} + +public void scheduleTask(long delay) { // Noncompliant + // Schedule task after delay +} ---- ==== Compliant solution [source,java,diff-id=1,diff-type=compliant] ---- -FIXME ----- +public void setTimeout(Duration timeout) { + this.timeout = timeout; +} -//=== How does this work? +public void scheduleTask(Duration delay) { + // Schedule task after delay +} +---- -//=== Pitfalls +== Resources -//=== Going the extra mile +=== Documentation + * Duration Class Documentation - https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html[Official documentation for the Duration class] -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks + * Instant Class Documentation - https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html[Official documentation for the Instant class] From ec9f161d6970cf4033eb0cdd69beec633244704c Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Fri, 24 Oct 2025 14:18:41 +0200 Subject: [PATCH 3/3] Update rules/S8229/java/metadata.json in PR #5795 --- rules/S8229/java/metadata.json | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/rules/S8229/java/metadata.json b/rules/S8229/java/metadata.json index 63412245b7a..f069e021c19 100644 --- a/rules/S8229/java/metadata.json +++ b/rules/S8229/java/metadata.json @@ -1,25 +1,28 @@ { - "title": "FIXME", + "title": "Date and time values should use appropriate temporal types instead of numeric primitives", "type": "CODE_SMELL", "status": "ready", "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" + "func": "Constant/Issue", + "constantCost": "10 min" }, "tags": [ + "java8", + "temporal", + "readability" ], - "defaultSeverity": "Major", + "defaultSeverity": "Blocker", "ruleSpecification": "RSPEC-8229", "sqKey": "S8229", "scope": "All", - "defaultQualityProfiles": ["Sonar way"], + "defaultQualityProfiles": [ + "Sonar way" + ], "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "MAINTAINABILITY": "BLOCKER" }, - "attribute": "CONVENTIONAL" + "attribute": "CLEAR" } -} +} \ No newline at end of file