From 4eb56abe8b98ed0380d68d335200e0e09d6c8392 Mon Sep 17 00:00:00 2001
From: Mathew Inwood
Date: Tue, 14 Aug 2018 17:24:32 +0100
Subject: Add @UnsupportedAppUsage annotations
For packages:
android.util.proto
android.util.jar
android.util.apk
android.util
This is an automatically generated CL. See go/UnsupportedAppUsage
for more details.
Exempted-From-Owner-Approval: Mechanical changes to the codebase
which have been approved by Android API council and announced on
android-eng@
Bug: 110868826
Test: m
Change-Id: Ia0f48c244b0fbe33d40d797702a82303648196ed
---
core/java/android/util/SparseArray.java | 4 ++++
1 file changed, 4 insertions(+)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index af18caa0e122..aa5ca3530621 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -19,6 +19,7 @@ package android.util;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
+import android.annotation.UnsupportedAppUsage;
import libcore.util.EmptyArray;
/**
@@ -55,8 +56,11 @@ public class SparseArray implements Cloneable {
private static final Object DELETED = new Object();
private boolean mGarbage = false;
+ @UnsupportedAppUsage
private int[] mKeys;
+ @UnsupportedAppUsage
private Object[] mValues;
+ @UnsupportedAppUsage
private int mSize;
/**
--
cgit v1.2.3
From a8a0435d59c777bec23bdd3533de884364fbb36a Mon Sep 17 00:00:00 2001
From: Jake Wharton
Date: Sat, 29 Sep 2018 01:52:24 -0400
Subject: Expose a few APIs in util collections.
These are either already exposed on other specialized collection variants or are exposed as public API on the androidx versions, or both.
With these APIs exposed, all of the unsupported app usage can be done through public API. As a result, all unsupported app usage is now locked to apps targeting API 28 or earlier.
Bug: 116877302
Test: none, no implementation change
Change-Id: I548d71319bffb0a6b529e380ea936df674dbf515
---
core/java/android/util/SparseArray.java | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index aa5ca3530621..89ea2d35fc2f 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -56,11 +56,11 @@ public class SparseArray 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;
/**
--
cgit v1.2.3
From 17d453ea85703a5bc26c0669231be4d6f0d8bbac Mon Sep 17 00:00:00 2001
From: Kweku Adams
Date: Tue, 5 Mar 2019 15:30:42 -0800
Subject: Checkng upper bound in *Array classes.
*Array classes will now throw an IndexOutOfBoundsException if a caller
tries to get or set a value for an index that's invalid based on the
current size.
Bug: 118339123
Test: atest CtsUtilTestCases
Change-Id: Iddc9a0c7c89e0ac743b0380049527a1b2dfb434f
---
core/java/android/util/SparseArray.java | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index 89ea2d35fc2f..67dfb02a0b95 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;
/**
@@ -171,6 +172,10 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public void removeAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
@@ -279,6 +284,10 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public int keyAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
@@ -302,6 +311,10 @@ public class SparseArray implements Cloneable {
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
@@ -317,6 +330,10 @@ public class SparseArray implements Cloneable {
* For indices outside of the range 0...size()-1, the behavior is undefined.
*/
public void setValueAt(int index, E value) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
--
cgit v1.2.3
From 91ec97056451753d6db55d310fc93fbd93c61cb3 Mon Sep 17 00:00:00 2001
From: Paul Scovanner
Date: Tue, 12 Mar 2019 18:03:48 +0000
Subject: Revert "Checkng upper bound in *Array classes."
This reverts commit 17d453ea85703a5bc26c0669231be4d6f0d8bbac.
Reason for revert: b/127750694
Change-Id: I0ffbf0e64109b3ec724e0687a27b231e335f76b4
---
core/java/android/util/SparseArray.java | 19 +------------------
1 file changed, 1 insertion(+), 18 deletions(-)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index 67dfb02a0b95..89ea2d35fc2f 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -16,11 +16,10 @@
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;
/**
@@ -172,10 +171,6 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public void removeAt(int index) {
- if (index >= mSize) {
- // The array might be slightly bigger than mSize, in which case, indexing won't fail.
- throw new ArrayIndexOutOfBoundsException(index);
- }
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
@@ -284,10 +279,6 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public int keyAt(int index) {
- if (index >= mSize) {
- // The array might be slightly bigger than mSize, in which case, indexing won't fail.
- throw new ArrayIndexOutOfBoundsException(index);
- }
if (mGarbage) {
gc();
}
@@ -311,10 +302,6 @@ public class SparseArray implements Cloneable {
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
- if (index >= mSize) {
- // The array might be slightly bigger than mSize, in which case, indexing won't fail.
- throw new ArrayIndexOutOfBoundsException(index);
- }
if (mGarbage) {
gc();
}
@@ -330,10 +317,6 @@ public class SparseArray implements Cloneable {
* For indices outside of the range 0...size()-1, the behavior is undefined.
*/
public void setValueAt(int index, E value) {
- if (index >= mSize) {
- // The array might be slightly bigger than mSize, in which case, indexing won't fail.
- throw new ArrayIndexOutOfBoundsException(index);
- }
if (mGarbage) {
gc();
}
--
cgit v1.2.3
From 025a1664abaafde21ff0262a3679a3e6aab853e7 Mon Sep 17 00:00:00 2001
From: Kweku Adams
Date: Sat, 30 Mar 2019 00:03:17 +0000
Subject: Revert "Revert "Checkng upper bound in *Array classes.""
This reverts commit 91ec97056451753d6db55d310fc93fbd93c61cb3.
Reason for revert: b/128433495
Change-Id: I4aac43f6aacd594f9c2bf58db9fbc4a1395d8888
---
core/java/android/util/SparseArray.java | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index 89ea2d35fc2f..67dfb02a0b95 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;
/**
@@ -171,6 +172,10 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public void removeAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
@@ -279,6 +284,10 @@ public class SparseArray implements Cloneable {
* the behavior is undefined.
*/
public int keyAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
@@ -302,6 +311,10 @@ public class SparseArray implements Cloneable {
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
@@ -317,6 +330,10 @@ public class SparseArray implements Cloneable {
* For indices outside of the range 0...size()-1, the behavior is undefined.
*/
public void setValueAt(int index, E value) {
+ if (index >= mSize) {
+ // The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
if (mGarbage) {
gc();
}
--
cgit v1.2.3
From 4be0b1acaf8f4752e5a5940b5fad0602588f4741 Mon Sep 17 00:00:00 2001
From: Kweku Adams
Date: Thu, 25 Apr 2019 16:16:34 -0700
Subject: 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
---
core/java/android/util/SparseArray.java | 25 +++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)
(limited to 'core/java/android/util/SparseArray.java')
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 implements Cloneable {
* Removes the mapping at the specified index.
*
* For indices outside of the range 0...size()-1,
- * the behavior is undefined.
+ * 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.
*/
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 implements Cloneable {
* key.
*
* For indices outside of the range 0...size()-1,
- * the behavior is undefined.
+ * 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.
*/
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 implements Cloneable {
* associated with the largest key.
*
* For indices outside of the range 0...size()-1,
- * the behavior is undefined.
+ * 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.
*/
@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 implements Cloneable {
* value for the indexth key-value mapping that this
* SparseArray stores.
*
- * For indices outside of the range 0...size()-1, the behavior is undefined.
+ * For indices outside of the range 0...size()-1, 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.
*/
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);
}
--
cgit v1.2.3
From 3858b2d1dcd9a5105f6a895b25d215a1d31ae5bc Mon Sep 17 00:00:00 2001
From: Kweku Adams
Date: Mon, 29 Apr 2019 11:47:41 -0700
Subject: Add extra comment for implementation.
Add a comment noting that the check to throw the exception is
intentionally second so that it's out of the critical path.
Bug: 118339123
Test: N/A
Change-Id: I36c5ea67579bcd7906f711530392110d9987ffb4
---
core/java/android/util/SparseArray.java | 4 ++++
1 file changed, 4 insertions(+)
(limited to 'core/java/android/util/SparseArray.java')
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index 7a8c780d665a..0a15db227823 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -176,6 +176,7 @@ public class SparseArray implements Cloneable {
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) {
@@ -290,6 +291,7 @@ public class SparseArray implements Cloneable {
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) {
@@ -319,6 +321,7 @@ public class SparseArray implements Cloneable {
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) {
@@ -341,6 +344,7 @@ public class SparseArray implements Cloneable {
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) {
--
cgit v1.2.3