summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jeongik Cha <jeongik@google.com> 2020-11-19 02:32:37 +0900
committer Jeongik Cha <jeongik@google.com> 2020-11-23 19:42:43 +0900
commiteaf5d14c1e907660b68d96d4744ece2b3b91e206 (patch)
treee612a90cdbeb4d4e07325e89239622c84a5e6ef3
parent8ffbbc693911f9f044665543a4308c31e7efef0a (diff)
Throw BadParcelableException from set/getParcelable
As-is: It returns false or null, so it could fail silently. To-be: In exceptional cases, it throws Runtime exception Test: atest aidl_integration_test Bug: 171982001 Change-Id: Ia5902bc2d35d7e72b5f014722453b6d9e313f1ed
-rw-r--r--core/api/system-current.txt2
-rw-r--r--core/java/android/os/ParcelableHolder.java30
2 files changed, 20 insertions, 12 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index 18d10641d0cd..184637a7c5af 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -7284,7 +7284,7 @@ package android.os {
method @Nullable public <T extends android.os.Parcelable> T getParcelable(@NonNull Class<T>);
method public int getStability();
method public void readFromParcel(@NonNull android.os.Parcel);
- method public boolean setParcelable(@Nullable android.os.Parcelable);
+ method public void setParcelable(@Nullable android.os.Parcelable);
method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.os.ParcelableHolder> CREATOR;
}
diff --git a/core/java/android/os/ParcelableHolder.java b/core/java/android/os/ParcelableHolder.java
index 95c07b6b2451..881935dc874c 100644
--- a/core/java/android/os/ParcelableHolder.java
+++ b/core/java/android/os/ParcelableHolder.java
@@ -120,31 +120,37 @@ public final class ParcelableHolder implements Parcelable {
/**
* Write a parcelable into ParcelableHolder, the previous parcelable will be removed.
- * @return {@code false} if the parcelable's stability is more unstable ParcelableHolder.
+ * @throws BadParcelableException if the parcelable's stability is more unstable
+ * ParcelableHolder.
*/
- public boolean setParcelable(@Nullable Parcelable p) {
- // a ParcelableHolder can only hold things at its stability or higher
+ public void setParcelable(@Nullable Parcelable p) {
+ // A ParcelableHolder can only hold things at its stability or higher.
if (p != null && this.getStability() > p.getStability()) {
- return false;
+ throw new BadParcelableException(
+ "A ParcelableHolder can only hold things at its stability or higher. "
+ + "The ParcelableHolder's stability is " + this.getStability()
+ + ", but the parcelable's stability is " + p.getStability());
}
mParcelable = p;
if (mParcel != null) {
mParcel.recycle();
mParcel = null;
}
- return true;
}
/**
* @return the parcelable that was written by {@link #setParcelable} or {@link #readFromParcel},
- * or {@code null} if the parcelable has not been written, or T is different from
- * the type written by (@link #setParcelable}.
+ * or {@code null} if the parcelable has not been written.
+ * @throws BadParcelableException if T is different from the type written by
+ * (@link #setParcelable}.
*/
@Nullable
public <T extends Parcelable> T getParcelable(@NonNull Class<T> clazz) {
if (mParcel == null) {
- if (!clazz.isInstance(mParcelable)) {
- return null;
+ if (mParcelable != null && !clazz.isInstance(mParcelable)) {
+ throw new BadParcelableException(
+ "The ParcelableHolder has " + mParcelable.getClass().getName()
+ + ", but the requested type is " + clazz.getName());
}
return (T) mParcelable;
}
@@ -152,8 +158,10 @@ public final class ParcelableHolder implements Parcelable {
mParcel.setDataPosition(0);
T parcelable = mParcel.readParcelable(clazz.getClassLoader());
- if (!clazz.isInstance(parcelable)) {
- return null;
+ if (parcelable != null && !clazz.isInstance(parcelable)) {
+ throw new BadParcelableException(
+ "The ParcelableHolder has " + parcelable.getClass().getName()
+ + ", but the requested type is " + clazz.getName());
}
mParcelable = parcelable;