← All recipes
Moderne licensed

Wrap nullable values that are auto-unboxed in requireNonNull

Recipe IDio.moderne.nullability.WrapNullableUnboxingInRequireNonNull
Artifactio.moderne.recipe:rewrite-nullability

Wraps a provably-nullable boxed value (Integer, Long, Boolean, ...) that is used in a primitive context in java.util.Objects.requireNonNull(...) (statically imported). Unboxing such a value is a NullAway error inside an annotated scope, and auto-unboxing a null already throws NullPointerException, so requireNonNull throws exactly where the unboxing already would and runtime behavior is unchanged. The primitive contexts handled are an operand of an arithmetic, relational, or bitwise expression whose other side is a primitive (including ==/!= against a primitive, but not a reference comparison or string concatenation), an argument bound to a primitive callee parameter, an array index, an if/while/do/for/ternary condition, and the return of a primitive-returning method. Nullability is determined from type attribution. The fix only fires where NullAway is active and never asserts non-null on a value that is genuinely nullable at the site. The recipe is idempotent and conservative; only Java sources are modified.

Single recipeProprietary

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.WrapNullableUnboxingInRequireNonNull

If the recipe isn’t available locally, install it with:

mod config recipes jar install io.moderne.recipe:rewrite-nullability:0.3.1

Options

NameTypeDescription
annotatedPackagesStringComma-separated package prefixes to limit the fix to (matching NullAway's AnnotatedPackages option). When unset, the recipe fixes all packages.
e.g. com.example
assertionStyleAssertionStyleThe 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