SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time#5614
Conversation
SummaryS2143 Rule Enhancement: String-Based Import Detection This PR modifies the "Use Java 8 Date and Time API instead" rule to detect problematic imports directly via string parsing, rather than relying solely on semantic analysis of method invocations and constructors. Key Changes:
What reviewers should knowWhere to Start Read the core logic change in
Key Implementation Details
What the Baseline Changes Mean The integration test files (
Testing The test suite now includes:
|
aurelien-coet-sonarsource
left a comment
There was a problem hiding this comment.
Should we consider reporting issues for the old date and time API on imports of Date and Calendar rather than on their uses, to be consistent with what we do on JodaTime ?
aurelien-coet-sonarsource
left a comment
There was a problem hiding this comment.
The current implementation for imports LGTM, but we should probably still report an issue on uses or insantiations of Date and Calendar when they are imported via a wildcard import. For example, we would report an issue for the rule on a call to new Date() when the class is imported with import java.util.*:
import java.util.*;
class ShouldRaiseOnUse {
void foo() {
Date now = new Date(); // Noncompliant ...
}
}
|
Agentic Analysis: Early ResultsAgentic Analysis and Context Augmentation are available on your project. Here are some issues that could have been prevented. Follow the links to learn how to put them into action. 17 issue(s) found across 4 file(s):
Analyzed by SonarQube Agentic Analysis in 5.8 s |
Code Review 🚫 Blocked 1 resolved / 3 findingsRefactors S2143 to use string parsing for file-level detection of legacy date-time types and Joda-Time, but the implementation contains a critical message mismatch and a potential false positive due to loose string matching. 🚨 Bug: Test message does not match implementation constant📄 java-checks/src/main/java/org/sonar/java/checks/DateAndTimesCheck.java:54 📄 java-checks/src/test/java/org/sonar/java/checks/DateAndTimesCheckTest.java:30 📄 java-checks/src/test/java/org/sonar/java/checks/DateAndTimesCheckTest.java:39 📄 java-checks/src/test/java/org/sonar/java/checks/DateAndTimesCheckTest.java:48 📄 java-checks/src/test/java/org/sonar/java/checks/DateAndTimesCheckTest.java:57 📄 java-checks/src/test/java/org/sonar/java/checks/DateAndTimesCheckTest.java:67 The Update all test assertions to use the actual message from ISSUE_MESSAGE.
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
aurelien-coet-sonarsource
left a comment
There was a problem hiding this comment.
LGTM ! We'll just need to update the rule metadata once its severity level has been changed on RSPEC.
4f753af to
7e38733
Compare
|
Code Review ✅ Approved 3 resolved / 3 findingsExtends S2143 detection to include Joda-Time and legacy java.sql date types using string-based import parsing. Resolved issues regarding state reset between files, incorrect test messages, and false-positive class matching. ✅ 3 resolved✅ Bug: issueAlreadyRaised never reset between files
✅ Bug: Test message does not match implementation constant
✅ Bug: startsWith match causes false positives for similar class names
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary by Gitar
S2143detection:Joda-Timeimports as a recommendation to migrate tojava.time.java.sql.Date,java.sql.Time,java.sql.Timestamp, andjava.util.GregorianCalendar.S2143.htmlto clarify the transition from legacy date types and Joda-Time tojava.time.java.timemappings and updated code examples and resources.defaultSeverityfromMajortoInfoand updatedMAINTAINABILITYimpact inS2143.json.S2143toSonar_way_profile.json.This will update automatically on new commits.