Skip to main content

Hoist a guarded @Nullable field read into a local variable

io.moderne.nullability.cleanup.HoistNullableFieldReadIntoLocal

NullAway cannot refine a @Nullable field across a dereference, because the field could be mutated between the null check and the use, so if (this.f != null) { this.f.foo(); } is rejected. This recipe reads the field into a local once — String f = this.f; if (f != null) { f.foo(); } — which NullAway can refine. It matches an if whose condition is <field> != null (or null != <field>) for an instance field declared with a nullability annotation, and whose then-block dereferences the field, then introduces a local of the field's type before the if and replaces the field reads in the condition and then-block with that local. The else-block is left untouched (the field is still nullable there). Conservative by design: it skips locals (which NullAway already refines), non-@Nullable fields, fields reassigned inside the then-block, and cases where the chosen local name would collide with an in-scope name. Only Java sources are modified.

Recipe source

This recipe is only available to users of Moderne.

This recipe is available under the Moderne Proprietary License.

Used by

This recipe is used as part of the following composite recipes:

Example

Before
import org.jspecify.annotations.Nullable;

class Test {
@Nullable String f;

void m() {
if (this.f != null) {
System.out.println(this.f.length());
}
}
}
After
import org.jspecify.annotations.Nullable;

class Test {
@Nullable String f;

void m() {
String f = this.f;
if (f != null) {
System.out.println(f.length());
}
}
}

Usage

This recipe has no required configuration options. Users of Moderne can run it via the Moderne CLI.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

shell
mod run . --recipe HoistNullableFieldReadIntoLocal

If the recipe is not available locally, then you can install it using:

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

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Source files that had results

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.