Add @Nullable to generated Thrift getters of optional fields
io.moderne.nullability.AnnotateThriftGetterNullabilityArtifactio.moderne.recipe:rewrite-nullabilityAnnotates 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.
Add to your build
~/.gradle/gradle.properties
codeGenomeUsername=you@example.com
codeGenomeToken=YOUR_DOWNLOAD_TOKEN
build.gradle.kts
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("io.moderne.nullability.AnnotateThriftGetterNullability")
}
repositories {
mavenCentral()
maven {
url = uri("https://artifacts.codegenomeproject.org/maven")
credentials {
username = providers.gradleProperty("codeGenomeUsername").get()
password = providers.gradleProperty("codeGenomeToken").get()
}
}
}
dependencies {
rewrite("io.moderne.recipe:rewrite-nullability:0.6.0")
}
Then run
./gradlew rewriteRunUsage
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.AnnotateThriftGetterNullabilityIf the recipe isn’t available locally, install it with:
mod config recipes jar install io.moderne.recipe:rewrite-nullability:RELEASEData 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