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 15 commits into
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 ...
}
}
|
aurelien-coet-sonarsource
left a comment
There was a problem hiding this comment.
We should probably not raise an issue twice when an import of the form import java.util.Date is found and the class is instantiated later. Instead, when Date and related classes are explicitly imported, the issue should only be raised on the import. It is only in the case of a wildcard import that the issue would be raised on the use. This could be done with a flag recording if an "explicit" import was already found in the implementatoin. WDYT ?
| Date now = new Date(); // Noncompliant | ||
| // ^^^^^^^^^^ | ||
| Date date = new Date(1499159427440L); // Noncompliant | ||
| 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.
I don't think an issue should be raised here. In the case where these classes are explicitly imported (i.e. not with a wildcard import), we already raise the issue on the import, so it is redundant to report it again on the use.




No description provided.