SONARJAVA-6096 Group import declarations by specificity#5457
SONARJAVA-6096 Group import declarations by specificity#5457asya-vorobeva merged 10 commits intomasterfrom
Conversation
tomasz-tylenda-sonarsource
left a comment
There was a problem hiding this comment.
Let's resolve this the issue of S2208 before we move to the rest of this PR.
java-checks/src/main/java/org/sonar/java/checks/WildcardImportsShouldNotBeUsedCheck.java
Outdated
Show resolved
Hide resolved
…r tests Replace custom fullQualifiedName method with ExpressionsHelper.concatenate for better code reuse. Add comprehensive parameterized tests for the concatenate method covering all import types including module imports. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add JavaVersionAwareVisitor implementation to S2208 to restrict it to Java 24 and earlier. Java 25's import organization strategy (S8445) allows wildcard imports when properly grouped by specificity, making this restriction incompatible with Java 25+. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add new rule to enforce proper grouping of import declarations by specificity. Import order should be: module imports, on-demand package imports, single-type imports, followed by static imports in the same order. Rule only applies to Java 25+ projects. Key features: - Classifies imports using ImportTree.isModule(), isStatic() and ExpressionsHelper.concatenate() for wildcard detection - Reports ordering violations with clear descriptive messages - Uses SENTINEL_IMPORT enum value to simplify initialization logic - Properly cleans up imports list in leaveNode() Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add comprehensive test suite with sample files: - ImportDeclarationOrderCheckSample.java: Tests violations with mixed import orders - ImportDeclarationOrderCheckSampleNoIssues.java: Tests correctly ordered imports - Tests verify rule only runs on Java 25+ Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add JSON and HTML metadata for ImportDeclarationOrderCheck rule: - S8445.json: Rule configuration with Minor severity, 2min remediation - S8445.html: Rule documentation with examples and rationale - Sonar_way_profile.json: Add S8445 to Sonar way profile Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…y when module declarations aren't present
0b9d7b7 to
8d5b335
Compare
43d1c14 to
989c74f
Compare
java-checks/src/main/java/org/sonar/java/checks/ImportDeclarationOrderCheck.java
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/ImportDeclarationOrderCheck.java
Outdated
Show resolved
Hide resolved
java-checks-test-sources/default/src/main/java/checks/ImportDeclarationOrderCheckSample.java
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/ImportDeclarationOrderCheck.java
Outdated
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/WildcardImportsShouldNotBeUsedCheck.java
Show resolved
Hide resolved
1643095 to
185bf76
Compare
tomasz-tylenda-sonarsource
left a comment
There was a problem hiding this comment.
Overall LGTM. Since this now targets only Java 25, we should not have problems with regressions on existing code.
java-checks/src/test/java/org/sonar/java/checks/WildcardImportsShouldNotBeUsedCheckTest.java
Show resolved
Hide resolved
java-checks-test-sources/default/src/main/java/checks/ImportDeclarationOrderCheckSample.java
Show resolved
Hide resolved
...st-sources/default/src/main/java/checks/ImportDeclarationOrderCheckSampleNoIssuesSample.java
Show resolved
Hide resolved
185bf76 to
121f790
Compare
121f790 to
7a18835
Compare
tomasz-tylenda-sonarsource
left a comment
There was a problem hiding this comment.
LGTM, please take a look at the two small comments.
| .stream() | ||
| .filter(importTree -> importTree.is(Tree.Kind.IMPORT)) | ||
| .map(ImportTree.class::cast) | ||
| .forEach(imports::add); |
There was a problem hiding this comment.
using List<ImportTree> imports = (...).toList() creates unmodifiable list, which is generally preferred.
There was a problem hiding this comment.
It's really preferred when it's publicly accessible / can be potentially modified by external modules. In the case when it's private access within one class, I don't think it's important.
|
|
||
| // Basic violation: package import after single-type import | ||
| import java.util.List; // Compliant | ||
| import java.io.*; // Noncompliant {{Reorder this on-demand package import to come before single-type imports.}} |
There was a problem hiding this comment.
Just noticed this. Right now the squiggly line is on the import keyword. It think it it would be nicer to underline the import itself. It can be done with reportIssue(importTree.qualifiedIdentifier(), message); and tested with
// ^^^^^^^^^ on the next line.
There was a problem hiding this comment.
I'll do it, but as we've discussed, the whole functionality will be updated to QuickFix and single raise. So there's no real sense doing it.
|




See SONARJAVA-6096.