Skip to content

SONARJAVA-6660 Fix S2229 FP for self-invocations inside TransactionTemplate callbacks - #6049

Merged
asya-vorobeva merged 1 commit into
masterfrom
asya/fix-s2229-fp
Aug 28, 2026
Merged

SONARJAVA-6660 Fix S2229 FP for self-invocations inside TransactionTemplate callbacks#6049
asya-vorobeva merged 1 commit into
masterfrom
asya/fix-s2229-fp

Conversation

@asya-vorobeva

Copy link
Copy Markdown
Contributor

Self-invocations inside TransactionTemplate.execute/executeWithoutResult callbacks are suppressed because the template guarantees an active transaction at the call site. Resolving the template's configured propagation statically would require data-flow or symbolic-execution techniques, so all self-invocations within such callbacks are treated as compliant.

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

SONARJAVA-6660

…emplate callbacks

Self-invocations inside TransactionTemplate.execute/executeWithoutResult callbacks
are suppressed because the template guarantees an active transaction at the call site.
Resolving the template's configured propagation statically would require data-flow or
symbolic-execution techniques, so all self-invocations within such callbacks are
treated as compliant.

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

gitar-bot Bot commented Aug 28, 2026

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

Fixes false positives for rule S2229 by suppressing self-invocations inside TransactionTemplate.execute and executeWithoutResult callbacks, where the template guarantees an active transaction. The fix treats all such invocations as compliant since resolving the template's propagation statically would require advanced data-flow analysis.

✅ 2 resolved
Edge Case: Matcher misses TransactionOperations/subclass receivers, FP persists

📄 java-checks/src/main/java/org/sonar/java/checks/spring/SpringIncompatibleTransactionalCheck.java:63-67
TRANSACTION_TEMPLATE_EXECUTE uses .ofTypes(...TransactionTemplate), and MethodMatchersBuilder.isSearchedMethod tests the call-site type with exact equality (type.is(name), MethodMatchersBuilder.java:102/236/251). So a field declared with the interface type — TransactionOperations ops; ops.executeWithoutResult(status -> retry(id)); — or with a custom class MyTemplate extends TransactionTemplate receiver does not match, and the S2229 false positive this PR targets still fires for those shapes (both execute and executeWithoutResult are declared on TransactionOperations). Match on the interface with ofSubTypes so all template flavours are covered.

Bug: Suppression covers receiver and eager arguments, not just callback

📄 java-checks/src/main/java/org/sonar/java/checks/spring/SpringIncompatibleTransactionalCheck.java:105-119
The flag is set before super.visitMethodInvocation, which scans methodSelect() then arguments() (BaseTreeVisitor.java:259), so every self-invocation anywhere in the execute(...) subtree is suppressed — including expressions evaluated before the transaction starts, e.g. transactionTemplate.execute(buildCallback(retryNew(id))) or templateFor(retry(id)).execute(status -> ...). Those calls run outside the template's transaction, so the incompatible-propagation issue is silently lost (false negative). Restrict the flag to the lambda / anonymous-class arguments that actually form the callback body.

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
Contributor

@lijun-chen-sonarsource lijun-chen-sonarsource left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

I've seen your counterarguments to Gitar's comments. I think they make sense.
Do you think it'd make sense to add test cases that make explicit that these two cases are FN and FP that we intentionally accept?

Comment thread java-checks-test-sources/default/pom.xml
@asya-vorobeva

Copy link
Copy Markdown
Contributor Author

LGTM!

I've seen your counterarguments to Gitar's comments. I think they make sense. Do you think it'd make sense to add test cases that make explicit that these two cases are FN and FP that we intentionally accept?

I think no. These cases are super-low, there's no need to document them.

@asya-vorobeva
asya-vorobeva merged commit 4143700 into master Aug 28, 2026
19 checks passed
@asya-vorobeva
asya-vorobeva deleted the asya/fix-s2229-fp branch August 28, 2026 10:56
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