From c7e1d3f22280485bb98a470b5d6afac1c7d14d3c Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Tue, 1 Dec 2020 12:30:50 +0000 Subject: [PATCH] Mark assertion methods as @Pure See https://checkerframework.org/manual/#type-refinement-purity. PiperOrigin-RevId: 344994705 --- .../google/android/exoplayer2/util/Assertions.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Assertions.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Assertions.java index 0f3bbfa14d..c6173730ff 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Assertions.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Assertions.java @@ -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 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 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 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 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");