Make a codebase null-safe

Proprietary

Make 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.NullSafety

Optional

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

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

  1. Migrate @NonNullApi/ParametersAreNonnullByDefault to JSpecify @NullMarked
  2. Add @NullMarked to every package
  3. Compose JSpecify best practices (intra-body nullability inference)
  4. Add @Nullable to methods that can return null
  5. Add @Nullable to method parameters that can receive null
  6. Add @Nullable to fields that can hold null
  7. Propagate @Nullable across override relationships
  8. Add @Nullable to Java returns from Kotlin call sites
  9. Add @Nullable to array element types that can hold null
  10. Add @Nullable to collection and map type arguments that hold null elements
  11. Add a @Nullable upper bound to a pass-through type parameter fed a null-returning lambda
  12. Add @Nullable to a functional-interface return type argument fed a null-returning lambda
  13. Remove a redundant declaration-position @Nullable on a method return
  14. Relocate a misplaced leading @Nullable on a primitive array to the array reference
  15. Move a leading @Nullable to the type-use position
  16. Replace @Nullable with @MonotonicNonNull on a lazily-initialized field
  17. Hoist a guarded @Nullable field read into a local variable
  18. Add a @Contract nullness contract to a validation helper
  19. Route a guarded raw accessor through its present Optional
  20. Extract a repeated @Nullable invocation into a local variable
  21. Replace nullable x.toString() with String.valueOf(x)
  22. Make a nullable Boolean condition null-safe with Boolean.TRUE.equals(...)
  23. Return an empty collection instead of null
  24. Relax Optional.of to Optional.ofNullable on nullable values
  25. Add @MonotonicNonNull to an uninitialized field
  26. Align override nullability with the supertype
  27. Wrap nullable arguments passed to non-null parameters in requireNonNull
  28. Wrap nullable dereferenced values in requireNonNull
  29. Wrap nullable values that are auto-unboxed in requireNonNull
  30. Wrap nullable switch selectors in requireNonNull
  31. Wrap a nullable for-each iterable in requireNonNull
  32. Wrap nullable thrown expressions in requireNonNull
  33. Add @Nullable to a method that can return null
  34. Add @Nullable to a field assigned a nullable value
  35. Add @Nullable to a mismatched generic type argument
  36. Move a leading @Nullable to the type-use position
  37. Add Gradle or Maven dependency org.jspecify:jspecify:1.0.0
  38. Add Gradle or Maven dependency org.checkerframework:checker-qual:3.x
  39. Add Gradle or Maven dependency org.jetbrains:annotations:26.x
  40. Require the nullability annotation modules in module-info.java