Make a nullable Boolean condition null-safe with Boolean.TRUE.equals(...)
io.moderne.nullability.SafeNullableBooleanConditionio.moderne.recipe:rewrite-nullabilityRewrites a provably-nullable boxed Boolean used as a condition that auto-unboxes to boolean — the control expression of an if, while, or do/while, the condition of a for, or the condition of a ternary ?: — to Boolean.TRUE.equals(cond). Inside an annotated scope NullAway flags such an unboxing because it throws NullPointerException when the Boolean is null; the Boolean.TRUE.equals(...) form yields false on null instead. This is a behavior change on the null path (a thrown NullPointerException becomes false), so each rewritten condition is stamped with a behavior-change marker recording exactly that. The fix fires only when the condition's static type is the boxed java.lang.Boolean (a primitive boolean is never touched) and it is provably nullable at the site; nullability comes from type attribution and a path-sensitive flow analysis (a value already null-checked on the path is not rewritten). A condition already wrapped in Boolean.TRUE.equals(...) or requireNonNull(...) is left unchanged (idempotent). The recipe is conservative — a value not proven nullable is never rewritten; 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.SafeNullableBooleanConditionIf 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 |
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