• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public final class ParcelableException extends RuntimeException implements Parcelable {
ParcelableException(Throwable t)45     public ParcelableException(Throwable t) {
46         super(t);
47     }
48 
49     /** Check class javadoc. */
50     @SuppressWarnings("unchecked")
maybeRethrow(Class<T> clazz)51     public <T extends Throwable> void maybeRethrow(Class<T> clazz) throws T {
52         if (clazz.isAssignableFrom(getCause().getClass())) {
53             throw (T) getCause();
54         }
55     }
56 
57     /** Check class javadoc. */
readFromParcel(Parcel in)58     public static Throwable readFromParcel(Parcel in) {
59         final String name = in.readString();
60         final String msg = in.readString();
61         try {
62             final Class<?> clazz = Class.forName(name, true, Parcelable.class.getClassLoader());
63             if (Throwable.class.isAssignableFrom(clazz)) {
64                 return (Throwable) clazz.getConstructor(String.class).newInstance(msg);
65             }
66         } catch (ReflectiveOperationException e) {
67         }
68         return new RuntimeException(name + ": " + msg);
69     }
70 
71     /** Check class javadoc. */
writeToParcel(Parcel out, Throwable t)72     public static void writeToParcel(Parcel out, Throwable t) {
73         out.writeString(t.getClass().getName());
74         out.writeString(t.getMessage());
75     }
76 
77     @Override
78     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
describeContents()79     public int describeContents() {
80         return 0;
81     }
82 
83     @Override
writeToParcel(Parcel dest, int flags)84     public void writeToParcel(Parcel dest, int flags) {
85         writeToParcel(dest, getCause());
86     }
87 
88     @NonNull
89     public static final Creator<ParcelableException> CREATOR = new Creator<ParcelableException>() {
90         @Override
91         public ParcelableException createFromParcel(Parcel source) {
92             return new ParcelableException(readFromParcel(source));
93         }
94 
95         @Override
96         public ParcelableException[] newArray(int size) {
97             return new ParcelableException[size];
98         }
99     };
100 }
101