Add configurable null-checking methods to AnnotateNullableParameters #737
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
AnnotateNullableParametersrecipe currently uses a hardcoded list of known null-checking methods (e.g.,Objects.isNull(),StringUtils.isEmpty()). While this covers common utility libraries, many teams have custom utility methods for null checking that the recipe doesn't recognize.Example scenario:
Currently, only
lastNamewould get the@Nullableannotation despite both parameters being null-checked.Solution
Added an optional
additionalNullCheckingMethodsparameter that accepts a list of custom null-checking method patterns in OpenRewrite'sMethodMatcherformat.Key features:
Usage
Programmatic:
YAML configuration:
Result:
Implementation Details
..)!customMethod(param))Testing
Motivation
This enhancement enables teams with custom utility libraries to benefit from automated nullable annotation without needing to modify their existing code patterns. It's particularly valuable for:
@timtebeek
Relates to: #578