|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 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 | + */ |
1 | 16 | package androidx.test.services.events.internal;
|
2 | 17 |
|
3 | 18 | import java.io.BufferedReader;
|
@@ -35,6 +50,8 @@ public static String getTrimmedStackTrace(Throwable exception) {
|
35 | 50 |
|
36 | 51 | StringBuilder result = new StringBuilder(exception.toString());
|
37 | 52 | appendStackTraceLines(trimmedStackTraceLines, result);
|
| 53 | + // Throwable.lockedPrintStackTrace prints suppressed exceptions before it prints the cause. |
| 54 | + appendStackTraceLines(getSuppressedStackTraceLines(exception), result); |
38 | 55 | appendStackTraceLines(getCauseStackTraceLines(exception), result);
|
39 | 56 | return result.toString();
|
40 | 57 | }
|
@@ -63,6 +80,27 @@ private static List<String> getTrimmedStackTraceLines(Throwable exception) {
|
63 | 80 | return Collections.emptyList();
|
64 | 81 | }
|
65 | 82 |
|
| 83 | + private static List<String> getSuppressedStackTraceLines(Throwable exception) { |
| 84 | + if (exception.getSuppressed() == null || exception.getSuppressed().length == 0) { |
| 85 | + return Collections.emptyList(); |
| 86 | + } |
| 87 | + List<String> lines = new ArrayList<>(); |
| 88 | + for (Throwable suppressed : exception.getSuppressed()) { |
| 89 | + lines.add( |
| 90 | + "\tSuppressed: " + suppressed.getClass().getName() + ": " + suppressed.getMessage()); |
| 91 | + for (String line : getTrimmedStackTraceLines(suppressed)) { |
| 92 | + if (line.isEmpty()) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + lines.add("\t" + line); |
| 96 | + } |
| 97 | + for (String line : getCauseStackTraceLines(suppressed)) { |
| 98 | + lines.add("\t" + line); |
| 99 | + } |
| 100 | + } |
| 101 | + return lines; |
| 102 | + } |
| 103 | + |
66 | 104 | private static List<String> getCauseStackTraceLines(Throwable exception) {
|
67 | 105 | if (exception.getCause() != null) {
|
68 | 106 | String fullTrace = getFullStackTrace(exception);
|
|
0 commit comments