Skip to content

Commit

Permalink
Prepare for usage of ReflectionFreeAssertThrows from more packages:
Browse files Browse the repository at this point in the history
- Make `AbstractPackageSanityTests` ignore the class for simplicity. (But also add null checks (which solve only _part_ of the problem) to `ReflectionFreeAssertThrows` because why not.)
- Remove support for `util.concurrent` types so that we can also remove `util.concurrent` deps (mostly added during cl/675634517 and cl/686155720). I'll include support for those types in the packages that actually need it.

RELNOTES=n/a
PiperOrigin-RevId: 687009402
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 17, 2024
1 parent 9597b0d commit 8a1c90e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ public void testSerializable() throws Exception {
@Test
public void testNulls() throws Exception {
for (Class<?> classToTest : findClassesToTest(loadClassesInPackage(), NULL_TEST_METHOD_NAMES)) {
if (classToTest.getSimpleName().equals("ReflectionFreeAssertThrows")) {
/*
* These classes handle null properly but throw IllegalArgumentException for the default
* Class argument that this test uses. Normally we'd fix that by declaring a
* ReflectionFreeAssertThrowsTest with a testNulls method, but that's annoying to have to do
* for a package-private utility class. So we skip the class entirely instead.
*/
continue;
}
try {
tester.doTestNulls(classToTest, visibility);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.base;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand All @@ -22,8 +24,6 @@
import com.google.common.base.TestExceptions.SomeOtherCheckedException;
import com.google.common.base.TestExceptions.SomeUncheckedException;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.UnsupportedCharsetException;
Expand Down Expand Up @@ -67,6 +67,8 @@ static <T extends Throwable> T assertThrows(

private static <T extends Throwable> T doAssertThrows(
Class<T> expectedThrowable, ThrowingSupplier supplier, boolean userPassedSupplier) {
checkNotNull(expectedThrowable);
checkNotNull(supplier);
Predicate<Throwable> predicate = INSTANCE_OF.get(expectedThrowable);
if (predicate == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -132,7 +134,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(
ConcurrentModificationException.class,
e -> e instanceof ConcurrentModificationException)
.put(ExecutionError.class, e -> e instanceof ExecutionError)
.put(ExecutionException.class, e -> e instanceof ExecutionException)
.put(IllegalArgumentException.class, e -> e instanceof IllegalArgumentException)
.put(IllegalStateException.class, e -> e instanceof IllegalStateException)
Expand All @@ -146,7 +147,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(SomeOtherCheckedException.class, e -> e instanceof SomeOtherCheckedException)
.put(SomeUncheckedException.class, e -> e instanceof SomeUncheckedException)
.put(TimeoutException.class, e -> e instanceof TimeoutException)
.put(UncheckedExecutionException.class, e -> e instanceof UncheckedExecutionException)
.put(UnsupportedCharsetException.class, e -> e instanceof UnsupportedCharsetException)
.put(UnsupportedOperationException.class, e -> e instanceof UnsupportedOperationException)
.put(VerifyException.class, e -> e instanceof VerifyException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand All @@ -24,8 +26,6 @@
import com.google.common.collect.TestExceptions.SomeError;
import com.google.common.collect.TestExceptions.SomeOtherCheckedException;
import com.google.common.collect.TestExceptions.SomeUncheckedException;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.UnsupportedCharsetException;
Expand Down Expand Up @@ -69,6 +69,8 @@ static <T extends Throwable> T assertThrows(

private static <T extends Throwable> T doAssertThrows(
Class<T> expectedThrowable, ThrowingSupplier supplier, boolean userPassedSupplier) {
checkNotNull(expectedThrowable);
checkNotNull(supplier);
Predicate<Throwable> predicate = INSTANCE_OF.get(expectedThrowable);
if (predicate == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -134,7 +136,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(
ConcurrentModificationException.class,
e -> e instanceof ConcurrentModificationException)
.put(ExecutionError.class, e -> e instanceof ExecutionError)
.put(ExecutionException.class, e -> e instanceof ExecutionException)
.put(IllegalArgumentException.class, e -> e instanceof IllegalArgumentException)
.put(IllegalStateException.class, e -> e instanceof IllegalStateException)
Expand All @@ -149,7 +150,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(SomeOtherCheckedException.class, e -> e instanceof SomeOtherCheckedException)
.put(SomeUncheckedException.class, e -> e instanceof SomeUncheckedException)
.put(TimeoutException.class, e -> e instanceof TimeoutException)
.put(UncheckedExecutionException.class, e -> e instanceof UncheckedExecutionException)
.put(UnsupportedCharsetException.class, e -> e instanceof UnsupportedCharsetException)
.put(UnsupportedOperationException.class, e -> e instanceof UnsupportedOperationException)
.put(VerifyException.class, e -> e instanceof VerifyException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
V v8,
K k9,
V v9) {
return fromEntries(
/*
* This explicit type parameter works around what seems to be a javac bug in certain
* configurations: b/339186525#comment6
*/
return ImmutableSortedMap.<K, V>fromEntries(
entryOf(k1, v1),
entryOf(k2, v2),
entryOf(k3, v3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ public void testSerializable() throws Exception {
@Test
public void testNulls() throws Exception {
for (Class<?> classToTest : findClassesToTest(loadClassesInPackage(), NULL_TEST_METHOD_NAMES)) {
if (classToTest.getSimpleName().equals("ReflectionFreeAssertThrows")) {
/*
* These classes handle null properly but throw IllegalArgumentException for the default
* Class argument that this test uses. Normally we'd fix that by declaring a
* ReflectionFreeAssertThrowsTest with a testNulls method, but that's annoying to have to do
* for a package-private utility class. So we skip the class entirely instead.
*/
continue;
}
try {
tester.doTestNulls(classToTest, visibility);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.base;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand All @@ -22,8 +24,6 @@
import com.google.common.base.TestExceptions.SomeOtherCheckedException;
import com.google.common.base.TestExceptions.SomeUncheckedException;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.UnsupportedCharsetException;
Expand Down Expand Up @@ -67,6 +67,8 @@ static <T extends Throwable> T assertThrows(

private static <T extends Throwable> T doAssertThrows(
Class<T> expectedThrowable, ThrowingSupplier supplier, boolean userPassedSupplier) {
checkNotNull(expectedThrowable);
checkNotNull(supplier);
Predicate<Throwable> predicate = INSTANCE_OF.get(expectedThrowable);
if (predicate == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -132,7 +134,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(
ConcurrentModificationException.class,
e -> e instanceof ConcurrentModificationException)
.put(ExecutionError.class, e -> e instanceof ExecutionError)
.put(ExecutionException.class, e -> e instanceof ExecutionException)
.put(IllegalArgumentException.class, e -> e instanceof IllegalArgumentException)
.put(IllegalStateException.class, e -> e instanceof IllegalStateException)
Expand All @@ -146,7 +147,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(SomeOtherCheckedException.class, e -> e instanceof SomeOtherCheckedException)
.put(SomeUncheckedException.class, e -> e instanceof SomeUncheckedException)
.put(TimeoutException.class, e -> e instanceof TimeoutException)
.put(UncheckedExecutionException.class, e -> e instanceof UncheckedExecutionException)
.put(UnsupportedCharsetException.class, e -> e instanceof UnsupportedCharsetException)
.put(UnsupportedOperationException.class, e -> e instanceof UnsupportedOperationException)
.put(VerifyException.class, e -> e instanceof VerifyException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand All @@ -24,8 +26,6 @@
import com.google.common.collect.TestExceptions.SomeError;
import com.google.common.collect.TestExceptions.SomeOtherCheckedException;
import com.google.common.collect.TestExceptions.SomeUncheckedException;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.UnsupportedCharsetException;
Expand Down Expand Up @@ -69,6 +69,8 @@ static <T extends Throwable> T assertThrows(

private static <T extends Throwable> T doAssertThrows(
Class<T> expectedThrowable, ThrowingSupplier supplier, boolean userPassedSupplier) {
checkNotNull(expectedThrowable);
checkNotNull(supplier);
Predicate<Throwable> predicate = INSTANCE_OF.get(expectedThrowable);
if (predicate == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -134,7 +136,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(
ConcurrentModificationException.class,
e -> e instanceof ConcurrentModificationException)
.put(ExecutionError.class, e -> e instanceof ExecutionError)
.put(ExecutionException.class, e -> e instanceof ExecutionException)
.put(IllegalArgumentException.class, e -> e instanceof IllegalArgumentException)
.put(IllegalStateException.class, e -> e instanceof IllegalStateException)
Expand All @@ -149,7 +150,6 @@ ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() {
.put(SomeOtherCheckedException.class, e -> e instanceof SomeOtherCheckedException)
.put(SomeUncheckedException.class, e -> e instanceof SomeUncheckedException)
.put(TimeoutException.class, e -> e instanceof TimeoutException)
.put(UncheckedExecutionException.class, e -> e instanceof UncheckedExecutionException)
.put(UnsupportedCharsetException.class, e -> e instanceof UnsupportedCharsetException)
.put(UnsupportedOperationException.class, e -> e instanceof UnsupportedOperationException)
.put(VerifyException.class, e -> e instanceof VerifyException)
Expand Down
6 changes: 5 additions & 1 deletion guava/src/com/google/common/collect/ImmutableSortedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
V v8,
K k9,
V v9) {
return fromEntries(
/*
* This explicit type parameter works around what seems to be a javac bug in certain
* configurations: b/339186525#comment6
*/
return ImmutableSortedMap.<K, V>fromEntries(
entryOf(k1, v1),
entryOf(k2, v2),
entryOf(k3, v3),
Expand Down

0 comments on commit 8a1c90e

Please sign in to comment.