@Incubating
class NullChecker
extends TypeCheckingDSL
A compile-time type checker that detects potential null dereferences and null-safety violations
in code annotated with @Nullable, @NonNull, and @MonotonicNonNull annotations.
By default, this checker performs annotation-based null checking only. For additional flow-sensitive
analysis that tracks nullability through assignments and control flow (even in unannotated code),
enable the strict option:
@TypeChecked(extensions = 'groovy.typecheckers.NullChecker(strict: true)')
Supported annotations are recognized by simple name from any package:
@Nullable, @CheckForNull, @MonotonicNonNull@NonNull, @NotNull, @NonnullDetected errors include:
null to a @NonNull variablenull or a @Nullable value to a @NonNull parameternull or a @Nullable value from a @NonNull method@Nullable variable without a null check or safe navigation (?.)@Nullable-returning method without a null check,
whether called explicitly (x.getA().b) or via property syntax (x.a.b)a?.b.c;
use a?.b?.c instead)@Nullable-returning
calls, ternaries with a nullable branch) to @NonNull parameters, or returning
them from @NonNull methodsnull to a @MonotonicNonNull field after initializationstrict mode only)The checker recognizes a range of null-guard patterns:
if (x != null), if (x == null)if (x == null) return/throw?.if (x), if (!x) returnif (x != null && x.length() > 0), if (x == null || x.isEmpty()) returnif (x instanceof Foo), if (x !instanceof Foo) returnObjects.nonNull(x), Objects.isNull(x)assert x, assert x != null
@TypeChecked(extensions = 'groovy.typecheckers.NullChecker')
void process(@Nullable String input) {
// input.length() // error: potential null dereference
input?.length() // ok: safe navigation
if (input != null) {
input.length() // ok: null guard
}
}
Over time, the idea would be to support more cases as per:
https://checkerframework.org/manual/#nullness-checker | Constructor and description |
|---|
NullChecker() |
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.