SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time#5614
SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time#5614NoemieBenard wants to merge 2 commits intomasterfrom
Conversation
SummaryThis PR extends SonarQube rule S2143 (DateAndTimesCheck) to detect and flag Joda-Time imports in addition to the existing checks for java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat usage. The change adds an What reviewers should knowStart here: Review the two new methods in DateAndTimesCheck.java:
What to watch for:
Test coverage: The test file shows the new behavior working correctly with both specific and wildcard Joda-Time imports flagged as violations.
|
|
| public void visitNode(Tree tree) { | ||
| if (tree instanceof ImportTree importTree) { | ||
| String qualifiedName = ExpressionsHelper.concatenate((ExpressionTree) importTree.qualifiedIdentifier()); | ||
| if (qualifiedName.startsWith("org.joda.time")) { |
There was a problem hiding this comment.
False positive risk: startsWith("org.joda.time") matches any package whose name begins with those characters — including a hypothetical org.joda.timex or org.joda.timetravel. Use a trailing dot to anchor to the package boundary.
| if (qualifiedName.startsWith("org.joda.time")) { | |
| if (qualifiedName.startsWith("org.joda.time.") || qualifiedName.equals("org.joda.time")) { |
- Mark as noise
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.IMPORT, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS); |
There was a problem hiding this comment.
Overriding nodesToVisit() with a hardcoded list silently drops Tree.Kind.METHOD_REFERENCE that the parent (AbstractMethodDetection) registers. While DateAndTimesCheck doesn't currently implement onMethodReferenceFound, this inconsistency will silently break the rule if someone adds onMethodReferenceFound later (e.g. to catch Calendar::getInstance).
Build on the parent list instead:
| public List<Tree.Kind> nodesToVisit() { | |
| return List.of(Tree.Kind.IMPORT, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS); | |
| @Override | |
| public List<Tree.Kind> nodesToVisit() { | |
| List<Tree.Kind> kinds = new java.util.ArrayList<>(super.nodesToVisit()); | |
| kinds.add(Tree.Kind.IMPORT); | |
| return kinds; | |
| } |
- Mark as noise
| import java.util.Calendar; | ||
| import org.joda.time.DateTime; // Noncompliant {{Use the Java 8 Date and Time API instead.}} | ||
| import org.joda.time.*; // Noncompliant {{Use the Java 8 Date and Time API instead.}} | ||
| import java.time; |
There was a problem hiding this comment.
import java.time; is not valid Java — java.time is a package, not a type, so it cannot appear in a single-type-import declaration. The parser may tolerate it, but this line does not function as a meaningful negative test case showing that java.time imports are not flagged.
Replace with a valid java.time type import to properly assert the negative case, e.g.:
| import java.time; | |
| import java.time.LocalDateTime; |
- Mark as noise




No description provided.