Home

Awesome

gradle-nullaway-plugin

This plugin is a companion to the net.ltgt.errorprone Gradle plugin that adds a Gradle DSL to configure NullAway

Requirements

This plugin requires using at least Gradle 6.8, and applying the net.ltgt.errorprone plugin (it won't do anything otherwise).

Usage

plugins {
    id("net.ltgt.errorprone") version "<error prone plugin version>"
    id("net.ltgt.nullaway") version "<plugin version>"
}

then add the NullAway dependency to the errorprone configuration:

dependencies {
    errorprone("com.uber.nullaway:nullaway:$nullawayVersion")
}

and finally configure NullAway's annotated packages:

nullaway {
    annotatedPackages.add("net.ltgt")
}

Configuration

Other NullAway flags, as well as the check severity, can be configured on the JavaCompile tasks:

tasks.withType(JavaCompile).configureEach {
    options.errorprone.nullaway {
        error()
        unannotatedSubPackages.add("com.foo.baz")
    }
}

or with Kotlin DSL:

import net.ltgt.gradle.errorprone.errorprone
import net.ltgt.gradle.nullaway.nullaway

tasks.withType<JavaCompile>().configureEach {
    options.errorprone.nullaway {
        error()
        unannotatedSubPackages.add("com.foo.baz")
    }
}

Properties

Please note that all properties are lazy, so while you can use = in place of .set(…) in the Groovy DSL, you cannot use << or += to add to lists for instance.

Each property (except for severity) maps to an -XepOpt:NullAway:[propertyName]=[value] Error Prone argument.

PropertyDescription
severityThe check severity. Almost equivalent to options.errorprone.check("NullAway", severity) (NullAway won't actually appear in options.errorprone.checks). Can be set to CheckSeverity.OFF to disable NullAway.
annotatedPackagesThe list of packages that should be considered properly annotated according to the NullAway convention. This can be used to add to or override the annotatedPackages at the project level.
unannotatedSubPackagesA list of subpackages to be excluded from the AnnotatedPackages list.
unannotatedClassesA list of classes within annotated packages that should be treated as unannotated.
knownInitializersThe fully qualified name of those methods from third-party libraries that NullAway should treat as initializers.
excludedClassAnnotationsA list of annotations that cause classes to be excluded from nullability analysis.
excludedClassesA list of classes to be excluded from the nullability analysis.
excludedFieldAnnotationsA list of annotations that cause fields to be excluded from being checked for proper initialization.
customInitializerAnnotationsA list of annotations that should be considered equivalent to @Initializer annotations, and thus mark methods as initializers.
externalInitAnnotationsA list of annotations for classes that are "externally initialized."
treatGeneratedAsUnannotatedIf set to true, NullAway treats any class annotated with @Generated as if its APIs are unannotated when analyzing uses from other classes.
acknowledgeRestrictiveAnnotationsIf set to true, NullAway will acknowledge nullability annotations whenever they are available in unannotated code and also more restrictive than it's optimistic defaults.
checkOptionalEmptinessIf set to true, NullAway will check for .get() accesses to potentially empty Optional values, analogously to how it handles dereferences to @Nullable values.
checkOptionalEmptinessCustomClassesA list of classes to be treated as Optional implementations (e.g. Guava's com.google.common.base.Optional).
suggestSuppressionsIf set to true, NullAway will use Error Prone's suggested fix functionality to suggest suppressing any warning that it finds.
autoFixSuppressionCommentA comment that will be added alongside the @SuppressWarnings("NullAway") annotation when isSuggestSuppressions is set to true.
castToNonNullMethodThe fully qualified name of a method to be used for downcasting to a non-null value rather than standard suppressions in some instances.
assertsEnabled(isAssertsEnabled with Kotlin DSL) If set to true, NullAway will handle assertions, and use that to reason about the possibility of null dereferences in the code that follows these assertions. This assumes that assertions will always be enabled at runtime.
handleTestAssertionLibrariesIf set to true, NullAway will handle assertions from test libraries, like assertThat(...).isNotNull(), and use that to reason about the possibility of null dereferences in the code that follows these assertions.
exhaustiveOverride(isExhaustiveOverride with Kotlin DSL) If set to true, NullAway will check every method to see whether or not it overrides a method of a super-type, rather than relying only on the @Override annotation.
acknowledgeAndroidRecentIf set to true, treats @RecentlyNullable as @Nullable, and @RecentlyNonNull as @NonNull; requires that acknowledgeRestrictiveAnnotations is also set to true.
checkContractsIf set to true, NullAway will check @Contract annotations.
customContractAnnotationsA list of annotations that should be considered equivalent to @Contract annotations.
customNullableAnnotationsA list of annotations that should be considered equivalent to @Nullable annotations.
customNonnullAnnotationsA list of annotations that should be considered equivalent to @NonNull annotations, for the cases where NullAway cares about such annotations (see e.g. acknowledgeRestrictiveAnnotations).
customGeneratedCodeAnnotationsA list of annotations that should be considered equivalent to @Generated annotations, for the cases where NullAway cares about such annotations (see e.g. treatGeneratedAsUnannotated).
jspecifyMode(isJSpecifyMode with Kotlin DSL) If set to true, enables new checks based on JSpecify (like checks for generic types).
extraFuturesClassesA list of classes to be treated equivalently to Guava Futures and FluentFuture; this special support will likely be removed once NullAway's JSpecify support is more complete.

Methods

MethodDescription
enable()Enable NullAway. Equivalent to severity.set(CheckSeverity.DEFAULT).
disable()Disable NullAway. Equivalent to severity.set(CheckSeverity.OFF).
warn()Enable NullAway as a warning. Equivalent to severity.set(CheckSeverity.WARN).
error()Enable NullAway as an error. Equivalent to severity.set(CheckSeverity.ERROR).