Skip to content

SONARJAVA-6613 Implement new rule S9021#5784

Open
romainbrenguier wants to merge 5 commits into
masterfrom
new-rule/SONARJAVA-6613-S9021
Open

SONARJAVA-6613 Implement new rule S9021#5784
romainbrenguier wants to merge 5 commits into
masterfrom
new-rule/SONARJAVA-6613-S9021

Conversation

@romainbrenguier

Copy link
Copy Markdown
Contributor

This rule detects Spring @configuration classes declared with the 'final' modifier, which prevents Spring from creating CGLIB proxies necessary for proper bean lifecycle management and singleton enforcement.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6613

This rule detects Spring @configuration classes declared with the 'final'
modifier, which prevents Spring from creating CGLIB proxies necessary for
proper bean lifecycle management and singleton enforcement.
@romainbrenguier romainbrenguier force-pushed the new-rule/SONARJAVA-6613-S9021 branch from 4ba1571 to 26003e4 Compare July 14, 2026 07:14
romainbrenguier and others added 4 commits July 14, 2026 09:18
…Check by replacing invalid Tree.Kind.MODIFIER with ModifiersUtils.findModifier()
- Removed `public` modifier from top-level classes in ConfigurationClassShouldNotBeFinalCheckSample.java to fix Java compilation error (multiple public classes in a single file)
- Removed `public` access modifier from top-level classes in ConfigurationClassShouldNotBeFinalCheckSample.java to fix compilation error caused by multiple public classes in a single file.
…ClassShouldNotBeFinalCheckSample.java that pointed to column 8 (position of old 'public' modifier) after 'public' was removed and 'final' moved to column 1
Add explicit documentation and test case clarifying that S9021 intentionally
only detects direct @configuration annotations, not meta-annotations like
@SpringBootApplication. This design decision avoids complexity with checking
proxyBeanMethods on composed annotations.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Jul 14, 2026

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

Implements rule S9021 to detect final @Configuration classes, resolving the limitation where meta-annotations were incorrectly identified. No remaining issues found.

✅ 1 resolved
Edge Case: @configuration detected only as direct annotation, not meta-annotation

📄 java-checks/src/main/java/org/sonar/java/checks/spring/ConfigurationClassShouldNotBeFinalCheck.java:62-66
getConfigurationAnnotation() filters annotations by symbolType().is(SpringUtils.CONFIGURATION_ANNOTATION), which only matches a directly-applied @configuration. Composed/meta-annotations that are themselves @configuration (e.g. @SpringBootApplication, or custom stereotypes meta-annotated with @configuration) also create CGLIB-proxied configuration classes and would be affected by the same final problem, but are not detected here. Other Spring checks in this repo (e.g. SpringConfigurationWithAutowiredFieldsCheck) use symbol().metadata().isAnnotatedWith(...) to catch meta-annotations. If catching those cases is in scope, consider using metadata().isAnnotatedWith(CONFIGURATION_ANNOTATION); if intentionally limited to direct annotations, a test sample documenting the exclusion would make the intent clear.

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

1 similar comment
@gitar-bot

gitar-bot Bot commented Jul 14, 2026

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

Implements rule S9021 to detect final @Configuration classes, resolving the limitation where meta-annotations were incorrectly identified. No remaining issues found.

✅ 1 resolved
Edge Case: @configuration detected only as direct annotation, not meta-annotation

📄 java-checks/src/main/java/org/sonar/java/checks/spring/ConfigurationClassShouldNotBeFinalCheck.java:62-66
getConfigurationAnnotation() filters annotations by symbolType().is(SpringUtils.CONFIGURATION_ANNOTATION), which only matches a directly-applied @configuration. Composed/meta-annotations that are themselves @configuration (e.g. @SpringBootApplication, or custom stereotypes meta-annotated with @configuration) also create CGLIB-proxied configuration classes and would be affected by the same final problem, but are not detected here. Other Spring checks in this repo (e.g. SpringConfigurationWithAutowiredFieldsCheck) use symbol().metadata().isAnnotatedWith(...) to catch meta-annotations. If catching those cases is in scope, consider using metadata().isAnnotatedWith(CONFIGURATION_ANNOTATION); if intentionally limited to direct annotations, a test sample documenting the exclusion would make the intent clear.

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

@sonarqube-next

Copy link
Copy Markdown

@romainbrenguier romainbrenguier marked this pull request as ready for review July 14, 2026 08:50
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