Skip to content

SONARJAVA-6430 Centralize class-like tree kinds - #5869

Merged
aurelien-coet-sonarsource merged 5 commits into
masterfrom
ac/SONARJAVA-6430
Aug 6, 2026
Merged

SONARJAVA-6430 Centralize class-like tree kinds#5869
aurelien-coet-sonarsource merged 5 commits into
masterfrom
ac/SONARJAVA-6430

Conversation

@aurelien-coet-sonarsource

@aurelien-coet-sonarsource aurelien-coet-sonarsource commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • API additions:
    • Added centralized Tree.CLASS_KINDS list containing all class-like node kinds in Tree.java
    • Added unit test in TreeTest.java to verify CLASS_KINDS matches all kinds backed by ClassTree

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6430

Comment thread java-frontend/src/main/java/org/sonar/java/ast/visitors/ComplexityVisitor.java Outdated
@aurelien-coet-sonarsource
aurelien-coet-sonarsource force-pushed the ac/SONARJAVA-6430 branch 3 times, most recently from ac4bb52 to 2273fde Compare August 4, 2026 07:12
Comment thread its/autoscan/src/test/resources/autoscan/diffs/diff_S1172.json
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Ruling needs updating. A fix PR has been created: #5872

Please review and merge it into your branch.

Comment thread java-checks/src/main/java/org/sonar/java/checks/CallOuterPrivateMethodCheck.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/ClassFieldCountCheck.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/TooManyMethodsCheck.java Outdated
@sonarqube-next

sonarqube-next Bot commented Aug 6, 2026

Copy link
Copy Markdown

@aurelien-coet-sonarsource
aurelien-coet-sonarsource merged commit 4a20ebf into master Aug 6, 2026
21 checks passed
@aurelien-coet-sonarsource
aurelien-coet-sonarsource deleted the ac/SONARJAVA-6430 branch August 6, 2026 12:34
@gitar-bot

gitar-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 5 resolved / 5 findings

Centralizes class-like tree kinds into Tree.CLASS_KINDS and updates frontend checks, addressing findings related to HiddenFieldCheck, MockitoStaticImportCheck, and RedundantNullability scope. No issues found.

✅ 5 resolved
Quality: HiddenFieldCheck excludedNodes() now also excludes RECORD/ANN/IMPLICIT

📄 java-checks/src/main/java/org/sonar/java/checks/HiddenFieldCheck.java:164-169
VariableList.excludedNodes() previously stopped recursion at {METHOD, CLASS, ENUM, INTERFACE, NEW_CLASS}; it now uses Tree.CLASS_KINDS plus METHOD/NEW_CLASS, additionally excluding RECORD, ANNOTATION_TYPE and IMPLICIT_CLASS. This changes which variables inside static blocks/method bodies are collected as excluded when a local record (or annotation type) is present, altering hidden-field detection in that edge case. Verify this matches the intended behavior or that it is exercised by tests.

Quality: MockitoStaticImportCheck switch can drift from CLASS_KINDS

📄 java-checks/src/main/java/org/sonar/java/checks/tests/MockitoStaticImportCheck.java:56-57 📄 java-checks/src/main/java/org/sonar/java/checks/tests/MockitoStaticImportCheck.java:63-64 📄 java-checks/src/main/java/org/sonar/java/checks/tests/MockitoStaticImportCheck.java:72-73
nodesToVisit() now derives from Tree.CLASS_KINDS, but the visitNode()/leaveNode() switch statements still hand-list the class-like kinds (CLASS, ENUM, INTERFACE, RECORD, ANNOTATION_TYPE, IMPLICIT_CLASS). If Tree.CLASS_KINDS gains a new kind later, this check would subscribe to it but silently ignore it in the switch (falling into the default branch), leaving the classMethodsStack unbalanced. Consider centralizing the handling to avoid divergence from CLASS_KINDS.

Bug: RedundantNullability rule scope expands to enums/annotations

📄 java-checks/src/main/java/org/sonar/java/checks/RedundantNullabilityAnnotationsCheck.java:42-44 📄 java-checks/src/main/java/org/sonar/java/checks/RedundantNullabilityAnnotationsCheck.java:81
Previously nodesToVisit() returned only {INTERFACE, CLASS, RECORD} and checkMembers only recursed into {CLASS, INTERFACE, RECORD} members. This commit switches nodesToVisit() to Tree.CLASS_KINDS (adding ENUM, ANNOTATION_TYPE, IMPLICIT_CLASS) and line 81 manually adds ENUM/ANNOTATION_TYPE to the inner-class member check. This is not a behavior-preserving refactor: S6665 will now analyze top-level and nested enums and annotation types for redundant nullability annotations, which can raise new (possibly false-positive) issues on user code. Confirm this expansion is intended and covered by tests; if the rule should keep its original scope, restrict nodesToVisit() to the original kinds rather than CLASS_KINDS.

Bug: Refactor changes behavior for RECORD/IMPLICIT_CLASS in two spots

📄 java-frontend/src/main/java/org/sonar/java/ast/visitors/ComplexityVisitor.java:60 📄 java-frontend/src/main/java/org/sonar/java/ast/visitors/ComplexityVisitor.java:67 📄 java-frontend/src/main/java/org/sonar/java/model/JUtils.java:118
Tree.CLASS_KINDS includes RECORD and IMPLICIT_CLASS, but the two hand-written lists replaced here did not. In ComplexityVisitor.visitClass/visitLambdaExpression (previously CLASS, ENUM, INTERFACE, ANNOTATION_TYPE, COMPILATION_UNIT) and JUtils.enclosingClass (previously CLASS, ENUM, INTERFACE, ANNOTATION_TYPE), records and implicit classes now match where they were skipped before. This alters complexity computation for record roots and makes enclosingClass return the record/implicit-class symbol instead of walking up to an outer class — a semantic change hidden inside a 'centralize kinds' refactor. The cast to ClassTree is safe (both kinds are backed by ClassTree), so this is likely a correct improvement, but it is a behavior change, not a pure refactor. Confirm it is intended and covered by tests; otherwise keep the original narrower kind sets in these two locations. Note Measurer and PublicApiChecker already used the identical 6-kind list, so those replacements are true no-ops.

Bug: S1172 autoscan false negatives increased by the CLASS_KINDS refactor

📄 its/autoscan/src/test/resources/autoscan/diffs/diff_S1172.json:4
This baseline update raises S1172's expected false negatives from 32 to 33, meaning the centralize-class-kinds refactor causes the rule (unused method/constructor parameters) to miss one additional real issue. The cause is that helpers such as ClassPatternsUtils.isClassTree now use Tree.CLASS_KINDS, which adds RECORD and IMPLICIT_CLASS to the previously enumerated CLASS/ENUM/INTERFACE/ANNOTATION_TYPE set, changing scoping/filtering. Confirm this detection regression is intentional; if not, restrict the affected helper(s) to the original kind set rather than the broader CLASS_KINDS constant.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

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