Merge branch 'not-null-by-default' into 'master'

Null safety annotations

The @NotNullByDefault annotation marks all fields, methods and parameters in a class or package @NotNull, so Android Studio will warn if values that may be null are used. Please use this annotation for new classes, and specify @Nullable for any fields, methods and parameters that may be null.

Injected fields are initialised to null, so injected classes should use @MethodsNotNullByDefault and @ParametersNotNullByDefault, or specify @Nullable for injected fields.

See merge request !349
This commit is contained in:
akwizgran
2016-10-12 16:00:54 +00:00
5 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
package org.briarproject.api.nullsafety;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This annotation can be applied to a package or class to indicate that
* the fields in that element are non-null by default unless:
* <ul>
* <li> There is an explicit nullness annotation
* <li> There is a default nullness annotation applied to a more tightly
* nested element.
* </ul>
*/
@Documented
@Nonnull
@TypeQualifierDefault(FIELD)
@Retention(RUNTIME)
public @interface FieldsNotNullByDefault {
}

View File

@@ -0,0 +1,28 @@
package org.briarproject.api.nullsafety;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This annotation can be applied to a package or class to indicate that
* the methods in that element are non-null by default unless:
* <ul>
* <li> There is an explicit nullness annotation
* <li> The method overrides a method in a superclass (in which case the
* annotation of the corresponding method in the superclass applies)
* <li> There is a default nullness annotation applied to a more tightly
* nested element.
* </ul>
*/
@Documented
@Nonnull
@TypeQualifierDefault(METHOD)
@Retention(RUNTIME)
public @interface MethodsNotNullByDefault {
}

View File

@@ -0,0 +1,32 @@
package org.briarproject.api.nullsafety;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This annotation can be applied to a package or class to indicate that
* the fields, methods and parameters in that element are non-null by default
* unless:
* <ul>
* <li> There is an explicit nullness annotation
* <li> The method overrides a method in a superclass (in which case the
* annotation of the corresponding method or parameter in the superclass
* applies)
* <li> There is a default nullness annotation applied to a more tightly
* nested element.
* </ul>
*/
@Documented
@Nonnull
@TypeQualifierDefault({FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface NotNullByDefault {
}

View File

@@ -0,0 +1,28 @@
package org.briarproject.api.nullsafety;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This annotation can be applied to a package or class to indicate that
* the method parameters in that element are non-null by default unless:
* <ul>
* <li> There is an explicit nullness annotation
* <li> The method overrides a method in a superclass (in which case the
* annotation of the corresponding parameter in the superclass applies)
* <li> There is a default nullness annotation applied to a more tightly
* nested element.
* </ul>
*/
@Documented
@Nonnull
@TypeQualifierDefault(PARAMETER)
@Retention(RUNTIME)
public @interface ParametersNotNullByDefault {
}