Make a codebase null-safe
ProprietaryMake Java code null-safe end to end. Infers and adds JSpecify @Nullable/@MonotonicNonNull from the code's own signals (returns, parameters, fields, override hierarchies, Kotlin call sites, and the JSpecify generic frontier), then detects and repairs the residual nullability violations — dereferences, unboxing, switch, enhanced-for, passing a nullable argument, nullable returns, uninitialized non-null fields, and override consistency — directly from the LST, and declares the annotation libraries it imports in Maven and Gradle builds. Behavior-preserving and idempotent; run to a fixpoint over recipe cycles. If the code still uses non-JSpecify annotation flavors, run org.openrewrite.java.jspecify.MigrateToJSpecify first.
io. moderne. nullability. NullSafetyOptional
Comma-separated package prefixes to limit the fix to (matching NullAway's AnnotatedPackages option). When unset, the recipe fixes all packages.
The non-null assertion form the residual Wrap* fixers emit at a last-resort site. AUTO (the default) emits Preconditions.checkNotNull in a file that already imports Guava's Preconditions, otherwise Objects.requireNonNull. CAST_TO_NON_NULL is explicit-only (it is suppression-like — no runtime throw) and never auto-selected.
mod run . --recipe= io. moderne. nullability. NullSafetyInstall it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.
Recipe list
It runs these recipes, in this order.
- Migrate
@NonNullApi/ParametersAreNonnullByDefaultto JSpecify@NullMarked - Add
@NullMarkedto every package - Compose JSpecify best practices (intra-body nullability inference)
- Add
@Nullableto methods that can return null - Add
@Nullableto method parameters that can receive null - Add
@Nullableto fields that can hold null - Propagate
@Nullableacross override relationships - Add
@Nullableto Java returns from Kotlin call sites - Add
@Nullableto array element types that can hold null - Add
@Nullableto collection and map type arguments that hold null elements - Add a
@Nullableupper bound to a pass-through type parameter fed a null-returning lambda - Add
@Nullableto a functional-interface return type argument fed a null-returning lambda - Remove a redundant declaration-position
@Nullableon a method return - Relocate a misplaced leading
@Nullableon a primitive array to the array reference - Move a leading
@Nullableto the type-use position - Replace
@Nullablewith@MonotonicNonNullon a lazily-initialized field - Hoist a guarded
@Nullablefield read into a local variable - Add a
@Contractnullness contract to a validation helper - Route a guarded raw accessor through its present
Optional - Extract a repeated
@Nullableinvocation into a local variable - Replace nullable
x.toString()withString.valueOf(x) - Make a nullable
Booleancondition null-safe withBoolean.TRUE.equals(...) - Return an empty collection instead of
null - Relax
Optional.oftoOptional.ofNullableon nullable values - Add
@MonotonicNonNullto an uninitialized field - Align override nullability with the supertype
- Wrap nullable arguments passed to non-null parameters in
requireNonNull - Wrap nullable dereferenced values in
requireNonNull - Wrap nullable values that are auto-unboxed in
requireNonNull - Wrap nullable
switchselectors inrequireNonNull - Wrap a nullable for-each iterable in
requireNonNull - Wrap nullable thrown expressions in
requireNonNull - Add
@Nullableto a method that can return null - Add
@Nullableto a field assigned a nullable value - Add
@Nullableto a mismatched generic type argument - Move a leading
@Nullableto the type-use position - Add Gradle or Maven dependency
org.jspecify:jspecify:1.0.0 - Add Gradle or Maven dependency
org.checkerframework:checker-qual:3.x - Add Gradle or Maven dependency
org.jetbrains:annotations:26.x - Require the nullability annotation modules in
module-info.java