diff options
Diffstat (limited to 'core/java/android/util/SparseArray.java')
| -rw-r--r-- | core/java/android/util/SparseArray.java | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index aa5ca3530621..0a15db227823 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -16,10 +16,11 @@ package android.util; +import android.annotation.UnsupportedAppUsage; + import com.android.internal.util.ArrayUtils; import com.android.internal.util.GrowingArrayUtils; -import android.annotation.UnsupportedAppUsage; import libcore.util.EmptyArray; /** @@ -56,11 +57,11 @@ public class SparseArray<E> implements Cloneable { private static final Object DELETED = new Object(); private boolean mGarbage = false; - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use keyAt(int) private int[] mKeys; - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use valueAt(int), setValueAt(int, E) private Object[] mValues; - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = 28) // Use size() private int mSize; /** @@ -168,9 +169,16 @@ public class SparseArray<E> implements Cloneable { * Removes the mapping at the specified index. * * <p>For indices outside of the range <code>0...size()-1</code>, - * the behavior is undefined.</p> + * the behavior is undefined for apps targeting {@link android.os.Build.VERSION_CODES#P} and + * earlier, and an {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public void removeAt(int index) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } if (mValues[index] != DELETED) { mValues[index] = DELETED; mGarbage = true; @@ -276,9 +284,16 @@ public class SparseArray<E> implements Cloneable { * key.</p> * * <p>For indices outside of the range <code>0...size()-1</code>, - * the behavior is undefined.</p> + * the behavior is undefined for apps targeting {@link android.os.Build.VERSION_CODES#P} and + * earlier, and an {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public int keyAt(int index) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } @@ -298,10 +313,17 @@ public class SparseArray<E> implements Cloneable { * associated with the largest key.</p> * * <p>For indices outside of the range <code>0...size()-1</code>, - * the behavior is undefined.</p> + * the behavior is undefined for apps targeting {@link android.os.Build.VERSION_CODES#P} and + * earlier, and an {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ @SuppressWarnings("unchecked") public E valueAt(int index) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } @@ -314,9 +336,17 @@ public class SparseArray<E> implements Cloneable { * value for the <code>index</code>th key-value mapping that this * SparseArray stores. * - * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined.</p> + * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for + * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an + * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting + * {@link android.os.Build.VERSION_CODES#Q} and later.</p> */ public void setValueAt(int index, E value) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + // Check if exception should be thrown outside of the critical path. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } |
