Skip to content

JAVASE-241 Remove Bean Validation @NotNull annotaions from nullability configuration to fix FPs - #5991

Merged
asya-vorobeva merged 1 commit into
masterfrom
asya/fix-bean-validation-annotations-nullability-types
Aug 24, 2026
Merged

JAVASE-241 Remove Bean Validation @NotNull annotaions from nullability configuration to fix FPs#5991
asya-vorobeva merged 1 commit into
masterfrom
asya/fix-bean-validation-annotations-nullability-types

Conversation

@asya-vorobeva

@asya-vorobeva asya-vorobeva commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

javax.validation.constraints.NotNull and jakarta.validation.constraints.NotNull are runtime constraints, not static nullability guarantees. Removing both of them from nullability status configuration gives consistent, conservative treatment.

Rules fixed as a direct consequence:

  • S4454: @NotNull on equals() parameter no longer fires
  • S6539: @NotNull inside @NullMarked no longer flagged as redundant
  • S2583/S2589 from java-symbolic-execution no longer raised for these annotations

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

JAVASE-241

@GabrielFleischer GabrielFleischer 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.

I am definitely open to discussion, but I feel in the end, the annotations are used to express something tangent to the nullability of the target and we should not try to extract information from it.

I feel like we should simply remove ignore them completely and treat annotated targets as UNKNOWN.

It will remove the need for the changes in the two rules while fixing the problems raised in the ticket.

What do you think ?

@asya-vorobeva

Copy link
Copy Markdown
Contributor Author

I am definitely open to discussion, but I feel in the end, the annotations are used to express something tangent to the nullability of the target and we should not try to extract information from it.

I feel like we should simply remove ignore them completely and treat annotated targets as UNKNOWN.

It will remove the need for the changes in the two rules while fixing the problems raised in the ticket.

What do you think ?

After discussion we decided to remove support for jakarta / javax NonNull annotations.
Proper support will be implemented in DBD when we say that the fields are NonNull only after we see the successful validation call.

…VASE-241)

javax.validation.constraints.NotNull and jakarta.validation.constraints.NotNull
are runtime constraints with no static nullability semantics. Removing them from
all annotation lists (previously NON_NULL, then WEAK_NULLABLE) lets them resolve
to UNKNOWN, which is the correct treatment: the engine makes no nullability
assumptions and no rules fire based on them.

Rules naturally unaffected as a consequence:
- S4454: @NotNull on equals() param resolves to UNKNOWN, not isNonNull() → no FP
- S6539: @NotNull inside @NullMarked resolves to UNKNOWN → not flagged as redundant

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@asya-vorobeva
asya-vorobeva force-pushed the asya/fix-bean-validation-annotations-nullability-types branch from 4c4fb08 to c2ec8f5 Compare August 24, 2026 14:08
@gitar-bot

gitar-bot Bot commented Aug 24, 2026

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

Reclassifies Bean Validation @NotNull annotations as WEAK_NULLABLE to resolve static analysis false positives and inconsistent nullability treatment, addressing the duplicated FQN set finding. No issues found.

✅ 2 resolved
Quality: Bean Validation @NotNull FQN set duplicated across 3 files

📄 java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadataNullabilityHelper.java:97-98 📄 java-checks/src/main/java/org/sonar/java/checks/ChangeMethodContractCheck.java:48-51 📄 java-checks/src/main/java/org/sonar/java/checks/PrimitivesMarkedNullableCheck.java:43-46
The two FQNs javax.validation.constraints.NotNull and jakarta.validation.constraints.NotNull are now hard-coded in three independent places: WEAK_NULLABLE_ANNOTATIONS in JSymbolMetadataNullabilityHelper, BEAN_VALIDATION_ANNOTATIONS in ChangeMethodContractCheck, and CONSTRAINT_ANNOTATIONS_NOT_FLAGGED in PrimitivesMarkedNullableCheck. A future change (e.g. adding another BV constraint or a new package) risks being applied inconsistently. Consider exposing a single shared constant/helper (e.g. an isBeanValidationNotNull(String fqn) in a shared helper) and referencing it from all three sites.

Bug: Reclassifying @NotNull may silently change untested SE rules

📄 java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadataNullabilityHelper.java:97-98 📄 java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadataNullabilityHelper.java:124-125
Moving @NotNull from NONNULL_ANNOTATIONS to WEAK_NULLABLE_ANNOTATIONS is a global change that affects every consumer of isNonNull(), including rules in the java-symbolic-execution module (e.g. NonNullSetToNullCheck / S2637 and related NPE checks) that are not part of this repo module and receive no test updates in this PR. Those rules will stop treating @NotNull as a non-null guarantee, which may turn previously-reported issues into false negatives. Verify the intended behavior of the SE-based rules and add/adjust their test coverage in the symbolic-execution module accordingly.

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

@asya-vorobeva asya-vorobeva changed the title JAVASE-241 Treat Bean Validation @NotNull as WEAK_NULLABLE to fix FPs (JAVASE-241) JAVASE-241 Remove Bean Validation @NotNull annotaions from nullability configuration to fix FPs (JAVASE-241) Aug 24, 2026
@asya-vorobeva asya-vorobeva changed the title JAVASE-241 Remove Bean Validation @NotNull annotaions from nullability configuration to fix FPs (JAVASE-241) JAVASE-241 Remove Bean Validation @NotNull annotaions from nullability configuration to fix FPs Aug 24, 2026

@GabrielFleischer GabrielFleischer 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

@asya-vorobeva
asya-vorobeva merged commit e833707 into master Aug 24, 2026
18 checks passed
@asya-vorobeva
asya-vorobeva deleted the asya/fix-bean-validation-annotations-nullability-types branch August 24, 2026 15:10
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