-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardize on
sneakyThrow
for "impossible" checked exceptions.
This makes our code behave the same for sneaky checked exceptions that occur _when our implementations use reflection_ as for those that occur when our implementations use direct calls. This shouldn't matter for any of the code that I'm migrating to `sneakyThrow` in this CL, but there are other cases in which it could matter, and we're probably best off standardizing on one approach, just as we've done for _catching_ sneaky checked exceptions. Plus, pull each package's `sneakyThrow` out into its own top-level class for reuse. Also, sneak in a few other tiny cleanups: - We don't normally use `@GwtCompatible` in Truth, so remove it from `J2ktIncompatible`. - We can now use `Primitives.wrap` to produce a better error message for `isIntanceOf(primitiveType)`. RELNOTES=n/a PiperOrigin-RevId: 710060382
- Loading branch information
Showing
20 changed files
with
368 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
android/guava-tests/test/com/google/common/base/SneakyThrows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2015 The Guava Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you | ||
* may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.google.common.base; | ||
|
||
import com.google.common.annotations.GwtCompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
|
||
/** Static utility method for throwing an undeclared checked exception. */ | ||
@GwtCompatible | ||
final class SneakyThrows<T extends Throwable> { | ||
/** | ||
* Throws an undeclared checked exception. | ||
* | ||
* @return never; this method declares a return type of {@link Error} only so that callers can | ||
* write {@code throw sneakyThrow(t);} to convince the compiler that the statement will always | ||
* throw. | ||
*/ | ||
@CanIgnoreReturnValue | ||
static Error sneakyThrow(Throwable t) { | ||
throw new SneakyThrows<Error>().throwIt(t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") // not really safe, but that's the point | ||
private Error throwIt(Throwable t) throws T { | ||
throw (T) t; | ||
} | ||
|
||
private SneakyThrows() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
android/guava/src/com/google/common/collect/SneakyThrows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2015 The Guava Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you | ||
* may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.google.common.collect; | ||
|
||
import com.google.common.annotations.GwtCompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
|
||
/** Static utility method for throwing an undeclared checked exception. */ | ||
@GwtCompatible | ||
final class SneakyThrows<T extends Throwable> { | ||
/** | ||
* Throws an undeclared checked exception. | ||
* | ||
* @return never; this method declares a return type of {@link Error} only so that callers can | ||
* write {@code throw sneakyThrow(t);} to convince the compiler that the statement will always | ||
* throw. | ||
*/ | ||
@CanIgnoreReturnValue | ||
static Error sneakyThrow(Throwable t) { | ||
throw new SneakyThrows<Error>().throwIt(t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") // not really safe, but that's the point | ||
private Error throwIt(Throwable t) throws T { | ||
throw (T) t; | ||
} | ||
|
||
private SneakyThrows() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
android/guava/src/com/google/common/hash/SneakyThrows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2015 The Guava Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you | ||
* may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.google.common.hash; | ||
|
||
import com.google.common.annotations.GwtCompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
|
||
/** Static utility method for throwing an undeclared checked exception. */ | ||
@GwtCompatible | ||
final class SneakyThrows<T extends Throwable> { | ||
/** | ||
* Throws an undeclared checked exception. | ||
* | ||
* @return never; this method declares a return type of {@link Error} only so that callers can | ||
* write {@code throw sneakyThrow(t);} to convince the compiler that the statement will always | ||
* throw. | ||
*/ | ||
@CanIgnoreReturnValue | ||
static Error sneakyThrow(Throwable t) { | ||
throw new SneakyThrows<Error>().throwIt(t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") // not really safe, but that's the point | ||
private Error throwIt(Throwable t) throws T { | ||
throw (T) t; | ||
} | ||
|
||
private SneakyThrows() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
android/guava/src/com/google/common/util/concurrent/SneakyThrows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2015 The Guava Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you | ||
* may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.google.common.util.concurrent; | ||
|
||
import com.google.common.annotations.GwtCompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
|
||
/** Static utility method for throwing an undeclared checked exception. */ | ||
@GwtCompatible | ||
final class SneakyThrows<T extends Throwable> { | ||
/** | ||
* Throws an undeclared checked exception. | ||
* | ||
* @return never; this method declares a return type of {@link Error} only so that callers can | ||
* write {@code throw sneakyThrow(t);} to convince the compiler that the statement will always | ||
* throw. | ||
*/ | ||
@CanIgnoreReturnValue | ||
static Error sneakyThrow(Throwable t) { | ||
throw new SneakyThrows<Error>().throwIt(t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") // not really safe, but that's the point | ||
private Error throwIt(Throwable t) throws T { | ||
throw (T) t; | ||
} | ||
|
||
private SneakyThrows() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.