diff options
| author | Kweku Adams <kwekua@google.com> | 2019-04-25 16:16:34 -0700 |
|---|---|---|
| committer | Kweku Adams <kwekua@google.com> | 2019-04-26 12:22:16 -0700 |
| commit | 4be0b1acaf8f4752e5a5940b5fad0602588f4741 (patch) | |
| tree | 6ba6d8ae56dbdaecbb5211c35b2d79638aee3f1f /core/java/android/util/SparseArray.java | |
| parent | a23242a492a9f0c893cd7d5a25f53983218a8d97 (diff) | |
Gating OutOfBoundsException on targetSdkVersion.
Apps targeting Pie or older will get the old undefined behavior. Apps
targeting Q or newer will get the OutOfBoundsException.
Bug: 118339123
Test: atest CtsUtilTestCases
Change-Id: Ibf5467aadec4a2f76ee180e963afeaf5a8a013a2
Diffstat (limited to 'core/java/android/util/SparseArray.java')
| -rw-r--r-- | core/java/android/util/SparseArray.java | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index 67dfb02a0b95..7a8c780d665a 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -169,10 +169,12 @@ 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) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { // The array might be slightly bigger than mSize, in which case, indexing won't fail. throw new ArrayIndexOutOfBoundsException(index); } @@ -281,10 +283,12 @@ 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) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { // The array might be slightly bigger than mSize, in which case, indexing won't fail. throw new ArrayIndexOutOfBoundsException(index); } @@ -307,11 +311,13 @@ 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) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { // The array might be slightly bigger than mSize, in which case, indexing won't fail. throw new ArrayIndexOutOfBoundsException(index); } @@ -327,10 +333,13 @@ 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) { + if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { // The array might be slightly bigger than mSize, in which case, indexing won't fail. throw new ArrayIndexOutOfBoundsException(index); } |
