Skip to content

SONARJAVA-6096 Group import declarations by specificity#5457

Merged
asya-vorobeva merged 10 commits intomasterfrom
asya/s8445
Feb 15, 2026
Merged

SONARJAVA-6096 Group import declarations by specificity#5457
asya-vorobeva merged 10 commits intomasterfrom
asya/s8445

Conversation

@asya-vorobeva
Copy link
Contributor

@asya-vorobeva asya-vorobeva commented Feb 12, 2026

@hashicorp-vault-sonar-prod
Copy link

hashicorp-vault-sonar-prod bot commented Feb 12, 2026

SONARJAVA-6096

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's resolve this the issue of S2208 before we move to the rest of this PR.

asya-vorobeva and others added 7 commits February 13, 2026 11:54
…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>
Copy link
Contributor

Choose a reason for hiding this comment

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

Overall LGTM. Since this now targets only Java 25, we should not have problems with regressions on existing code.

Copy link
Contributor

Choose a reason for hiding this comment

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

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);

Choose a reason for hiding this comment

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

using List<ImportTree> imports = (...).toList() creates unmodifiable list, which is generally preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

@sonarqube-next
Copy link

@asya-vorobeva asya-vorobeva merged commit 207cac2 into master Feb 15, 2026
16 checks passed
@asya-vorobeva asya-vorobeva deleted the asya/s8445 branch February 15, 2026 14:43
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.

2 participants