Skip to content

SONARJAVA-6716: Fix FN in S8909 for classes with explicit constructors but no no-arg constructor - #5878

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

SONARJAVA-6716: Fix FN in S8909 for classes with explicit constructors but no no-arg constructor#5878
romainbrenguier wants to merge 3 commits into
masterfrom
romain/sonarjava-6716

Conversation

@romainbrenguier

@romainbrenguier romainbrenguier commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixed false negative in CacheKeyGeneratorInstantiableCheck (S8909) where public classes implementing CacheKeyGenerator with explicit constructors but no no-arg constructor were not flagged
  • The hasPublicNoArgsConstructor() method now correctly distinguishes between classes with no explicit constructors (implicit default constructor inherits class visibility) and classes with explicit constructors that lack a no-arg option
  • Added test case for a class with an explicit parameterized constructor but no no-arg constructor

Test plan

  • Existing tests pass (CacheKeyGeneratorInstantiableCheckTest)
  • New noncompliant test case added for the FN scenario
  • CI pipeline passes

🤖 Generated with Claude Code


Summary by Gitar

  • New features:
    • Added verifyAnalysisSucceeds() method to CheckVerifier to test analysis execution without assertions
  • Refactoring:
    • Removed the obsolete autoscan integration test module and associated test files

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

SONARJAVA-6716

… but no no-arg constructor

The hasPublicNoArgsConstructor() method incorrectly returned true for
public classes that had explicit constructors but no no-arg constructor.
The orElse(classTree.symbol().isPublic()) fallback conflated two cases:
classes with no explicit constructors (implicit public no-arg) and
classes with explicit constructors that exclude a no-arg option.

Split the logic: if no constructors are declared, check class visibility
(implicit constructor inherits it); if constructors exist but none is
no-arg, return false.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
romainbrenguier and others added 2 commits August 4, 2026 16:13
… constructor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add an arg constructor to CacheKeyGeneratorInstantiableCheckSample so
it no longer has an implicit public no-arg constructor, making it a
non-compliant example for the CacheKeyGeneratorInstantiableCheck rule.

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

gitar-bot Bot commented Aug 4, 2026

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

Fixes a false negative in rule S8909 for public classes implementing CacheKeyGenerator with explicit constructors by updating no-arg constructor validation. The duplicate verifyAnalysisSucceeds setup logic was also addressed.

✅ 1 resolved
Quality: verifyAnalysisSucceeds duplicates verifyAll setup logic

📄 java-checks-testkit/src/main/java/org/sonar/java/checks/verifier/internal/InternalCheckVerifier.java:287-301 📄 java-checks-testkit/src/main/java/org/sonar/java/checks/verifier/internal/InternalCheckVerifier.java:317-331
InternalCheckVerifier.verifyAnalysisSucceeds() re-implements almost the entire visitor/bridge/scanner setup already present in verifyAll() (sonarComponents, VisitorsBridgeForTests.Builder, semantic-vs-no-semantic branching, cache/scanWithoutParsing handling). This duplication means future changes to the analysis setup must be kept in sync across two methods and risks drift. Consider extracting the shared bridge/scanner construction into a private helper used by both verifyAll() and verifyAnalysisSucceeds().

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 4, 2026

Copy link
Copy Markdown

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