Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*/
package org.sonar.java.checks;

import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.ExpressionsHelper;
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.ImportTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.NewClassTree;
import org.sonar.plugins.java.api.tree.Tree;
Expand Down Expand Up @@ -49,12 +53,27 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {

private void reportIssue(Tree tree) {
reportIssue(tree, "Use the Java 8 Date and Time API instead." + context.getJavaVersion().java8CompatibilityMessage());

}

@Override
public boolean isCompatibleWithJavaVersion(JavaVersion version) {
return version.isJava8Compatible();
}

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.IMPORT, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS);
Comment on lines +64 to +65
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

}

@Override
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

reportIssue(importTree);
}
}
super.visitNode(tree);
}

}
10 changes: 9 additions & 1 deletion java-checks/src/test/files/checks/DateAndTimesCheck.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import java.util.Date;
import java.util.Locale;
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


class A {
void foo() {
void javaUtil() {
Date now = new Date(); // Noncompliant {{Use the Java 8 Date and Time API instead.}}
// ^^^^^^^^^^
now = new Date(1499159427440L); // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

Calendar christmas = Calendar.getInstance(); // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^
christmas = Calendar.getInstance(Locale.CANADA); // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
christmas.setTime(df.parse("25.12.2020"));
}
}
Loading