The Checker Framework distribution includes many useful checkers that
help developers to detect, and more importantly to prevent, errors in
their Java programs. The checkers are easy to use and are invoked as
arguments to javac
.
Using the checkers, developers can express important information about their variables, such as whether a variable may be null, whether a value is intended to be side-effected, or whether a String is interned. As a result, the checkers can detect null pointer exceptions, incorrect mutations, errors in equality testing, among other problems.
This tutorial will teach you how to install and start using the checkers. For more details, see the Checker Framework Manual. The manual discusses how to use the checkers and each checker in greater detail.
$ jsr308-javac -version javac 1.8.0-jsr308-1.8.1
The Checker Framework distribution includes many checkers. This tutorial describes using the Nullness Checker, a checker for detecting nullness errors.
To run a checker on a source file, use the following command
(where jsr308-javac
is the JSR 308 compiler):
jsr308-javac -processor [processor name] source-files ...
NonNull
, indicating that ref
must be a reference to a non-null object. Save the file as
GetStarted
.
import org.checkerframework.checker.nullness.qual.*; public class GetStarted { void sample() { @NonNull Object ref = new Object(); } }
jsr308-javac -processor org.checkerframework.checker.nullness.NullnessChecker GetStarted.javaThe compilation should end without any errors.
@NonNull Object ref = null;
GetStarted.java:5: incompatible types. found : @Nullable <nulltype> required: @NonNull Object @NonNull Object ref = null; ^ 1 error
NonNull
) are permitted anywhere
that would write a type, including generics and casts.
@Interned String intern() { ... } // return value int compareTo(@NonNull String other) { ... } // parameter @NonNull List<@Interned String> messages; // generics: non-null list of interned Strings
Last updated: January 14, 2017.
Back to the Type annotations (JSR 308) webpage.