Skip to content

Preserve @Nullable parameters in AnnotateRequiredParameters#897

Draft
timtebeek wants to merge 2 commits into
mainfrom
tim/issue-7741
Draft

Preserve @Nullable parameters in AnnotateRequiredParameters#897
timtebeek wants to merge 2 commits into
mainfrom
tim/issue-7741

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

  • Fixes JSpecifyBestPractices ignores my annotation and rewrites the code rewrite#7741: JSpecifyBestPractices (via AnnotateRequiredParameters) was stripping an explicit @Nullable annotation, swapping in @NonNull, and deleting the user's if (x == null) throw new IllegalArgumentException("…") business-exception check.
  • The recipe now skips any parameter already annotated with @Nullable (matched by simple name to cover all common nullness libraries), so the annotation, the null check, and the custom thrown exception are all preserved.
  • Replaced the old removeNullableAnnotationWhenAddingNonNull test (which asserted the broken behavior) with preserveNullableAnnotationAndNullCheck, modeled on the reporter's example.

Test plan

  • ./gradlew test --tests AnnotateRequiredParametersTest

Skip parameters already annotated `@Nullable` so the developer's explicit
intent to allow nulls — along with any accompanying custom null check and
business exception — is preserved.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 20, 2026
@timtebeek timtebeek marked this pull request as draft May 20, 2026 13:50
Only treat a null-check as redundant when the thrown exception is a JDK
type constructed with no arguments. Custom exception types and non-empty
message arguments signal intentional business logic and are left intact,
along with the surrounding `if` and the parameter (no `@NonNull` added).

`Objects.requireNonNull(value, "msg")` continues to be rewritten — that
form is idiomatic null-guard boilerplate where message loss is acceptable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

JSpecifyBestPractices ignores my annotation and rewrites the code

1 participant