-
Notifications
You must be signed in to change notification settings - Fork 727
SONARJAVA-6298 Modify S2143 to also suggest using java.time instead of Joda-Time #5614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||
|
|
@@ -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); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void visitNode(Tree tree) { | ||||||
| if (tree instanceof ImportTree importTree) { | ||||||
| String qualifiedName = ExpressionsHelper.concatenate((ExpressionTree) importTree.qualifiedIdentifier()); | ||||||
| if (qualifiedName.startsWith("org.joda.time")) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. False positive risk:
Suggested change
|
||||||
| reportIssue(importTree); | ||||||
| } | ||||||
| } | ||||||
| super.visitNode(tree); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| 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; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Replace with a valid
Suggested change
|
||||||
|
|
||||||
| 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")); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
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 dropsTree.Kind.METHOD_REFERENCEthat the parent (AbstractMethodDetection) registers. WhileDateAndTimesCheckdoesn't currently implementonMethodReferenceFound, this inconsistency will silently break the rule if someone addsonMethodReferenceFoundlater (e.g. to catchCalendar::getInstance).Build on the parent list instead: