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
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index 18d1064..184637a 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -7284,7 +7284,7 @@
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 95c07b6..881935d 100644
--- a/core/java/android/os/ParcelableHolder.java
+++ b/core/java/android/os/ParcelableHolder.java
@@ -120,31 +120,37 @@
/**
* 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 @@
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;