Skip to content

SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time#5614

Open
NoemieBenard wants to merge 2 commits intomasterfrom
nb/sonarjava-6298-modify-S2143
Open

SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time#5614
NoemieBenard wants to merge 2 commits intomasterfrom
nb/sonarjava-6298-modify-S2143

Conversation

@NoemieBenard
Copy link
Copy Markdown
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented May 8, 2026

SONARJAVA-6298

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented May 8, 2026

Summary

This 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 IMPORT visitor that identifies any imports from the org.joda.time package and reports them with the same "Use the Java 8 Date and Time API instead" message, encouraging teams to migrate from Joda-Time to java.time.

What reviewers should know

Start here: Review the two new methods in DateAndTimesCheck.java:

  • nodesToVisit() extends the visitor to include Tree.Kind.IMPORT nodes
  • visitNode() intercepts imports and checks if they belong to the org.joda.time package using ExpressionsHelper.concatenate()

What to watch for:

  • The error message is identical for both old Date/Calendar usage and Joda-Time imports. Consider whether a more specific message for Joda-Time would be clearer (e.g., "Use the Java 8 Date and Time API instead of Joda-Time").
  • The check uses ExpressionsHelper.concatenate() to reconstruct the qualified name from the import statement's expression tree. Verify this correctly handles wildcard imports (org.joda.time.*) and fully-qualified imports.
  • The inheritance of AbstractMethodDetection still handles the original Date/Calendar/SimpleDateFormat detections; this PR only adds import-level detection on top.

Test coverage: The test file shows the new behavior working correctly with both specific and wildcard Joda-Time imports flagged as violations.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonarqube-next
Copy link
Copy Markdown

sonarqube-next Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is small and the intent is clear, but there are two correctness issues in the implementation and one invalid test case that need to be addressed before merge.

🗣️ Give feedback

public void visitNode(Tree tree) {
if (tree instanceof ImportTree importTree) {
String qualifiedName = ExpressionsHelper.concatenate((ExpressionTree) importTree.qualifiedIdentifier());
if (qualifiedName.startsWith("org.joda.time")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if (qualifiedName.startsWith("org.joda.time")) {
if (qualifiedName.startsWith("org.joda.time.") || qualifiedName.equals("org.joda.time")) {
  • Mark as noise

Comment on lines +64 to +65
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.IMPORT, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.:

Suggested change
import java.time;
import java.time.LocalDateTime;
  • Mark as noise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant