Skip to content

SONARJAVA-6696: S8989 should not raise on private methods - #5885

Open
romainbrenguier wants to merge 3 commits into
masterfrom
romain/sonarjava-6696
Open

SONARJAVA-6696: S8989 should not raise on private methods#5885
romainbrenguier wants to merge 3 commits into
masterfrom
romain/sonarjava-6696

Conversation

@romainbrenguier

Copy link
Copy Markdown
Contributor

Summary

  • S8989 (@Transactional methods should specify rollback behavior for checked exceptions) now skips private methods
  • Spring AOP uses proxies that cannot intercept private methods, so @Transactional has no effect on them — raising an issue would be a false positive
  • Added test cases covering private methods at both method-level and class-level @Transactional

Test plan

  • Existing unit tests pass (3/3)
  • New compliant test cases for private methods with @Transactional
  • New compliant test case for private method under class-level @Transactional
  • Public methods in same class-level @Transactional still raise issues

🤖 Generated with Claude Code

@transactional has no effect on private methods since Spring AOP
uses proxies that cannot intercept private methods. Skip private
methods to avoid false positives.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

SONARJAVA-6696

romainbrenguier and others added 2 commits August 5, 2026 11:36
Spring proxy-based AOP only intercepts public methods, so extend
the exclusion from private-only to all non-public methods (protected,
package-private). Also fix import ordering and update autoscan diffs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Aug 5, 2026

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

Updates rule S8989 to skip non-public methods where Spring AOP proxies cannot intercept @Transactional, addressing the import ordering and scope coverage findings. Comprehensive test cases have been added to verify the fix.

✅ 2 resolved
Quality: Import ordering broken by new ModifiersUtils import

📄 java-checks/src/main/java/org/sonar/java/checks/spring/TransactionalMethodCheckedExceptionCheck.java:27-28
org.sonar.java.model.ModifiersUtils is inserted before org.sonar.java.checks.helpers.SpringUtils, breaking the alphabetical import order the rest of the file follows. If the project enforces import ordering (checkstyle/spotless), this will fail the build. Move the ModifiersUtils import below the checks.helpers.* imports.

Edge Case: Only private skipped; protected/package-private also not proxied

📄 java-checks/src/main/java/org/sonar/java/checks/spring/TransactionalMethodCheckedExceptionCheck.java:57-60
Spring's proxy-based AOP only applies @transactional to public methods; protected and package-private methods are also not intercepted, so the rule still raises false positives on them just like it did on private methods. If the intent is to avoid false positives on non-proxied methods, consider skipping any non-public method (e.g. also check for absence of PUBLIC), not just PRIVATE.

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

sonarqube-next Bot commented Aug 5, 2026

Copy link
Copy Markdown

Comment on lines +174 to +180
@Transactional
private void privateMethod() throws IOException { // Compliant - private methods are not proxied by Spring
}

@Transactional
private void privateMethodMultipleExceptions() throws IOException, SQLException { // Compliant
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If it has not effect, why would one put the annotation? This case will be covered by S2230.

@romainbrenguier
romainbrenguier marked this pull request as ready for review August 5, 2026 12: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.

1 participant