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 /** 32 * Wrapper class that offers to transport typical {@link Throwable} across a 33 * {@link Binder} call. This class is typically used to transport exceptions 34 * that cannot be modified to add {@link Parcelable} behavior, such as 35 * {@link IOException}. 36 * <ul> 37 * <li>The wrapped throwable must be defined as system class (that is, it must 38 * be in the same {@link ClassLoader} as {@link Parcelable}). 39 * <li>The wrapped throwable must support the 40 * {@link Throwable#Throwable(String)} constructor. 41 * <li>The receiver side must catch any thrown {@link ParcelableException} and 42 * call {@link #maybeRethrow(Class)} for all expected exception types. 43 * </ul> 44 * 45 * @hide 46 */ 47 public final class ParcelableException extends RuntimeException implements Parcelable { ParcelableException(Throwable t)48 public ParcelableException(Throwable t) { 49 super(t); 50 } 51 52 /** Check class javadoc. */ 53 @SuppressWarnings("unchecked") maybeRethrow(Class<T> clazz)54 public <T extends Throwable> void maybeRethrow(Class<T> clazz) throws T { 55 if (clazz.isAssignableFrom(getCause().getClass())) { 56 throw (T) getCause(); 57 } 58 } 59 60 /** Check class javadoc. */ readFromParcel(Parcel in)61 public static Throwable readFromParcel(Parcel in) { 62 final String name = in.readString(); 63 final String msg = in.readString(); 64 try { 65 final Class<?> clazz = Class.forName(name, true, Parcelable.class.getClassLoader()); 66 if (Throwable.class.isAssignableFrom(clazz)) { 67 return (Throwable) clazz.getConstructor(String.class).newInstance(msg); 68 } 69 } catch (ReflectiveOperationException e) { 70 } 71 return new RuntimeException(name + ": " + msg); 72 } 73 74 /** Check class javadoc. */ writeToParcel(Parcel out, Throwable t)75 public static void writeToParcel(Parcel out, Throwable t) { 76 out.writeString(t.getClass().getName()); 77 out.writeString(t.getMessage()); 78 } 79 80 @Override 81 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) describeContents()82 public int describeContents() { 83 return 0; 84 } 85 86 @Override writeToParcel(Parcel dest, int flags)87 public void writeToParcel(Parcel dest, int flags) { 88 writeToParcel(dest, getCause()); 89 } 90 91 @NonNull 92 public static final Creator<ParcelableException> CREATOR = new Creator<ParcelableException>() { 93 @Override 94 public ParcelableException createFromParcel(Parcel source) { 95 return new ParcelableException(readFromParcel(source)); 96 } 97 98 @Override 99 public ParcelableException[] newArray(int size) { 100 return new ParcelableException[size]; 101 } 102 }; 103 } 104