1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.internal.util; 18 19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 20 21 import android.annotation.NonNull; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 26 27 import java.io.IOException; 28 29 // Copied from frameworks/base 30 /** 31 * Wrapper class that offers to transport typical {@link Throwable} across a 32 * {@link Binder} call. This class is typically used to transport exceptions 33 * that cannot be modified to add {@link Parcelable} behavior, such as 34 * {@link IOException}. 35 * <ul> 36 * <li>The wrapped throwable must be defined as system class (that is, it must 37 * be in the same {@link ClassLoader} as {@link Parcelable}). 38 * <li>The wrapped throwable must support the 39 * {@link Throwable#Throwable(String)} constructor. 40 * <li>The receiver side must catch any thrown {@link ParcelableException} and 41 * call {@link #maybeRethrow(Class)} for all expected exception types. 42 * </ul> 43 * 44 * @hide 45 */ 46 public final class ParcelableException extends RuntimeException implements Parcelable { ParcelableException(Throwable t)47 public ParcelableException(Throwable t) { 48 super(t); 49 } 50 51 /** Check class javadoc. */ 52 @SuppressWarnings("unchecked") maybeRethrow(Class<T> clazz)53 public <T extends Throwable> void maybeRethrow(Class<T> clazz) throws T { 54 if (clazz.isAssignableFrom(getCause().getClass())) { 55 throw (T) getCause(); 56 } 57 } 58 59 /** Check class javadoc. */ readFromParcel(Parcel in)60 public static Throwable readFromParcel(Parcel in) { 61 final String name = in.readString(); 62 final String msg = in.readString(); 63 try { 64 final Class<?> clazz = Class.forName(name, true, Parcelable.class.getClassLoader()); 65 if (Throwable.class.isAssignableFrom(clazz)) { 66 return (Throwable) clazz.getConstructor(String.class).newInstance(msg); 67 } 68 } catch (ReflectiveOperationException e) { 69 } 70 return new RuntimeException(name + ": " + msg); 71 } 72 73 /** Check class javadoc. */ writeToParcel(Parcel out, Throwable t)74 public static void writeToParcel(Parcel out, Throwable t) { 75 out.writeString(t.getClass().getName()); 76 out.writeString(t.getMessage()); 77 } 78 79 @Override 80 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) describeContents()81 public int describeContents() { 82 return 0; 83 } 84 85 @Override writeToParcel(Parcel dest, int flags)86 public void writeToParcel(Parcel dest, int flags) { 87 writeToParcel(dest, getCause()); 88 } 89 90 @NonNull 91 public static final Creator<ParcelableException> CREATOR = new Creator<ParcelableException>() { 92 @Override 93 public ParcelableException createFromParcel(Parcel source) { 94 return new ParcelableException(readFromParcel(source)); 95 } 96 97 @Override 98 public ParcelableException[] newArray(int size) { 99 return new ParcelableException[size]; 100 } 101 }; 102 } 103