@Documented @Retention(value=RUNTIME) @Target(value={METHOD,CONSTRUCTOR}) public @interface RequiresNonNull
For example:
class MyClass { @Nullable Object field1; @Nullable Object field2; @RequiresNonNull({"field1", "other.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(); // OK, satisfies method precondition field1 = null; other.field1 = new Object(); method1(); // error, does not satisfy this.field1 method precondition field1 = new Object(); other.field1 = null; method1(); // 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.public abstract String[] value
NonNull
.