Add a @Contract nullness contract to a validation helper
io.moderne.nullability.AddNullnessContractToValidationHelperArtifactio.moderne.recipe:rewrite-nullabilityAdds an org.jetbrains.annotations.@Contract annotation to a single-@Nullable-parameter helper method whose body provably encodes a nullness contract, so the checker can narrow at every call site without any runtime assertion. Three canonical body shapes are recognized: a boolean-returning method whose body is return arg != null && ...; (the argument's non-nullity is a required conjunct) becomes @Contract("null -> false"); a method that unconditionally throws — or delegates to requireNonNull / checkNotNull — when the argument is null becomes @Contract("null -> fail"); and an identity pass-through that returns the argument unchanged becomes @Contract("null -> null"). Only methods with exactly one parameter, a simple recognizable body, and no existing @Contract are annotated. The edit is annotation-only and behavior-preserving — runtime semantics are unchanged. Idempotent and conservative; only Java sources are modified.
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.AddNullnessContractToValidationHelper")
}
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.AddNullnessContractToValidationHelperIf the recipe isn’t available locally, install it with:
mod config recipes jar install io.moderne.recipe:rewrite-nullability:RELEASEOptions
| 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