Skip to content

Commit

Permalink
Call getSuppressed() directly.
Browse files Browse the repository at this point in the history
We dropped support for Java 7 long ago, and we've required API Level 19 (nowadays 21: cl/628429745), which is enough for [Android support](https://developer.android.com/reference/java/lang/Throwable#getSuppressed()), for a while now, too.

RELNOTES=n/a
PiperOrigin-RevId: 686712422
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 17, 2024
1 parent b69c73e commit 62d61a1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 30 deletions.
21 changes: 0 additions & 21 deletions core/src/main/java/com/google/common/truth/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,6 @@ static boolean containsMatch(String actual, String regex) {
return Pattern.compile(regex).matcher(actual).find();
}

/**
* Returns an array containing all the exceptions that were suppressed to deliver the given
* exception. If suppressed exceptions are not supported (pre-Java 1.7), an empty array will be
* returned.
*/
static Throwable[] getSuppressed(Throwable throwable) {
try {
Method getSuppressed = throwable.getClass().getMethod("getSuppressed");
return (Throwable[]) checkNotNull(getSuppressed.invoke(throwable));
} catch (NoSuchMethodException e) {
return new Throwable[0];
} catch (IllegalAccessException e) {
// We're calling a public method on a public class.
throw newLinkageError(e);
} catch (InvocationTargetException e) {
throwIfUnchecked(e.getCause());
// getSuppressed has no `throws` clause.
throw newLinkageError(e);
}
}

static void cleanStackTrace(Throwable throwable) {
StackTraceCleaner.cleanStackTrace(throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void clean(Set<Throwable> seenThrowables) {
if (throwable.getCause() != null) {
new StackTraceCleaner(throwable.getCause()).clean(seenThrowables);
}
for (Throwable suppressed : Platform.getSuppressed(throwable)) {
for (Throwable suppressed : throwable.getSuppressed()) {
new StackTraceCleaner(suppressed).clean(seenThrowables);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ static boolean containsMatch(String subject, String regex) {
return compile(regex).test(subject);
}

/**
* Returns an array containing all the exceptions that were suppressed to deliver the given
* exception. Delegates to the getSuppressed() method on Throwable that is available in Java 1.7+
*/
static Throwable[] getSuppressed(Throwable throwable) {
return throwable.getSuppressed();
}

static void cleanStackTrace(Throwable throwable) {
// Do nothing. See notes in StackTraceCleanerTest.
}
Expand Down

0 comments on commit 62d61a1

Please sign in to comment.