Merge "Throw BadParcelableException from set/getParcelable"

This commit is contained in:
Treehugger Robot
2020-12-07 07:32:29 +00:00
committed by Gerrit Code Review
2 changed files with 20 additions and 12 deletions

View File

@@ -7286,7 +7286,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;
}

View File

@@ -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;