Skip to content

Commit

Permalink
Migrate from soon-to-be-deprecated propagateIfPossible to equivalen…
Browse files Browse the repository at this point in the history
…t `throwIfInstanceOf` and `throwIfUnchecked` calls.

RELNOTES=n/a
PiperOrigin-RevId: 613203993
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Mar 6, 2024
1 parent c96c7d4 commit aa1df9f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 4 additions & 2 deletions android/guava-tests/test/com/google/common/io/CloserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.google.common.io;

import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -447,7 +448,8 @@ public boolean isClosed() {
public void close() throws IOException {
closed = true;
if (throwOnClose != null) {
Throwables.propagateIfPossible(throwOnClose, IOException.class);
throwIfInstanceOf(throwOnClose, IOException.class);
throwIfUnchecked(throwOnClose);
throw new AssertionError(throwOnClose);
}
}
Expand Down
20 changes: 13 additions & 7 deletions android/guava/src/com/google/common/io/Closer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package com.google.common.io;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Throwables.throwIfUnchecked;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -149,7 +150,8 @@ public static Closer create() {
public RuntimeException rethrow(Throwable e) throws IOException {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
throwIfInstanceOf(e, IOException.class);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand All @@ -171,8 +173,9 @@ public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> decl
throws IOException, X {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
Throwables.propagateIfPossible(e, declaredType);
throwIfInstanceOf(e, IOException.class);
throwIfInstanceOf(e, declaredType);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand All @@ -195,8 +198,10 @@ public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(
Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2 {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
Throwables.propagateIfPossible(e, declaredType1, declaredType2);
throwIfInstanceOf(e, IOException.class);
throwIfInstanceOf(e, declaredType1);
throwIfInstanceOf(e, declaredType2);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand Down Expand Up @@ -226,7 +231,8 @@ public void close() throws IOException {
}

if (thrown == null && throwable != null) {
Throwables.propagateIfPossible(throwable, IOException.class);
throwIfInstanceOf(throwable, IOException.class);
throwIfUnchecked(throwable);
throw new AssertionError(throwable); // not possible
}
}
Expand Down
6 changes: 4 additions & 2 deletions guava-tests/test/com/google/common/io/CloserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.google.common.io;

import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -447,7 +448,8 @@ public boolean isClosed() {
public void close() throws IOException {
closed = true;
if (throwOnClose != null) {
Throwables.propagateIfPossible(throwOnClose, IOException.class);
throwIfInstanceOf(throwOnClose, IOException.class);
throwIfUnchecked(throwOnClose);
throw new AssertionError(throwOnClose);
}
}
Expand Down
20 changes: 13 additions & 7 deletions guava/src/com/google/common/io/Closer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package com.google.common.io;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Throwables.throwIfUnchecked;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -149,7 +150,8 @@ public static Closer create() {
public RuntimeException rethrow(Throwable e) throws IOException {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
throwIfInstanceOf(e, IOException.class);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand All @@ -171,8 +173,9 @@ public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> decl
throws IOException, X {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
Throwables.propagateIfPossible(e, declaredType);
throwIfInstanceOf(e, IOException.class);
throwIfInstanceOf(e, declaredType);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand All @@ -195,8 +198,10 @@ public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(
Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2 {
checkNotNull(e);
thrown = e;
Throwables.propagateIfPossible(e, IOException.class);
Throwables.propagateIfPossible(e, declaredType1, declaredType2);
throwIfInstanceOf(e, IOException.class);
throwIfInstanceOf(e, declaredType1);
throwIfInstanceOf(e, declaredType2);
throwIfUnchecked(e);
throw new RuntimeException(e);
}

Expand Down Expand Up @@ -226,7 +231,8 @@ public void close() throws IOException {
}

if (thrown == null && throwable != null) {
Throwables.propagateIfPossible(throwable, IOException.class);
throwIfInstanceOf(throwable, IOException.class);
throwIfUnchecked(throwable);
throw new AssertionError(throwable); // not possible
}
}
Expand Down

0 comments on commit aa1df9f

Please sign in to comment.