For example:
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
class MyClass {
@Nullable Object field1;
@Nullable Object field2;
@RequiresNonNull({"field1", "#1.field1"})
void method1(@NonNull MyClass other) {
field1.toString(); // OK, this.field1 is known to be non-null
field2.toString(); // error, might throw NullPointerException
other.field1.toString(); // OK, other.field1 is known to be non-null
other.field2.toString(); // error, might throw NullPointerException
}
void method2() {
MyClass other = new MyClass();
field1 = new Object();
other.field1 = new Object();
method1(other); // OK, satisfies method precondition
field1 = null;
other.field1 = new Object();
method1(other); // error, does not satisfy this.field1 method precondition
field1 = new Object();
other.field1 = null;
method1(other); // error, does not satisfy other.field1 method precondition
}
}
Do not use this annotation for formal parameters (instead, give them a @NonNull type,
which is the default and need not be written). The @RequiresNonNull annotation is
intended for other expressions, such as field accesses or method calls.
| Modifier and Type | Class and Description |
|---|---|
| public static @interface | RequiresNonNull.List
A wrapper annotation that makes the |
| Modifier and Type | Method and Description |
|---|---|
| public String[] | Returns: the Java expressions that need to beorg.checkerframework.checker.nullness.qual.NonNullThe Java expressions that need to be |
| value | back to summary |
|---|---|
| public String[] value() The Java expressions that need to be
| |
RequiresNonNull annotation repeatable.
Programmers generally do not need to write this. It is created by Java when a programmer
writes more than one RequiresNonNull annotation at the same location.
| Modifier and Type | Method and Description |
|---|---|
| public RequiresNonNull[] |
| value | back to summary |
|---|---|
| public RequiresNonNull[] value() Returns the repeatable annotations.
| |