← All recipes
Moderne licensed

Add @Nullable to generated Thrift getters of optional fields

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

Annotates the generated-Java getter of a Thrift optional field with the JSpecify @Nullable annotation, reading the field's requiredness from the .thrift IDL (which appears as a plain-text source). In an Apache-Thrift-generated struct only an optional field may be left unset, so only its getter (getFoo(), or isFoo() for a bool) can return null; a required field, the Airbnb non_null extension, and an unqualified default field are populated and left untouched. A scanning pass parses each .thrift for struct/union/exception blocks and their optional fields; the edit pass annotates the matching getter on the generated Java class whose simple name equals the struct name and which implements org.apache.thrift.TBase. Adding @Nullable only states the contract the IDL already declares, so behavior is unchanged. Conservative by design: a getter is annotated only when its owning class is a recognized Thrift struct, its name matches an optional field, and it is not already nullable. Gated off the NullSafety apex (generated source); only Java sources are modified, and re-running after regeneration converges.

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

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

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

Data tables

Structured output this recipe can produce.

  • 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