Mark assertion methods as @Pure

See https://checkerframework.org/manual/#type-refinement-purity.

PiperOrigin-RevId: 344994705
This commit is contained in:
andrewlewis 2020-12-01 12:30:50 +00:00 committed by Oliver Woodman
parent 69dad31b67
commit c7e1d3f222

View File

@ -20,6 +20,7 @@ import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.dataflow.qual.Pure;
/**
* Provides methods for asserting the truth of expressions and properties.
@ -34,6 +35,7 @@ public final class Assertions {
* @param expression The expression to evaluate.
* @throws IllegalArgumentException If {@code expression} is false.
*/
@Pure
public static void checkArgument(boolean expression) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && !expression) {
throw new IllegalArgumentException();
@ -48,6 +50,7 @@ public final class Assertions {
* to a {@link String} using {@link String#valueOf(Object)}.
* @throws IllegalArgumentException If {@code expression} is false.
*/
@Pure
public static void checkArgument(boolean expression, Object errorMessage) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && !expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
@ -63,6 +66,7 @@ public final class Assertions {
* @return The {@code index} that was validated.
* @throws IndexOutOfBoundsException If {@code index} falls outside the specified bounds.
*/
@Pure
public static int checkIndex(int index, int start, int limit) {
if (index < start || index >= limit) {
throw new IndexOutOfBoundsException();
@ -76,6 +80,7 @@ public final class Assertions {
* @param expression The expression to evaluate.
* @throws IllegalStateException If {@code expression} is false.
*/
@Pure
public static void checkState(boolean expression) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && !expression) {
throw new IllegalStateException();
@ -90,6 +95,7 @@ public final class Assertions {
* to a {@link String} using {@link String#valueOf(Object)}.
* @throws IllegalStateException If {@code expression} is false.
*/
@Pure
public static void checkState(boolean expression, Object errorMessage) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && !expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
@ -106,6 +112,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static <T> T checkStateNotNull(@Nullable T reference) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
throw new IllegalStateException();
@ -125,6 +132,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static <T> T checkStateNotNull(@Nullable T reference, Object errorMessage) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
throw new IllegalStateException(String.valueOf(errorMessage));
@ -142,6 +150,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static <T> T checkNotNull(@Nullable T reference) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
throw new NullPointerException();
@ -161,6 +170,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static <T> T checkNotNull(@Nullable T reference, Object errorMessage) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
@ -177,6 +187,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static String checkNotEmpty(@Nullable String string) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && TextUtils.isEmpty(string)) {
throw new IllegalArgumentException();
@ -195,6 +206,7 @@ public final class Assertions {
*/
@SuppressWarnings({"contracts.postcondition.not.satisfied", "return.type.incompatible"})
@EnsuresNonNull({"#1"})
@Pure
public static String checkNotEmpty(@Nullable String string, Object errorMessage) {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && TextUtils.isEmpty(string)) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
@ -208,6 +220,7 @@ public final class Assertions {
*
* @throws IllegalStateException If the calling thread is not the application's main thread.
*/
@Pure
public static void checkMainThread() {
if (ExoPlayerLibraryInfo.ASSERTIONS_ENABLED && Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("Not in applications main thread");