Wrap nullable arguments passed to non-null parameters in requireNonNull
io.moderne.nullability.WrapNullableArgumentInRequireNonNullio.moderne.recipe:rewrite-nullabilityWraps in java.util.Objects.requireNonNull(...) (statically imported) each argument that passes a provably-nullable value to a callee parameter that is not declared @Nullable. Inside a @NullMarked scope NullAway treats every unannotated parameter as non-null, so such a call is an error; requireNonNull throws only where the @NonNull callee would already misbehave on null, so runtime behavior is unchanged. Argument and parameter nullness are resolved from the nullness oracle and the flow engine (no name-based heuristics). The fix only fires where the call site is @NullMarked and the callee is itself in an annotated scope, and never asserts non-null on a value that is genuinely nullable at the site (a value read inside its own null-check is left for a human). A value that is explicitly null on some path (a bare null literal or a ternary with a null arm) is not wrapped but flagged for review with an advisory marker, since the parameter likely should be @Nullable. Idempotent and conservative; only Java sources are modified.
Usage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe io.moderne.nullability.WrapNullableArgumentInRequireNonNullIf the recipe isn’t available locally, install it with:
mod config recipes jar install io.moderne.recipe:rewrite-nullability:0.3.1Options
| Name | Type | Description |
|---|---|---|
annotatedPackages | String | Comma-separated package prefixes to limit the fix to (matching NullAway's AnnotatedPackages option). When unset, the recipe fixes all packages.e.g. com.example |
assertionStyle | AssertionStyle | The non-null assertion form to emit at a wrap site. AUTO (the default) inspects the classpath: Guava present (com.google.common) emits Preconditions.checkNotNull, otherwise Objects.requireNonNull. CAST_TO_NON_NULL is explicit-only (it is suppression-like — no runtime throw) and never auto-selected. |
Data tables
Structured output this recipe can produce.
- Null-safety fixes by rungEach residual null-safety fix applied, tagged with the precedence rung that produced it, so the share bottoming out at a last-resort `requireNonNull` assertion can be tracked.
io.moderne.nullability.table.NullFixes - Declined null-safety fixes needing reviewEach site the recipe declined to auto-fix because the fix is a design decision (the value is explicitly `null` on some path, so the slot likely should be `@Nullable`), for human triage.
io.moderne.nullability.table.DeclinedNullFixes - Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats