diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eebb90c09508..8f3647af6aab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,13 +32,13 @@ jobs: steps: # Cancel any previous runs for the same branch that are still running. - name: 'Cancel previous runs' - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa + uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # 0.12.1 with: access_token: ${{ github.token }} - name: 'Check out repository' - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - name: 'Set up JDK ${{ matrix.java }}' - uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 + uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 # v4.1.0 with: java-version: ${{ matrix.java }} @@ -67,9 +67,9 @@ jobs: runs-on: ubuntu-latest steps: - name: 'Check out repository' - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - name: 'Set up JDK 21' - uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 + uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 # v4.1.0 with: java-version: 21 @@ -93,9 +93,9 @@ jobs: runs-on: ubuntu-latest steps: - name: 'Check out repository' - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - name: 'Set up JDK 21' - uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 + uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 # v4.1.0 with: java-version: 21 diff --git a/.github/workflows/gradle-wrapper-validation.yml b/.github/workflows/gradle-wrapper-validation.yml index 3a198d0b17a9..317c184351f5 100644 --- a/.github/workflows/gradle-wrapper-validation.yml +++ b/.github/workflows/gradle-wrapper-validation.yml @@ -9,5 +9,5 @@ jobs: name: "Validation" runs-on: ubuntu-latest steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - - uses: gradle/wrapper-validation-action@699bb18358f12c5b78b37bb0111d3a0e2276e0e2 + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + - uses: gradle/wrapper-validation-action@699bb18358f12c5b78b37bb0111d3a0e2276e0e2 # v2.1.1 diff --git a/android/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java b/android/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java index c8989d34d884..e87c42be38bc 100644 --- a/android/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java +++ b/android/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java @@ -75,7 +75,7 @@ protected abstract > Set copyOf( public void testCreation_noArgs() { Set set = of(); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCreation_oneElement() { @@ -122,7 +122,7 @@ public void testCopyOf_emptyArray() { String[] array = new String[0]; Set set = copyOf(array); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_arrayOfOneElement() { @@ -153,7 +153,7 @@ public void testCopyOf_collection_empty() { Collection c = MinimalCollection.of(); Set set = copyOf(c); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_collection_oneElement() { @@ -203,7 +203,7 @@ public void testCopyOf_iterator_empty() { Iterator iterator = Iterators.emptyIterator(); Set set = copyOf(iterator); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_iterator_oneElement() { diff --git a/android/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java b/android/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java index 3a46bb4c3034..67a47e67d355 100644 --- a/android/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java +++ b/android/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java @@ -249,11 +249,12 @@ public void testTransformEntrySetContains() { Set> entries = map.entrySet(); assertTrue(entries.contains(Maps.immutableEntry("a", true))); - assertTrue(entries.contains(Maps.immutableEntry("b", (Boolean) null))); - assertTrue(entries.contains(Maps.immutableEntry((String) null, (Boolean) null))); + assertTrue(entries.contains(Maps.immutableEntry("b", null))); + assertTrue( + entries.contains(Maps.<@Nullable String, @Nullable Boolean>immutableEntry(null, null))); - assertFalse(entries.contains(Maps.immutableEntry("c", (Boolean) null))); - assertFalse(entries.contains(Maps.immutableEntry((String) null, true))); + assertFalse(entries.contains(Maps.immutableEntry("c", null))); + assertFalse(entries.contains(Maps.<@Nullable String, Boolean>immutableEntry(null, true))); } @Override diff --git a/android/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java b/android/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java index d89be34623ae..caa40a63a122 100644 --- a/android/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java +++ b/android/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java @@ -25,6 +25,7 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Table} read operations. @@ -33,8 +34,8 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public abstract class AbstractTableReadTest extends TestCase { - protected Table table; +public abstract class AbstractTableReadTest extends TestCase { + protected Table table; /** * Creates a table with the specified data. @@ -43,7 +44,7 @@ public abstract class AbstractTableReadTest extends TestCase { * @throws IllegalArgumentException if the size of {@code data} isn't a multiple of 3 * @throws ClassCastException if a data element has the wrong type */ - protected abstract Table create(Object... data); + protected abstract Table create(@Nullable Object... data); protected void assertSize(int expectedSize) { assertEquals(expectedSize, table.size()); @@ -120,14 +121,13 @@ public void testSize() { public void testEquals() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table hashCopy = HashBasedTable.create(table); - Table reordered = - create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); - Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); - Table swapOuter = - create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); - Table swapValues = - create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); + // We know that we have only added non-null Characters. + Table hashCopy = + HashBasedTable.create((Table) table); + Table reordered = create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); + Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); + Table swapOuter = create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); + Table swapValues = create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); new EqualsTester() .addEqualityGroup(table, hashCopy, reordered) diff --git a/android/guava-tests/test/com/google/common/collect/AbstractTableTest.java b/android/guava-tests/test/com/google/common/collect/AbstractTableTest.java index c8d9052d9c60..0639f2d325d0 100644 --- a/android/guava-tests/test/com/google/common/collect/AbstractTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/AbstractTableTest.java @@ -20,6 +20,8 @@ import com.google.common.annotations.GwtCompatible; import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for a {@link Table} implementation supporting reads and writes. @@ -29,12 +31,14 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public abstract class AbstractTableTest extends AbstractTableReadTest { +public abstract class AbstractTableTest + extends AbstractTableReadTest { - protected void populate(Table table, Object... data) { + protected void populate(Table table, @Nullable Object... data) { checkArgument(data.length % 3 == 0); for (int i = 0; i < data.length; i += 3) { - table.put((String) data[i], (Integer) data[i + 1], (Character) data[i + 2]); + table.put( + (String) data[i], (Integer) data[i + 1], nullableCellValue((Character) data[i + 2])); } } @@ -62,29 +66,28 @@ public void testClear() { } public void testPut() { - assertNull(table.put("foo", 1, 'a')); - assertNull(table.put("bar", 1, 'b')); - assertNull(table.put("foo", 3, 'c')); - assertEquals((Character) 'a', table.put("foo", 1, 'd')); + assertNull(table.put("foo", 1, cellValue('a'))); + assertNull(table.put("bar", 1, cellValue('b'))); + assertNull(table.put("foo", 3, cellValue('c'))); + assertEquals((Character) 'a', table.put("foo", 1, cellValue('d'))); assertEquals((Character) 'd', table.get("foo", 1)); assertEquals((Character) 'b', table.get("bar", 1)); assertSize(3); - assertEquals((Character) 'd', table.put("foo", 1, 'd')); + assertEquals((Character) 'd', table.put("foo", 1, cellValue('d'))); assertEquals((Character) 'd', table.get("foo", 1)); assertSize(3); } - // This test assumes that the implementation does not support nulls. public void testPutNull() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); assertSize(3); try { - table.put(null, 2, 'd'); + table.put(null, 2, cellValue('d')); fail(); } catch (NullPointerException expected) { } try { - table.put("cat", null, 'd'); + table.put("cat", null, cellValue('d')); fail(); } catch (NullPointerException expected) { } @@ -105,11 +108,11 @@ public void testPutNullReplace() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); if (supportsNullValues()) { - assertEquals((Character) 'b', table.put("bar", 1, null)); + assertEquals((Character) 'b', table.put("bar", 1, nullableCellValue(null))); assertNull(table.get("bar", 1)); } else { try { - table.put("bar", 1, null); + table.put("bar", 1, nullableCellValue(null)); fail(); } catch (NullPointerException expected) { } @@ -118,10 +121,10 @@ public void testPutNullReplace() { public void testPutAllTable() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table other = HashBasedTable.create(); - other.put("foo", 1, 'd'); - other.put("bar", 2, 'e'); - other.put("cat", 2, 'f'); + Table other = HashBasedTable.create(); + other.put("foo", 1, cellValue('d')); + other.put("bar", 2, cellValue('e')); + other.put("cat", 2, cellValue('f')); table.putAll(other); assertEquals((Character) 'd', table.get("foo", 1)); assertEquals((Character) 'b', table.get("bar", 1)); @@ -159,18 +162,29 @@ public void testRemove() { public void testRowClearAndPut() { if (supportsRemove()) { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Map row = table.row("foo"); + Map row = table.row("foo"); assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), row); table.remove("foo", 3); assertEquals(ImmutableMap.of(1, 'a'), row); table.remove("foo", 1); assertEquals(ImmutableMap.of(), row); - table.put("foo", 2, 'b'); + table.put("foo", 2, cellValue('b')); assertEquals(ImmutableMap.of(2, 'b'), row); row.clear(); assertEquals(ImmutableMap.of(), row); - table.put("foo", 5, 'x'); + table.put("foo", 5, cellValue('x')); assertEquals(ImmutableMap.of(5, 'x'), row); } } + + @SuppressWarnings("unchecked") // C can only be @Nullable Character or Character + protected @NonNull C cellValue(Character character) { + return (C) character; + } + + // Only safe wrt. ClassCastException. Not null-safe (can be used to test expected Table NPEs) + @SuppressWarnings("unchecked") + protected C nullableCellValue(@Nullable Character character) { + return (C) character; + } } diff --git a/android/guava-tests/test/com/google/common/collect/ArrayTableTest.java b/android/guava-tests/test/com/google/common/collect/ArrayTableTest.java index 77abcc3e0734..f97a4393da7f 100644 --- a/android/guava-tests/test/com/google/common/collect/ArrayTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/ArrayTableTest.java @@ -38,10 +38,10 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class ArrayTableTest extends AbstractTableTest { +public class ArrayTableTest extends AbstractTableTest<@Nullable Character> { @Override - protected ArrayTable create(Object... data) { + protected ArrayTable create(@Nullable Object... data) { // TODO: Specify different numbers of rows and columns, to detect problems // that arise when the wrong size is used. ArrayTable table = @@ -128,12 +128,12 @@ public void testEquals() { hashCopy.put("foo", 1, 'a'); hashCopy.put("bar", 1, 'b'); hashCopy.put("foo", 3, 'c'); - Table reordered = + Table reordered = create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); - Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); - Table swapOuter = + Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); + Table swapOuter = create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); - Table swapValues = + Table swapValues = create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); new EqualsTester() @@ -242,9 +242,9 @@ public void testEmptyToArry() { } public void testCreateCopyArrayTable() { - Table original = + Table original = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table copy = ArrayTable.create(original); + Table copy = ArrayTable.create(original); assertEquals(original, copy); original.put("foo", 1, 'd'); assertEquals((Character) 'd', original.get("foo", 1)); @@ -258,7 +258,7 @@ public void testCreateCopyHashBasedTable() { original.put("foo", 1, 'a'); original.put("bar", 1, 'b'); original.put("foo", 3, 'c'); - Table copy = ArrayTable.create(original); + Table copy = ArrayTable.create(original); assertEquals(4, copy.size()); assertEquals((Character) 'a', copy.get("foo", 1)); assertEquals((Character) 'b', copy.get("bar", 1)); @@ -281,7 +281,7 @@ public void testCreateCopyEmptyTable() { } public void testCreateCopyEmptyArrayTable() { - Table original = + Table original = ArrayTable.create(Arrays.asList(), Arrays.asList()); ArrayTable copy = ArrayTable.create(original); assertThat(copy).isEqualTo(original); diff --git a/android/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java b/android/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java index daa929df6c2c..b021a0de47f0 100644 --- a/android/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java +++ b/android/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java @@ -52,7 +52,7 @@ public ForwardingSortedMapImplementsMapTest() { @Override protected SortedMap makeEmptyMap() { return new SimpleForwardingSortedMap<>( - new TreeMap(Ordering.natural().nullsFirst())); + new TreeMap(Ordering.natural().nullsFirst())); } @Override diff --git a/android/guava-tests/test/com/google/common/collect/GeneralRangeTest.java b/android/guava-tests/test/com/google/common/collect/GeneralRangeTest.java index d2b1d488878d..c50f1f5c46d1 100644 --- a/android/guava-tests/test/com/google/common/collect/GeneralRangeTest.java +++ b/android/guava-tests/test/com/google/common/collect/GeneralRangeTest.java @@ -35,7 +35,8 @@ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault public class GeneralRangeTest extends TestCase { - private static final Ordering<@Nullable Integer> ORDERING = Ordering.natural().nullsFirst(); + private static final Ordering<@Nullable Integer> ORDERING = + Ordering.natural().nullsFirst(); private static final List<@Nullable Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5); @@ -150,7 +151,7 @@ public void testIntersectAgainstBiggerRange() { assertEquals( GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), - range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED))); + range.intersect(GeneralRange.<@Nullable Integer>range(ORDERING, null, OPEN, 5, CLOSED))); assertEquals( GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), diff --git a/android/guava-tests/test/com/google/common/collect/HashBasedTableTest.java b/android/guava-tests/test/com/google/common/collect/HashBasedTableTest.java index fac4794dfff2..15ca3482d2e6 100644 --- a/android/guava-tests/test/com/google/common/collect/HashBasedTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/HashBasedTableTest.java @@ -23,6 +23,7 @@ import com.google.common.annotations.J2ktIncompatible; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link HashBasedTable}. @@ -31,10 +32,10 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class HashBasedTableTest extends AbstractTableTest { +public class HashBasedTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table table = HashBasedTable.create(); table.put("foo", 4, 'a'); table.put("cat", 1, 'b'); diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java index 59c8e3361214..a5f501ea5ad4 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java @@ -383,10 +383,10 @@ public void testCopyOfNullKey() { } public void testCopyOfNullValue() { - ArrayListMultimap input = ArrayListMultimap.create(); - input.putAll("foo", Arrays.asList(1, null, 3)); + ArrayListMultimap input = ArrayListMultimap.create(); + input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3)); try { - ImmutableListMultimap.copyOf(input); + ImmutableListMultimap.copyOf((ArrayListMultimap) input); fail(); } catch (NullPointerException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java index 7fe9305ac6b9..46ac06279f63 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableListTest.java @@ -635,9 +635,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() { } builder = ImmutableList.builder(); - Iterator iteratorWithNulls = asList("a", null, "b").iterator(); + Iterator<@Nullable String> iteratorWithNulls = + Arrays.<@Nullable String>asList("a", null, "b").iterator(); try { - builder.addAll(iteratorWithNulls); + builder.addAll((Iterator) iteratorWithNulls); fail("expected NullPointerException"); } catch (NullPointerException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java index 1bf1f4f841f5..815c7529eb3b 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java @@ -37,6 +37,7 @@ import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; @@ -264,9 +265,10 @@ public void testCopyOf_multiset_general() { } public void testCopyOf_multisetContainingNull() { - Multiset c = HashMultiset.create(asList("a", null, "b")); + Multiset<@Nullable String> c = + HashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b")); try { - ImmutableMultiset.copyOf(c); + ImmutableMultiset.copyOf((Multiset) c); fail(); } catch (NullPointerException expected) { } @@ -291,9 +293,10 @@ public void testCopyOf_iterator_general() { } public void testCopyOf_iteratorContainingNull() { - Iterator iterator = asList("a", null, "b").iterator(); + Iterator<@Nullable String> iterator = + Arrays.<@Nullable String>asList("a", null, "b").iterator(); try { - ImmutableMultiset.copyOf(iterator); + ImmutableMultiset.copyOf((Iterator) iterator); fail(); } catch (NullPointerException expected) { } @@ -430,9 +433,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() { } builder = ImmutableMultiset.builder(); - Multiset multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b")); + Multiset<@Nullable String> multisetWithNull = + LinkedHashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b")); try { - builder.addAll(multisetWithNull); + builder.addAll((Multiset) multisetWithNull); fail("expected NullPointerException"); } catch (NullPointerException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java index 33c6c0f0c570..a8663f619419 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java @@ -396,10 +396,10 @@ public void testCopyOfNullKey() { } public void testCopyOfNullValue() { - HashMultimap input = HashMultimap.create(); - input.putAll("foo", Arrays.asList(1, null, 3)); + HashMultimap input = HashMultimap.create(); + input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3)); try { - ImmutableSetMultimap.copyOf(input); + ImmutableSetMultimap.copyOf((Multimap) input); fail(); } catch (NullPointerException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java index 1bdc6092f4f7..2180f947dfb0 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java @@ -511,7 +511,7 @@ public void testCopyOf() { } public void testCopyOfExplicitComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); Map original = new LinkedHashMap<>(); original.put("one", 1); original.put("two", 2); @@ -524,7 +524,7 @@ public void testCopyOfExplicitComparator() { } public void testCopyOfImmutableSortedSetDifferentComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); Map original = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3); ImmutableSortedMap copy = ImmutableSortedMap.copyOf(original, comparator); assertMapEquals(copy, "two", 2, "three", 3, "one", 1); @@ -545,7 +545,7 @@ public void testCopyOfSortedNatural() { } public void testCopyOfSortedExplicit() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); SortedMap original = Maps.newTreeMap(comparator); original.put("one", 1); original.put("two", 2); @@ -613,11 +613,11 @@ public void testBuilderReverseOrder() { .put("five", 5) .build(); assertMapEquals(map, "two", 2, "three", 3, "one", 1, "four", 4, "five", 5); - assertEquals(Ordering.natural().reverse(), map.comparator()); + assertEquals(Ordering.natural().reverse(), map.comparator()); } public void testBuilderComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); ImmutableSortedMap map = new ImmutableSortedMap.Builder(comparator) .put("one", 1) diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java index 644d474b8d31..4f518b6eb8b3 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java @@ -293,8 +293,8 @@ public void testSingle_headSet() { SortedSet set = of("e"); assertTrue(set.headSet("g") instanceof ImmutableSortedSet); assertThat(set.headSet("g")).contains("e"); - assertSame(of(), set.headSet("c")); - assertSame(of(), set.headSet("e")); + assertSame(this.of(), set.headSet("c")); + assertSame(this.of(), set.headSet("e")); } public void testSingle_tailSet() { @@ -302,7 +302,7 @@ public void testSingle_tailSet() { assertTrue(set.tailSet("c") instanceof ImmutableSortedSet); assertThat(set.tailSet("c")).contains("e"); assertThat(set.tailSet("e")).contains("e"); - assertSame(of(), set.tailSet("g")); + assertSame(this.of(), set.tailSet("g")); } public void testSingle_subSet() { @@ -310,9 +310,9 @@ public void testSingle_subSet() { assertTrue(set.subSet("c", "g") instanceof ImmutableSortedSet); assertThat(set.subSet("c", "g")).contains("e"); assertThat(set.subSet("e", "g")).contains("e"); - assertSame(of(), set.subSet("f", "g")); - assertSame(of(), set.subSet("c", "e")); - assertSame(of(), set.subSet("c", "d")); + assertSame(this.of(), set.subSet("f", "g")); + assertSame(this.of(), set.subSet("c", "e")); + assertSame(this.of(), set.subSet("c", "d")); } public void testSingle_first() { @@ -398,8 +398,8 @@ public void testOf_headSet() { assertTrue(set.headSet("e") instanceof ImmutableSortedSet); assertThat(set.headSet("e")).containsExactly("b", "c", "d").inOrder(); assertThat(set.headSet("g")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.headSet("a")); - assertSame(of(), set.headSet("b")); + assertSame(this.of(), set.headSet("a")); + assertSame(this.of(), set.headSet("b")); } public void testOf_tailSet() { @@ -407,7 +407,7 @@ public void testOf_tailSet() { assertTrue(set.tailSet("e") instanceof ImmutableSortedSet); assertThat(set.tailSet("e")).containsExactly("e", "f").inOrder(); assertThat(set.tailSet("a")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.tailSet("g")); + assertSame(this.of(), set.tailSet("g")); } public void testOf_subSet() { @@ -415,9 +415,9 @@ public void testOf_subSet() { assertTrue(set.subSet("c", "e") instanceof ImmutableSortedSet); assertThat(set.subSet("c", "e")).containsExactly("c", "d").inOrder(); assertThat(set.subSet("a", "g")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.subSet("a", "b")); - assertSame(of(), set.subSet("g", "h")); - assertSame(of(), set.subSet("c", "c")); + assertSame(this.of(), set.subSet("a", "b")); + assertSame(this.of(), set.subSet("g", "h")); + assertSame(this.of(), set.subSet("c", "c")); try { set.subSet("e", "c"); fail(); @@ -1138,7 +1138,7 @@ private static ImmutableList sortedNumberNames(int i, int j) { ImmutableList.of("one", "two", "three", "four", "five", "six", "seven"); private static final ImmutableList SORTED_NUMBER_NAMES = - Ordering.natural().immutableSortedCopy(NUMBER_NAMES); + Ordering.natural().immutableSortedCopy(NUMBER_NAMES); private static class SelfComparableExample implements Comparable { @Override diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableTableTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableTableTest.java index a3cf4e6b1685..7c9aa8db5959 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableTableTest.java @@ -31,9 +31,9 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class ImmutableTableTest extends AbstractTableReadTest { +public class ImmutableTableTest extends AbstractTableReadTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { ImmutableTable.Builder builder = ImmutableTable.builder(); for (int i = 0; i < data.length; i = i + 3) { builder.put((String) data[i], (Integer) data[i + 1], (Character) data[i + 2]); diff --git a/android/guava-tests/test/com/google/common/collect/IterablesTest.java b/android/guava-tests/test/com/google/common/collect/IterablesTest.java index 5a049862ba2f..9d3ab7a35494 100644 --- a/android/guava-tests/test/com/google/common/collect/IterablesTest.java +++ b/android/guava-tests/test/com/google/common/collect/IterablesTest.java @@ -179,7 +179,7 @@ public void testGetOnlyElement_withDefault_empty() { public void testGetOnlyElement_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getOnlyElement(iterable, null)); + assertNull(Iterables.<@Nullable String>getOnlyElement(iterable, null)); } public void testGetOnlyElement_withDefault_multiple() { @@ -479,7 +479,7 @@ public void testPaddedPartition_basic() { List list = asList(1, 2, 3, 4, 5); Iterable> partitions = Iterables.paddedPartition(list, 2); assertEquals(3, Iterables.size(partitions)); - assertEquals(asList(5, null), Iterables.getLast(partitions)); + assertEquals(Arrays.<@Nullable Integer>asList(5, null), Iterables.getLast(partitions)); } public void testPaddedPartitionRandomAccessInput() { @@ -537,7 +537,7 @@ public void testElementsEqual() throws Exception { assertFalse(Iterables.elementsEqual(a, b)); // null versus non-null. - a = asList(4, 8, 15, null, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42); b = asList(4, 8, 15, 16, 23, 42); assertFalse(Iterables.elementsEqual(a, b)); assertFalse(Iterables.elementsEqual(b, a)); @@ -824,7 +824,7 @@ public void testGetFirst_withDefault_empty() { public void testGetFirst_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getFirst(iterable, null)); + assertNull(Iterables.<@Nullable String>getFirst(iterable, null)); } public void testGetFirst_withDefault_multiple() { @@ -863,7 +863,7 @@ public void testGetLast_withDefault_empty() { public void testGetLast_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getLast(iterable, null)); + assertNull(Iterables.<@Nullable String>getLast(iterable, null)); } public void testGetLast_withDefault_multiple() { @@ -1350,7 +1350,7 @@ public void testMergeSorted_pyramid() { list.add(j); allIntegers.add(j); } - iterables.add(Ordering.natural().sortedCopy(list)); + iterables.add(Ordering.natural().sortedCopy(list)); } verifyMergeSorted(iterables, allIntegers); @@ -1367,7 +1367,7 @@ public void testMergeSorted_skipping_pyramid() { list.add(j * i); allIntegers.add(j * i); } - iterables.add(Ordering.natural().sortedCopy(list)); + iterables.add(Ordering.natural().sortedCopy(list)); } verifyMergeSorted(iterables, allIntegers); @@ -1385,7 +1385,7 @@ public void testIterables_nullCheck() throws Exception { private static void verifyMergeSorted( Iterable> iterables, Iterable unsortedExpected) { - Iterable expected = Ordering.natural().sortedCopy(unsortedExpected); + Iterable expected = Ordering.natural().sortedCopy(unsortedExpected); Iterable mergedIterator = Iterables.mergeSorted(iterables, Ordering.natural()); diff --git a/android/guava-tests/test/com/google/common/collect/IteratorsTest.java b/android/guava-tests/test/com/google/common/collect/IteratorsTest.java index 6d4b4307f237..aba34944c1fa 100644 --- a/android/guava-tests/test/com/google/common/collect/IteratorsTest.java +++ b/android/guava-tests/test/com/google/common/collect/IteratorsTest.java @@ -162,7 +162,7 @@ public void testSize_partiallyConsumed() { } public void test_contains_nonnull_yes() { - Iterator set = asList("a", null, "b").iterator(); + Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator(); assertTrue(Iterators.contains(set, "b")); } @@ -172,7 +172,7 @@ public void test_contains_nonnull_no() { } public void test_contains_null_yes() { - Iterator set = asList("a", null, "b").iterator(); + Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator(); assertTrue(Iterators.contains(set, null)); } @@ -241,7 +241,7 @@ public void testGetOnlyElement_withDefault_empty() { public void testGetOnlyElement_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getOnlyElement(iterator, null)); + assertNull(Iterators.<@Nullable String>getOnlyElement(iterator, null)); } public void testGetOnlyElement_withDefault_two() { @@ -821,7 +821,10 @@ public void testConcatPartiallyAdvancedFirst() { /** Illustrates the somewhat bizarre behavior when a null is passed in. */ public void testConcatContainingNull() { - Iterator> input = asList(iterateOver(1, 2), null, iterateOver(3)).iterator(); + Iterator> input = + (Iterator>) + Arrays.<@Nullable Iterator>asList(iterateOver(1, 2), null, iterateOver(3)) + .iterator(); Iterator result = Iterators.concat(input); assertEquals(1, (int) result.next()); assertEquals(2, (int) result.next()); @@ -939,8 +942,8 @@ public void testElementsEqual() { assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); // The same, but with nulls. - a = asList(4, 8, null, 16, 23, 42); - b = asList(4, 8, null, 16, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42); + b = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42); assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); // Different Iterable types (still equal elements, though). @@ -954,7 +957,7 @@ public void testElementsEqual() { assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); // null versus non-null. - a = asList(4, 8, 15, null, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42); b = asList(4, 8, 15, 16, 23, 42); assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator())); @@ -1072,17 +1075,17 @@ public void testPaddedPartition_singleton2() { Iterator> partitions = Iterators.paddedPartition(source, 2); assertTrue(partitions.hasNext()); assertTrue(partitions.hasNext()); - assertEquals(asList(1, null), partitions.next()); + assertEquals(Arrays.<@Nullable Integer>asList(1, null), partitions.next()); assertFalse(partitions.hasNext()); } @GwtIncompatible // fairly slow (~50s) public void testPaddedPartition_general() { + ImmutableList> expectedElements = + ImmutableList.of( + asList(1, 2, 3), asList(4, 5, 6), Arrays.<@Nullable Integer>asList(7, null, null)); new IteratorTester>( - 5, - IteratorFeature.UNMODIFIABLE, - ImmutableList.of(asList(1, 2, 3), asList(4, 5, 6), asList(7, null, null)), - IteratorTester.KnownOrder.KNOWN_ORDER) { + 5, IteratorFeature.UNMODIFIABLE, expectedElements, IteratorTester.KnownOrder.KNOWN_ORDER) { @Override protected Iterator> newTargetIterator() { Iterator source = Iterators.forArray(1, 2, 3, 4, 5, 6, 7); @@ -1287,7 +1290,8 @@ public void testToString() { } public void testToStringWithNull() { - Iterator iterator = Lists.newArrayList("hello", null, "world").iterator(); + Iterator<@Nullable String> iterator = + Lists.<@Nullable String>newArrayList("hello", null, "world").iterator(); assertEquals("[hello, null, world]", Iterators.toString(iterator)); } @@ -1355,7 +1359,7 @@ public void testGetNext_withDefault_empty() { public void testGetNext_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getNext(iterator, null)); + assertNull(Iterators.<@Nullable String>getNext(iterator, null)); } public void testGetNext_withDefault_two() { @@ -1391,7 +1395,7 @@ public void testGetLast_withDefault_empty() { public void testGetLast_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getLast(iterator, null)); + assertNull(Iterators.<@Nullable String>getLast(iterator, null)); } public void testGetLast_withDefault_two() { diff --git a/android/guava-tests/test/com/google/common/collect/MapsTest.java b/android/guava-tests/test/com/google/common/collect/MapsTest.java index e910f1d3500d..07bdee8dfb1d 100644 --- a/android/guava-tests/test/com/google/common/collect/MapsTest.java +++ b/android/guava-tests/test/com/google/common/collect/MapsTest.java @@ -95,8 +95,7 @@ public void testHashMapGeneralizesTypes() { original.put("a", 1); original.put("b", 2); original.put("c", 3); - HashMap map = - Maps.newHashMap((Map) original); + HashMap map = Maps.newHashMap(original); assertEquals(original, map); } diff --git a/android/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java b/android/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java index 049c1ea7df85..3583c5675848 100644 --- a/android/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java +++ b/android/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java @@ -358,11 +358,12 @@ public void testTransformEntrySetContains() { Set> entries = map.entrySet(); assertTrue(entries.contains(Maps.immutableEntry("a", true))); - assertTrue(entries.contains(Maps.immutableEntry("b", (Boolean) null))); - assertTrue(entries.contains(Maps.immutableEntry((String) null, (Boolean) null))); + assertTrue(entries.contains(Maps.immutableEntry("b", null))); + assertTrue( + entries.contains(Maps.<@Nullable String, @Nullable Boolean>immutableEntry(null, null))); - assertFalse(entries.contains(Maps.immutableEntry("c", (Boolean) null))); - assertFalse(entries.contains(Maps.immutableEntry((String) null, true))); + assertFalse(entries.contains(Maps.immutableEntry("c", null))); + assertFalse(entries.contains(Maps.<@Nullable String, Boolean>immutableEntry(null, true))); } @Override diff --git a/android/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java b/android/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java index 83a835609a3a..ca19020ab9fd 100644 --- a/android/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java +++ b/android/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java @@ -59,7 +59,7 @@ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault public class MinMaxPriorityQueueTest extends TestCase { - private static final Ordering SOME_COMPARATOR = Ordering.natural().reverse(); + private static final Ordering SOME_COMPARATOR = Ordering.natural().reverse(); @J2ktIncompatible @GwtIncompatible // suite diff --git a/android/guava-tests/test/com/google/common/collect/MultimapsTest.java b/android/guava-tests/test/com/google/common/collect/MultimapsTest.java index c02d1b277a90..37ad3fece911 100644 --- a/android/guava-tests/test/com/google/common/collect/MultimapsTest.java +++ b/android/guava-tests/test/com/google/common/collect/MultimapsTest.java @@ -110,7 +110,8 @@ public void testUnmodifiableMultimapShortCircuit() { @GwtIncompatible // slow (~10s) public void testUnmodifiableArrayListMultimap() { - checkUnmodifiableMultimap(ArrayListMultimap.create(), true); + checkUnmodifiableMultimap( + ArrayListMultimap.<@Nullable String, @Nullable Integer>create(), true); } @J2ktIncompatible @@ -141,7 +142,7 @@ public void testUnmodifiableLinkedListMultimapRandomAccess() { @GwtIncompatible // slow (~10s) public void testUnmodifiableHashMultimap() { - checkUnmodifiableMultimap(HashMultimap.create(), false); + checkUnmodifiableMultimap(HashMultimap.<@Nullable String, @Nullable Integer>create(), false); } @J2ktIncompatible @@ -168,7 +169,9 @@ public void testSerializingUnmodifiableTreeMultimap() { @GwtIncompatible // slow (~10s) public void testUnmodifiableSynchronizedArrayListMultimap() { checkUnmodifiableMultimap( - Multimaps.synchronizedListMultimap(ArrayListMultimap.create()), true); + Multimaps.synchronizedListMultimap( + ArrayListMultimap.<@Nullable String, @Nullable Integer>create()), + true); } @J2ktIncompatible @@ -186,7 +189,9 @@ public void testSerializingUnmodifiableSynchronizedArrayListMultimap() { @GwtIncompatible // slow (~10s) public void testUnmodifiableSynchronizedHashMultimap() { checkUnmodifiableMultimap( - Multimaps.synchronizedSetMultimap(HashMultimap.create()), false); + Multimaps.synchronizedSetMultimap( + HashMultimap.<@Nullable String, @Nullable Integer>create()), + false); } @J2ktIncompatible @@ -263,7 +268,7 @@ public void testUnmodifiableMultimapEntries() { * multimap must support null keys and values. */ private static void checkUnmodifiableMultimap( - Multimap multimap, boolean permitsDuplicates) { + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates) { checkUnmodifiableMultimap(multimap, permitsDuplicates, null, null); } @@ -273,7 +278,7 @@ private static void checkUnmodifiableMultimap( * involving nulls. */ private static void checkUnmodifiableMultimap( - Multimap multimap, + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates, @Nullable String nullKey, @Nullable Integer nullValue) { @@ -302,8 +307,8 @@ private static void checkUnmodifiableMultimap( } /** Prepares the multimap for unmodifiable tests, returning an unmodifiable view of the map. */ - private static Multimap prepareUnmodifiableTests( - Multimap multimap, + private static Multimap<@Nullable String, @Nullable Integer> prepareUnmodifiableTests( + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates, @Nullable String nullKey, @Nullable Integer nullValue) { @@ -324,21 +329,26 @@ private static Multimap prepareUnmodifiableTests( assertEquals(8, multimap.size()); } - Multimap unmodifiable; + Multimap<@Nullable String, @Nullable Integer> unmodifiable; if (multimap instanceof SortedSetMultimap) { unmodifiable = - Multimaps.unmodifiableSortedSetMultimap((SortedSetMultimap) multimap); + Multimaps.unmodifiableSortedSetMultimap( + (SortedSetMultimap<@Nullable String, @Nullable Integer>) multimap); } else if (multimap instanceof SetMultimap) { - unmodifiable = Multimaps.unmodifiableSetMultimap((SetMultimap) multimap); + unmodifiable = + Multimaps.unmodifiableSetMultimap( + (SetMultimap<@Nullable String, @Nullable Integer>) multimap); } else if (multimap instanceof ListMultimap) { - unmodifiable = Multimaps.unmodifiableListMultimap((ListMultimap) multimap); + unmodifiable = + Multimaps.unmodifiableListMultimap( + (ListMultimap<@Nullable String, @Nullable Integer>) multimap); } else { unmodifiable = Multimaps.unmodifiableMultimap(multimap); } return unmodifiable; } - private static void assertUnmodifiableIterableInTandem( + private static void assertUnmodifiableIterableInTandem( Iterable unmodifiable, Iterable modifiable) { UnmodifiableCollectionTests.assertIteratorIsUnmodifiable(unmodifiable.iterator()); UnmodifiableCollectionTests.assertIteratorsInOrder( @@ -510,7 +520,7 @@ public void testForMapGetIteration() { IteratorTester tester = new IteratorTester( 4, MODIFIABLE, newHashSet(1), IteratorTester.KnownOrder.KNOWN_ORDER) { - private Multimap multimap; + private @Nullable Multimap multimap; @Override protected Iterator newTargetIterator() { @@ -809,9 +819,9 @@ public Integer apply(String input) { } public void testIndex_nullValue() { - List values = Arrays.asList(1, null); + List<@Nullable Integer> values = Arrays.asList(1, null); try { - Multimaps.index(values, Functions.identity()); + Multimaps.index((List) values, Functions.identity()); fail(); } catch (NullPointerException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/NewCustomTableTest.java b/android/guava-tests/test/com/google/common/collect/NewCustomTableTest.java index b048e4648a38..fc905f43ed90 100644 --- a/android/guava-tests/test/com/google/common/collect/NewCustomTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/NewCustomTableTest.java @@ -22,6 +22,7 @@ import com.google.common.base.Supplier; import java.util.Map; import java.util.TreeMap; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Tables#newCustomTable}. @@ -30,10 +31,10 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public class NewCustomTableTest extends AbstractTableTest { +public class NewCustomTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Supplier> factory = new Supplier>() { @Override diff --git a/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java b/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java index edbcf2a9e55e..c3ad40278fb2 100644 --- a/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java +++ b/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java @@ -22,10 +22,10 @@ import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.testing.NullPointerTester; -import java.io.Serializable; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Unit test for {@code ObjectArrays}. @@ -58,6 +58,7 @@ public void testNewArray_fromClass_Nonempty() { assertNull(array[0]); } + @J2ktIncompatible // Array::class literal not available in Kotlin KMP @GwtIncompatible // ObjectArrays.newArray(Class, int) public void testNewArray_fromClass_OfArray() { String[][] array = ObjectArrays.newArray(String[].class, 1); @@ -117,14 +118,14 @@ public void testConcatBasic() { @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) public void testConcatWithMoreGeneralType() { - Serializable[] result = ObjectArrays.concat(new String[0], new String[0], Serializable.class); - assertEquals(Serializable[].class, result.getClass()); + CharSequence[] result = ObjectArrays.concat(new String[0], new String[0], CharSequence.class); + assertEquals(CharSequence[].class, result.getClass()); } public void testToArrayImpl1() { doTestToArrayImpl1(Lists.newArrayList()); doTestToArrayImpl1(Lists.newArrayList(1)); - doTestToArrayImpl1(Lists.newArrayList(1, null, 3)); + doTestToArrayImpl1(Lists.<@Nullable Integer>newArrayList(1, null, 3)); } private void doTestToArrayImpl1(List list) { @@ -142,15 +143,17 @@ public void testToArrayImpl2() { doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true); doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] {2, 3}, true); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[0], false); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[2], false); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[3], true); } private void doTestToArrayImpl2(List list, Integer[] array1, boolean expectModify) { Integer[] starting = Arrays.copyOf(array1, array1.length); Integer[] array2 = Arrays.copyOf(array1, array1.length); - Object[] reference = list.toArray(array1); + // TODO b/283448200 - Remove temporary variable when Kotlin smartcast issue is resolved. + Integer[] array1Tmp = array1; + Object[] reference = list.toArray(array1Tmp); Object[] target = ObjectArrays.toArrayImpl(list, array2); diff --git a/android/guava-tests/test/com/google/common/collect/OrderingTest.java b/android/guava-tests/test/com/google/common/collect/OrderingTest.java index 311552735a1e..3a50b54e648f 100644 --- a/android/guava-tests/test/com/google/common/collect/OrderingTest.java +++ b/android/guava-tests/test/com/google/common/collect/OrderingTest.java @@ -97,9 +97,9 @@ public void testComplicatedOrderingExample() { // [[null, null], [null], [1, null, 2], [1, 1], [1, 2], [1], [2], [], null] assertThat(sorted) .containsExactly( - Lists.newArrayList(nullInt, nullInt), - Lists.newArrayList(nullInt), - Lists.newArrayList(1, null, 2), + Lists.<@Nullable Integer>newArrayList(nullInt, nullInt), + Lists.<@Nullable Integer>newArrayList(nullInt), + Lists.<@Nullable Integer>newArrayList(1, null, 2), Lists.newArrayList(1, 1), Lists.newArrayList(1, 2), Lists.newArrayList(1), @@ -291,7 +291,7 @@ public Character apply(String string) { } private static Ordering byCharAt(int index) { - return Ordering.natural().onResultOf(CharAtFunction.values()[index]); + return Ordering.natural().onResultOf(CharAtFunction.values()[index]); } public void testCompound_static() { @@ -385,18 +385,18 @@ public Integer apply(String string) { } } - private static final Ordering DECREASING_INTEGER = Ordering.natural().reverse(); + private static final Ordering DECREASING_INTEGER = Ordering.natural().reverse(); public void testOnResultOf_natural() { Comparator comparator = - Ordering.natural().onResultOf(StringLengthFunction.StringLength); + Ordering.natural().onResultOf(StringLengthFunction.StringLength); assertTrue(comparator.compare("to", "be") == 0); assertTrue(comparator.compare("or", "not") < 0); assertTrue(comparator.compare("that", "to") > 0); new EqualsTester() .addEqualityGroup( - comparator, Ordering.natural().onResultOf(StringLengthFunction.StringLength)) + comparator, Ordering.natural().onResultOf(StringLengthFunction.StringLength)) .addEqualityGroup(DECREASING_INTEGER) .testEquals(); reserializeAndAssert(comparator); @@ -440,8 +440,8 @@ public void testLexicographical() { } public void testNullsFirst() { - Ordering ordering = Ordering.natural().nullsFirst(); - Helpers.testComparator(ordering, null, Integer.MIN_VALUE, 0, 1); + Ordering<@Nullable Integer> ordering = Ordering.natural().nullsFirst(); + Helpers.<@Nullable Integer>testComparator(ordering, null, Integer.MIN_VALUE, 0, 1); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsFirst()) @@ -451,8 +451,8 @@ public void testNullsFirst() { } public void testNullsLast() { - Ordering ordering = Ordering.natural().nullsLast(); - Helpers.testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); + Ordering<@Nullable Integer> ordering = Ordering.natural().nullsLast(); + Helpers.<@Nullable Integer>testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsLast()) @@ -467,9 +467,10 @@ public void testBinarySearch() { } public void testSortedCopy() { - List unsortedInts = Collections.unmodifiableList(Arrays.asList(5, 0, 3, null, 0, 9)); - List sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts); - assertEquals(Arrays.asList(0, 0, 3, 5, 9, null), sortedInts); + List<@Nullable Integer> unsortedInts = + Collections.unmodifiableList(Arrays.<@Nullable Integer>asList(5, 0, 3, null, 0, 9)); + List<@Nullable Integer> sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts); + assertEquals(Arrays.<@Nullable Integer>asList(0, 0, 3, 5, 9, null), sortedInts); assertEquals( Collections.emptyList(), numberOrdering.sortedCopy(Collections.emptyList())); @@ -484,9 +485,9 @@ public void testImmutableSortedCopy() { Collections.emptyList(), numberOrdering.immutableSortedCopy(Collections.emptyList())); - List listWithNull = Arrays.asList(5, 3, null, 9); + List<@Nullable Integer> listWithNull = Arrays.asList(5, 3, null, 9); try { - Ordering.natural().nullsFirst().immutableSortedCopy(listWithNull); + Ordering.natural().nullsFirst().immutableSortedCopy((List) listWithNull); fail(); } catch (NullPointerException expected) { } @@ -599,16 +600,17 @@ public void testLeastOfIterator_simple_1() { } public void testLeastOfIterable_simple_nMinusOne_withNullElement() { - List list = Arrays.asList(3, null, 5, -1); - List result = Ordering.natural().nullsLast().leastOf(list, list.size() - 1); + List<@Nullable Integer> list = Arrays.asList(3, null, 5, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list, list.size() - 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); } public void testLeastOfIterator_simple_nMinusOne_withNullElement() { - Iterator itr = Iterators.forArray(3, null, 5, -1); - List result = Ordering.natural().nullsLast().leastOf(itr, 3); + Iterator<@Nullable Integer> itr = Iterators.forArray(3, null, 5, -1); + List<@Nullable Integer> result = Ordering.natural().nullsLast().leastOf(itr, 3); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); @@ -647,19 +649,21 @@ public void testLeastOfIterator_simple_n() { } public void testLeastOfIterable_simple_n_withNullElement() { - List list = Arrays.asList(3, 4, 5, null, -1); - List result = Ordering.natural().nullsLast().leastOf(list, list.size()); + List<@Nullable Integer> list = Arrays.asList(3, 4, 5, null, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list, list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); - assertEquals(Arrays.asList(-1, 3, 4, 5, null), result); + assertEquals(Arrays.<@Nullable Integer>asList(-1, 3, 4, 5, null), result); } public void testLeastOfIterator_simple_n_withNullElement() { - List list = Arrays.asList(3, 4, 5, null, -1); - List result = Ordering.natural().nullsLast().leastOf(list.iterator(), list.size()); + List<@Nullable Integer> list = Arrays.asList(3, 4, 5, null, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list.iterator(), list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); - assertEquals(Arrays.asList(-1, 3, 4, 5, null), result); + assertEquals(Arrays.<@Nullable Integer>asList(-1, 3, 4, 5, null), result); } public void testLeastOfIterable_simple_nPlusOne() { @@ -730,14 +734,15 @@ private static void runLeastOfComparison(int iterations, int elements, int seeds public void testLeastOfIterableLargeK() { List list = Arrays.asList(4, 2, 3, 5, 1); - assertEquals(Arrays.asList(1, 2, 3, 4, 5), Ordering.natural().leastOf(list, Integer.MAX_VALUE)); + assertEquals( + Arrays.asList(1, 2, 3, 4, 5), Ordering.natural().leastOf(list, Integer.MAX_VALUE)); } public void testLeastOfIteratorLargeK() { List list = Arrays.asList(4, 2, 3, 5, 1); assertEquals( Arrays.asList(1, 2, 3, 4, 5), - Ordering.natural().leastOf(list.iterator(), Integer.MAX_VALUE)); + Ordering.natural().leastOf(list.iterator(), Integer.MAX_VALUE)); } public void testGreatestOfIterable_simple() { @@ -1094,7 +1099,7 @@ public T apply(Integer from) { composites.add(new Composite(t, 2)); } Ordering> ordering = - Ordering.natural() + Ordering.>natural() .compound(scenario.ordering.onResultOf(Composite.getValueFunction())); return new Scenario>( ordering, composites, (Composite[]) new Composite[0]); diff --git a/android/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java b/android/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java index d070936c3bac..22686633c6ae 100644 --- a/android/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java +++ b/android/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java @@ -106,7 +106,7 @@ public void testPeekingIteratorBehavesLikeIteratorOnThreeElementIterable() { @GwtIncompatible // works but takes 5 minutes to run public void testPeekingIteratorAcceptsNullElements() { - actsLikeIteratorHelper(Lists.newArrayList(null, "A", null)); + actsLikeIteratorHelper(Lists.<@Nullable String>newArrayList(null, "A", null)); } public void testPeekOnEmptyList() { diff --git a/android/guava-tests/test/com/google/common/collect/SetsTest.java b/android/guava-tests/test/com/google/common/collect/SetsTest.java index 6809d6ef509a..4306b7343717 100644 --- a/android/guava-tests/test/com/google/common/collect/SetsTest.java +++ b/android/guava-tests/test/com/google/common/collect/SetsTest.java @@ -861,7 +861,7 @@ public void testPowerSetContents() { assertTrue(powerSet.contains(subset)); } assertFalse(powerSet.contains(ImmutableSet.of(1, 2, 4))); - assertFalse(powerSet.contains(singleton(null))); + assertFalse(powerSet.contains(Collections.<@Nullable Integer>singleton(null))); assertFalse(powerSet.contains(null)); assertFalse(powerSet.contains((Object) "notASet")); } diff --git a/android/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java b/android/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java index 216f32b2e6cf..aaa7d1662fc6 100644 --- a/android/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java @@ -22,7 +22,7 @@ import java.util.Set; import org.checkerframework.checker.nullness.qual.Nullable; -public class SynchronizedTableTest extends AbstractTableTest { +public class SynchronizedTableTest extends AbstractTableTest { private static final class TestTable implements Table, Serializable { final Table delegate = HashBasedTable.create(); public final Object mutex = new Integer(1); // something Serializable @@ -164,7 +164,7 @@ public Map> rowMap() { } @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { TestTable table = new TestTable<>(); Table synced = Synchronized.table(table, table.mutex); populate(synced, data); diff --git a/android/guava-tests/test/com/google/common/collect/TablesTest.java b/android/guava-tests/test/com/google/common/collect/TablesTest.java index 239f2ef50980..fd1096fc4475 100644 --- a/android/guava-tests/test/com/google/common/collect/TablesTest.java +++ b/android/guava-tests/test/com/google/common/collect/TablesTest.java @@ -22,6 +22,7 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.SerializableTester; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Tests for {@link Tables}. @@ -42,7 +43,8 @@ public void testImmutableEntryToString() { Cell entry = Tables.immutableCell("foo", 1, 'a'); assertEquals("(foo,1)=a", entry.toString()); - Cell nullEntry = Tables.immutableCell(null, null, null); + Cell<@Nullable String, @Nullable Integer, @Nullable Character> nullEntry = + Tables.immutableCell(null, null, null); assertEquals("(null,null)=null", nullEntry.toString()); } @@ -54,18 +56,27 @@ public void testEntryEquals() { .addEqualityGroup(Tables.immutableCell("bar", 1, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 2, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'b')) - .addEqualityGroup(Tables.immutableCell(null, null, null)) + .addEqualityGroup( + Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell( + null, null, null)) .testEquals(); } public void testEntryEqualsNull() { - Cell entry = Tables.immutableCell(null, null, null); + Cell<@Nullable String, @Nullable Integer, @Nullable Character> entry = + Tables.immutableCell(null, null, null); new EqualsTester() - .addEqualityGroup(entry, Tables.immutableCell(null, null, null)) - .addEqualityGroup(Tables.immutableCell("bar", null, null)) - .addEqualityGroup(Tables.immutableCell(null, 2, null)) - .addEqualityGroup(Tables.immutableCell(null, null, 'b')) + .addEqualityGroup( + entry, + Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell( + null, null, null)) + .addEqualityGroup( + Tables.immutableCell("bar", null, null)) + .addEqualityGroup( + Tables.<@Nullable Object, Integer, @Nullable Object>immutableCell(null, 2, null)) + .addEqualityGroup( + Tables.<@Nullable Object, @Nullable Object, Character>immutableCell(null, null, 'b')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'a')) .testEquals(); } diff --git a/android/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java b/android/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java index 20251795ce9a..8039b1dec5c1 100644 --- a/android/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java +++ b/android/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java @@ -31,7 +31,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class TablesTransformValuesTest extends AbstractTableTest { +public class TablesTransformValuesTest extends AbstractTableTest { private static final Function<@Nullable String, @Nullable Character> FIRST_CHARACTER = new Function<@Nullable String, @Nullable Character>() { @@ -42,7 +42,7 @@ public class TablesTransformValuesTest extends AbstractTableTest { }; @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table table = HashBasedTable.create(); checkArgument(data.length % 3 == 0); for (int i = 0; i < data.length; i += 3) { diff --git a/android/guava-tests/test/com/google/common/collect/TopKSelectorTest.java b/android/guava-tests/test/com/google/common/collect/TopKSelectorTest.java index 2cebdc338b08..05ecb2040fe4 100644 --- a/android/guava-tests/test/com/google/common/collect/TopKSelectorTest.java +++ b/android/guava-tests/test/com/google/common/collect/TopKSelectorTest.java @@ -37,12 +37,12 @@ public class TopKSelectorTest extends TestCase { public void testNegativeK() { try { - TopKSelector.least(-1); + TopKSelector.least(-1); fail(); } catch (IllegalArgumentException expected) { } try { - TopKSelector.greatest(-1); + TopKSelector.greatest(-1); fail(); } catch (IllegalArgumentException expected) { } diff --git a/android/guava-tests/test/com/google/common/collect/TransposedTableTest.java b/android/guava-tests/test/com/google/common/collect/TransposedTableTest.java index e556ea88fa9a..c560aa2dda19 100644 --- a/android/guava-tests/test/com/google/common/collect/TransposedTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/TransposedTableTest.java @@ -17,6 +17,7 @@ package com.google.common.collect; import com.google.common.annotations.GwtCompatible; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Tables#transpose}. @@ -25,10 +26,10 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public class TransposedTableTest extends AbstractTableTest { +public class TransposedTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table original = HashBasedTable.create(); Table table = Tables.transpose(original); table.clear(); diff --git a/android/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java b/android/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java index f42673766a21..c1a9f0066b9f 100644 --- a/android/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java +++ b/android/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java @@ -35,6 +35,7 @@ import java.util.SortedMap; import junit.framework.Test; import junit.framework.TestSuite; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link TreeBasedTable}. @@ -44,7 +45,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class TreeBasedTableTest extends AbstractTableTest { +public class TreeBasedTableTest extends AbstractTableTest { @J2ktIncompatible @GwtIncompatible // suite public static Test suite() { @@ -90,7 +91,7 @@ protected TreeBasedTable create( } @Override - protected TreeBasedTable create(Object... data) { + protected TreeBasedTable create(@Nullable Object... data) { TreeBasedTable table = TreeBasedTable.create(); table.put("foo", 4, 'a'); table.put("cat", 1, 'b'); @@ -285,7 +286,7 @@ public void testColumnKeySet_isSortedWithRealComparator() { table = create( String.CASE_INSENSITIVE_ORDER, - Ordering.natural().reverse(), + Ordering.natural().reverse(), "a", 2, 'X', diff --git a/android/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java b/android/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java index 601709b105e6..e887c0fadfa4 100644 --- a/android/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java +++ b/android/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java @@ -150,10 +150,10 @@ public void testOrderedEntries() { TreeMultimap<@Nullable String, @Nullable Integer> multimap = createPopulate(); assertThat(multimap.entries()) .containsExactly( - Maps.immutableEntry((String) null, 7), - Maps.immutableEntry((String) null, 3), - Maps.immutableEntry((String) null, 1), - Maps.immutableEntry("tree", (Integer) null), + Maps.<@Nullable String, Integer>immutableEntry(null, 7), + Maps.<@Nullable String, Integer>immutableEntry(null, 3), + Maps.<@Nullable String, Integer>immutableEntry(null, 1), + Maps.immutableEntry("tree", null), Maps.immutableEntry("tree", 0), Maps.immutableEntry("google", 6), Maps.immutableEntry("google", 2)) @@ -193,7 +193,7 @@ public void testSortedKeySet() { assertEquals(null, keySet.first()); assertEquals("google", keySet.last()); assertEquals(StringLength.COMPARATOR, keySet.comparator()); - assertEquals(Sets.newHashSet(null, "tree"), keySet.headSet("yahoo")); + assertEquals(Sets.<@Nullable String>newHashSet(null, "tree"), keySet.headSet("yahoo")); assertEquals(Sets.newHashSet("google"), keySet.tailSet("yahoo")); assertEquals(Sets.newHashSet("tree"), keySet.subSet("ask", "yahoo")); } diff --git a/android/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java b/android/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java index 012a14c5aab6..f475e87f996c 100644 --- a/android/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java +++ b/android/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java @@ -354,7 +354,7 @@ public void testMultimapConstructor() { private static final Comparator KEY_COMPARATOR = Ordering.natural(); private static final Comparator VALUE_COMPARATOR = - Ordering.natural().reverse().nullsFirst(); + Ordering.natural().reverse().nullsFirst(); /** * Test that creating one TreeMultimap from another does not copy the comparators from the source diff --git a/android/guava-tests/test/com/google/common/collect/TreeMultisetTest.java b/android/guava-tests/test/com/google/common/collect/TreeMultisetTest.java index 0077b75d168a..6f9651a9df51 100644 --- a/android/guava-tests/test/com/google/common/collect/TreeMultisetTest.java +++ b/android/guava-tests/test/com/google/common/collect/TreeMultisetTest.java @@ -287,8 +287,8 @@ public int compare(String o1, String o2) { } public void testNullAcceptingComparator() throws Exception { - Comparator comparator = Ordering.natural().nullsFirst(); - TreeMultiset ms = TreeMultiset.create(comparator); + Comparator<@Nullable String> comparator = Ordering.natural().nullsFirst(); + TreeMultiset<@Nullable String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add(null); diff --git a/android/guava-tests/test/com/google/common/io/CloserTest.java b/android/guava-tests/test/com/google/common/io/CloserTest.java index f30d771a8743..90c4eefe88af 100644 --- a/android/guava-tests/test/com/google/common/io/CloserTest.java +++ b/android/guava-tests/test/com/google/common/io/CloserTest.java @@ -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; @@ -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); } } diff --git a/android/guava-tests/test/com/google/common/math/LongMathTest.java b/android/guava-tests/test/com/google/common/math/LongMathTest.java index 25ad1dc325f0..21de0f55a837 100644 --- a/android/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/android/guava-tests/test/com/google/common/math/LongMathTest.java @@ -786,6 +786,7 @@ public void testBinomialNegative() { } + @J2ktIncompatible // slow enough to cause flakiness @GwtIncompatible // far too slow public void testSqrtOfPerfectSquareAsDoubleIsPerfect() { // This takes just over a minute on my machine. diff --git a/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java b/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java index c440ee53f509..b4cdb9b3b856 100644 --- a/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java +++ b/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java @@ -19,6 +19,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Ascii; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; @@ -501,6 +502,7 @@ private static InternetDomainName idn(String domain) { return InternetDomainName.from(domain); } + @J2ktIncompatible @GwtIncompatible // NullPointerTester public void testNulls() { final NullPointerTester tester = new NullPointerTester(); diff --git a/android/guava-tests/test/com/google/common/net/MediaTypeTest.java b/android/guava-tests/test/com/google/common/net/MediaTypeTest.java index a3428b32f53d..84ad5bc19fa7 100644 --- a/android/guava-tests/test/com/google/common/net/MediaTypeTest.java +++ b/android/guava-tests/test/com/google/common/net/MediaTypeTest.java @@ -35,6 +35,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; @@ -57,6 +58,7 @@ */ @GwtCompatible(emulated = true) public class MediaTypeTest extends TestCase { + @J2ktIncompatible @GwtIncompatible // reflection public void testParse_useConstants() throws Exception { for (MediaType constant : getConstants()) { @@ -64,6 +66,7 @@ public void testParse_useConstants() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testCreate_useConstants() throws Exception { for (MediaType constant : getConstants()) { @@ -74,6 +77,7 @@ public void testCreate_useConstants() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testConstants_charset() throws Exception { for (Field field : getConstantFields()) { @@ -86,11 +90,13 @@ public void testConstants_charset() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testConstants_areUnique() { assertThat(getConstants()).containsNoDuplicates(); } + @J2ktIncompatible @GwtIncompatible // reflection private static FluentIterable getConstantFields() { return FluentIterable.from(asList(MediaType.class.getDeclaredFields())) @@ -107,6 +113,7 @@ && isFinal(modifiers) }); } + @J2ktIncompatible @GwtIncompatible // reflection private static FluentIterable getConstants() { return getConstantFields() @@ -525,6 +532,7 @@ public void testGetCharset() { assertThat(MediaType.parse("text/plain; charset=utf-8").charset()).hasValue(UTF_8); } + @J2ktIncompatible @GwtIncompatible // Non-UTF-8 Charset public void testGetCharset_utf16() { assertThat(MediaType.parse("text/plain; charset=utf-16").charset()).hasValue(UTF_16); @@ -604,6 +612,7 @@ public void testEquals() { .testEquals(); } + @J2ktIncompatible @GwtIncompatible // Non-UTF-8 Charset public void testEquals_nonUtf8Charsets() { new EqualsTester() @@ -613,6 +622,7 @@ public void testEquals_nonUtf8Charsets() { .testEquals(); } + @J2ktIncompatible @GwtIncompatible // com.google.common.testing.NullPointerTester public void testNullPointer() { NullPointerTester tester = new NullPointerTester(); diff --git a/android/guava-tests/test/com/google/common/primitives/BooleansTest.java b/android/guava-tests/test/com/google/common/primitives/BooleansTest.java index a968353a9f57..e6fbd1aed90d 100644 --- a/android/guava-tests/test/com/google/common/primitives/BooleansTest.java +++ b/android/guava-tests/test/com/google/common/primitives/BooleansTest.java @@ -579,9 +579,9 @@ public void testAsListCanonicalValues() { assertThat(list.get(0)).isSameInstanceAs(true); assertThat(list.get(1)).isSameInstanceAs(false); @SuppressWarnings("deprecation") - Boolean anotherTrue = true; + Boolean anotherTrue = new Boolean(true); @SuppressWarnings("deprecation") - Boolean anotherFalse = false; + Boolean anotherFalse = new Boolean(false); list.set(0, anotherTrue); assertThat(list.get(0)).isSameInstanceAs(true); list.set(1, anotherFalse); diff --git a/android/guava/src/com/google/common/base/Throwables.java b/android/guava/src/com/google/common/base/Throwables.java index 1f1c346e0256..73dad9790a85 100644 --- a/android/guava/src/com/google/common/base/Throwables.java +++ b/android/guava/src/com/google/common/base/Throwables.java @@ -162,7 +162,10 @@ public static void propagateIfPossible(@CheckForNull Throwable throwable) { * * @param throwable the Throwable to possibly propagate * @param declaredType the single checked exception type declared by the calling method + * @deprecated Use a combination of {@link #throwIfInstanceOf} and {@link #throwIfUnchecked}, + * which togther provide the same behavior except that they reject {@code null}. */ + @Deprecated @J2ktIncompatible @GwtIncompatible // propagateIfInstanceOf public static void propagateIfPossible( @@ -175,13 +178,14 @@ public static void propagateIfPossible( * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. * - *

Discouraged in favor of calling {@link #throwIfInstanceOf} and {@link - * #throwIfUnchecked}. - * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method + * @deprecated Use a combination of two calls to {@link #throwIfInstanceOf} and one call to {@link + * #throwIfUnchecked}, which togther provide the same behavior except that they reject {@code + * null}. */ + @Deprecated @J2ktIncompatible @GwtIncompatible // propagateIfInstanceOf public static void propagateIfPossible( diff --git a/android/guava/src/com/google/common/collect/TableCollectors.java b/android/guava/src/com/google/common/collect/TableCollectors.java index 64ee1d587e3c..0508c4802fc9 100644 --- a/android/guava/src/com/google/common/collect/TableCollectors.java +++ b/android/guava/src/com/google/common/collect/TableCollectors.java @@ -86,7 +86,7 @@ final class TableCollectors { T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, @@ -107,7 +107,7 @@ final class TableCollectors { T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, diff --git a/android/guava/src/com/google/common/collect/Tables.java b/android/guava/src/com/google/common/collect/Tables.java index 46c26311f9f2..ba276d510abb 100644 --- a/android/guava/src/com/google/common/collect/Tables.java +++ b/android/guava/src/com/google/common/collect/Tables.java @@ -69,7 +69,7 @@ private Tables() {} T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, @@ -98,7 +98,7 @@ private Tables() {} T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, diff --git a/android/guava/src/com/google/common/io/Closer.java b/android/guava/src/com/google/common/io/Closer.java index 8e1db7a83096..b1512ae14ca7 100644 --- a/android/guava/src/com/google/common/io/Closer.java +++ b/android/guava/src/com/google/common/io/Closer.java @@ -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; @@ -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); } @@ -171,8 +173,9 @@ public RuntimeException rethrow(Throwable e, Class 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); } @@ -195,8 +198,10 @@ public RuntimeException rethrow( Throwable e, Class declaredType1, Class 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); } @@ -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 } } diff --git a/android/guava/src/com/google/common/util/concurrent/ExecutionError.java b/android/guava/src/com/google/common/util/concurrent/ExecutionError.java index 303383039fc0..a5a2a1a3aae9 100644 --- a/android/guava/src/com/google/common/util/concurrent/ExecutionError.java +++ b/android/guava/src/com/google/common/util/concurrent/ExecutionError.java @@ -31,25 +31,55 @@ @ElementTypesAreNonnullByDefault public class ExecutionError extends Error { /* - * Ideally, this class would have exposed only constructors that require a non-null cause. We - * might try to move in that direction, but there are complications. See + * Ideally, this class would have exposed only constructors that require a non-null cause. See * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789 + * and https://github.com/jspecify/jspecify/issues/490. + * + * (That would also have ensured that its cause was always an Error, rather than possibly another + * kind of Throwable that was later passed to initCause. Then we could have declared the override + * `public final Error getCause()`.) */ - /** Creates a new instance with {@code null} as its detail message. */ + /** + * Creates a new instance with {@code null} as its detail message and no cause. + * + * @deprecated Prefer {@linkplain ExecutionError(Error)} a constructor that accepts a cause: Users + * of this class typically expect for instances to have a non-null cause. At the moment, you + * can usually still preserve behavior by passing an explicit {@code null} cause. Note, + * however, that passing an explicit {@code null} cause prevents anyone from calling {@link + * #initCause} later, so it is not quite equivalent to using a constructor that omits the + * cause. + */ + @Deprecated protected ExecutionError() {} - /** Creates a new instance with the given detail message. */ + /** + * Creates a new instance with the given detail message and no cause. + * + * @deprecated Prefer {@linkplain ExecutionError(String, Error)} a constructor that accepts a + * cause: Users of this class typically expect for instances to have a non-null cause. At the + * moment, you can usually still preserve behavior by passing an explicit {@code null} + * cause. Note, however, that passing an explicit {@code null} cause prevents anyone from + * calling {@link #initCause} later, so it is not quite equivalent to using a constructor that + * omits the cause. + */ + @Deprecated protected ExecutionError(@CheckForNull String message) { super(message); } - /** Creates a new instance with the given detail message and cause. */ + /** + * Creates a new instance with the given detail message and cause. Prefer to provide a + * non-nullable {@code cause}, as many users expect to find one. + */ public ExecutionError(@CheckForNull String message, @CheckForNull Error cause) { super(message, cause); } - /** Creates a new instance with the given cause. */ + /** + * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to + * provide a non-nullable {@code cause}, as many users expect to find one. + */ public ExecutionError(@CheckForNull Error cause) { super(cause); } diff --git a/android/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java b/android/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java index a0ec427eeca4..bb6f5e04d054 100644 --- a/android/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java +++ b/android/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java @@ -36,25 +36,55 @@ @ElementTypesAreNonnullByDefault public class UncheckedExecutionException extends RuntimeException { /* - * Ideally, this class would have exposed only constructors that require a non-null cause. We - * might try to move in that direction, but there are complications. See + * Ideally, this class would have exposed only constructors that require a non-null cause. See * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789 + * and https://github.com/jspecify/jspecify/issues/490. + * + * (Perhaps it should also have required that its cause was a RuntimeException. However, that + * would have required that we throw a different kind of exception for wrapping *checked* + * exceptions in methods like Futures.getUnchecked and LoadingCache.get.) */ - /** Creates a new instance with {@code null} as its detail message. */ + /** + * Creates a new instance with {@code null} as its detail message and no cause. + * + * @deprecated Prefer {@linkplain UncheckedExecutionException(Throwable)} a constructor that + * accepts a cause: Users of this class typically expect for instances to have a non-null + * cause. At the moment, you can usually still preserve behavior by passing an explicit + * {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents + * anyone from calling {@link #initCause} later, so it is not quite equivalent to using a + * constructor that omits the cause. + */ + @Deprecated protected UncheckedExecutionException() {} - /** Creates a new instance with the given detail message. */ + /** + * Creates a new instance with the given detail message and no cause. + * + * @deprecated Prefer {@linkplain UncheckedExecutionException(String, Throwable)} a constructor + * that accepts a cause: Users of this class typically expect for instances to have a non-null + * cause. At the moment, you can usually still preserve behavior by passing an explicit + * {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents + * anyone from calling {@link #initCause} later, so it is not quite equivalent to using a + * constructor that omits the cause. + */ + @Deprecated protected UncheckedExecutionException(@CheckForNull String message) { super(message); } - /** Creates a new instance with the given detail message and cause. */ + /** + * Creates a new instance with the given detail message and cause. Prefer to provide a + * non-nullable {@code cause}, as many users expect to find one. + */ public UncheckedExecutionException(@CheckForNull String message, @CheckForNull Throwable cause) { super(message, cause); } - /** Creates a new instance with the given cause. */ + /** + * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to + * provide a non-nullable {@code cause}, as many users expect to find one. + */ public UncheckedExecutionException(@CheckForNull Throwable cause) { super(cause); } diff --git a/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java b/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java index c8989d34d884..e87c42be38bc 100644 --- a/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java +++ b/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java @@ -75,7 +75,7 @@ protected abstract > Set copyOf( public void testCreation_noArgs() { Set set = of(); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCreation_oneElement() { @@ -122,7 +122,7 @@ public void testCopyOf_emptyArray() { String[] array = new String[0]; Set set = copyOf(array); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_arrayOfOneElement() { @@ -153,7 +153,7 @@ public void testCopyOf_collection_empty() { Collection c = MinimalCollection.of(); Set set = copyOf(c); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_collection_oneElement() { @@ -203,7 +203,7 @@ public void testCopyOf_iterator_empty() { Iterator iterator = Iterators.emptyIterator(); Set set = copyOf(iterator); assertEquals(Collections.emptySet(), set); - assertSame(of(), set); + assertSame(this.of(), set); } public void testCopyOf_iterator_oneElement() { diff --git a/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java b/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java index 3a46bb4c3034..67a47e67d355 100644 --- a/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java +++ b/guava-tests/test/com/google/common/collect/AbstractMapsTransformValuesTest.java @@ -249,11 +249,12 @@ public void testTransformEntrySetContains() { Set> entries = map.entrySet(); assertTrue(entries.contains(Maps.immutableEntry("a", true))); - assertTrue(entries.contains(Maps.immutableEntry("b", (Boolean) null))); - assertTrue(entries.contains(Maps.immutableEntry((String) null, (Boolean) null))); + assertTrue(entries.contains(Maps.immutableEntry("b", null))); + assertTrue( + entries.contains(Maps.<@Nullable String, @Nullable Boolean>immutableEntry(null, null))); - assertFalse(entries.contains(Maps.immutableEntry("c", (Boolean) null))); - assertFalse(entries.contains(Maps.immutableEntry((String) null, true))); + assertFalse(entries.contains(Maps.immutableEntry("c", null))); + assertFalse(entries.contains(Maps.<@Nullable String, Boolean>immutableEntry(null, true))); } @Override diff --git a/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java b/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java index d89be34623ae..caa40a63a122 100644 --- a/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java +++ b/guava-tests/test/com/google/common/collect/AbstractTableReadTest.java @@ -25,6 +25,7 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Table} read operations. @@ -33,8 +34,8 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public abstract class AbstractTableReadTest extends TestCase { - protected Table table; +public abstract class AbstractTableReadTest extends TestCase { + protected Table table; /** * Creates a table with the specified data. @@ -43,7 +44,7 @@ public abstract class AbstractTableReadTest extends TestCase { * @throws IllegalArgumentException if the size of {@code data} isn't a multiple of 3 * @throws ClassCastException if a data element has the wrong type */ - protected abstract Table create(Object... data); + protected abstract Table create(@Nullable Object... data); protected void assertSize(int expectedSize) { assertEquals(expectedSize, table.size()); @@ -120,14 +121,13 @@ public void testSize() { public void testEquals() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table hashCopy = HashBasedTable.create(table); - Table reordered = - create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); - Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); - Table swapOuter = - create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); - Table swapValues = - create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); + // We know that we have only added non-null Characters. + Table hashCopy = + HashBasedTable.create((Table) table); + Table reordered = create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); + Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); + Table swapOuter = create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); + Table swapValues = create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); new EqualsTester() .addEqualityGroup(table, hashCopy, reordered) diff --git a/guava-tests/test/com/google/common/collect/AbstractTableTest.java b/guava-tests/test/com/google/common/collect/AbstractTableTest.java index c8d9052d9c60..0639f2d325d0 100644 --- a/guava-tests/test/com/google/common/collect/AbstractTableTest.java +++ b/guava-tests/test/com/google/common/collect/AbstractTableTest.java @@ -20,6 +20,8 @@ import com.google.common.annotations.GwtCompatible; import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for a {@link Table} implementation supporting reads and writes. @@ -29,12 +31,14 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public abstract class AbstractTableTest extends AbstractTableReadTest { +public abstract class AbstractTableTest + extends AbstractTableReadTest { - protected void populate(Table table, Object... data) { + protected void populate(Table table, @Nullable Object... data) { checkArgument(data.length % 3 == 0); for (int i = 0; i < data.length; i += 3) { - table.put((String) data[i], (Integer) data[i + 1], (Character) data[i + 2]); + table.put( + (String) data[i], (Integer) data[i + 1], nullableCellValue((Character) data[i + 2])); } } @@ -62,29 +66,28 @@ public void testClear() { } public void testPut() { - assertNull(table.put("foo", 1, 'a')); - assertNull(table.put("bar", 1, 'b')); - assertNull(table.put("foo", 3, 'c')); - assertEquals((Character) 'a', table.put("foo", 1, 'd')); + assertNull(table.put("foo", 1, cellValue('a'))); + assertNull(table.put("bar", 1, cellValue('b'))); + assertNull(table.put("foo", 3, cellValue('c'))); + assertEquals((Character) 'a', table.put("foo", 1, cellValue('d'))); assertEquals((Character) 'd', table.get("foo", 1)); assertEquals((Character) 'b', table.get("bar", 1)); assertSize(3); - assertEquals((Character) 'd', table.put("foo", 1, 'd')); + assertEquals((Character) 'd', table.put("foo", 1, cellValue('d'))); assertEquals((Character) 'd', table.get("foo", 1)); assertSize(3); } - // This test assumes that the implementation does not support nulls. public void testPutNull() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); assertSize(3); try { - table.put(null, 2, 'd'); + table.put(null, 2, cellValue('d')); fail(); } catch (NullPointerException expected) { } try { - table.put("cat", null, 'd'); + table.put("cat", null, cellValue('d')); fail(); } catch (NullPointerException expected) { } @@ -105,11 +108,11 @@ public void testPutNullReplace() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); if (supportsNullValues()) { - assertEquals((Character) 'b', table.put("bar", 1, null)); + assertEquals((Character) 'b', table.put("bar", 1, nullableCellValue(null))); assertNull(table.get("bar", 1)); } else { try { - table.put("bar", 1, null); + table.put("bar", 1, nullableCellValue(null)); fail(); } catch (NullPointerException expected) { } @@ -118,10 +121,10 @@ public void testPutNullReplace() { public void testPutAllTable() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table other = HashBasedTable.create(); - other.put("foo", 1, 'd'); - other.put("bar", 2, 'e'); - other.put("cat", 2, 'f'); + Table other = HashBasedTable.create(); + other.put("foo", 1, cellValue('d')); + other.put("bar", 2, cellValue('e')); + other.put("cat", 2, cellValue('f')); table.putAll(other); assertEquals((Character) 'd', table.get("foo", 1)); assertEquals((Character) 'b', table.get("bar", 1)); @@ -159,18 +162,29 @@ public void testRemove() { public void testRowClearAndPut() { if (supportsRemove()) { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Map row = table.row("foo"); + Map row = table.row("foo"); assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), row); table.remove("foo", 3); assertEquals(ImmutableMap.of(1, 'a'), row); table.remove("foo", 1); assertEquals(ImmutableMap.of(), row); - table.put("foo", 2, 'b'); + table.put("foo", 2, cellValue('b')); assertEquals(ImmutableMap.of(2, 'b'), row); row.clear(); assertEquals(ImmutableMap.of(), row); - table.put("foo", 5, 'x'); + table.put("foo", 5, cellValue('x')); assertEquals(ImmutableMap.of(5, 'x'), row); } } + + @SuppressWarnings("unchecked") // C can only be @Nullable Character or Character + protected @NonNull C cellValue(Character character) { + return (C) character; + } + + // Only safe wrt. ClassCastException. Not null-safe (can be used to test expected Table NPEs) + @SuppressWarnings("unchecked") + protected C nullableCellValue(@Nullable Character character) { + return (C) character; + } } diff --git a/guava-tests/test/com/google/common/collect/ArrayTableTest.java b/guava-tests/test/com/google/common/collect/ArrayTableTest.java index 77abcc3e0734..f97a4393da7f 100644 --- a/guava-tests/test/com/google/common/collect/ArrayTableTest.java +++ b/guava-tests/test/com/google/common/collect/ArrayTableTest.java @@ -38,10 +38,10 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class ArrayTableTest extends AbstractTableTest { +public class ArrayTableTest extends AbstractTableTest<@Nullable Character> { @Override - protected ArrayTable create(Object... data) { + protected ArrayTable create(@Nullable Object... data) { // TODO: Specify different numbers of rows and columns, to detect problems // that arise when the wrong size is used. ArrayTable table = @@ -128,12 +128,12 @@ public void testEquals() { hashCopy.put("foo", 1, 'a'); hashCopy.put("bar", 1, 'b'); hashCopy.put("foo", 3, 'c'); - Table reordered = + Table reordered = create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b'); - Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); - Table swapOuter = + Table smaller = create("foo", 1, 'a', "bar", 1, 'b'); + Table swapOuter = create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c'); - Table swapValues = + Table swapValues = create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a'); new EqualsTester() @@ -242,9 +242,9 @@ public void testEmptyToArry() { } public void testCreateCopyArrayTable() { - Table original = + Table original = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); - Table copy = ArrayTable.create(original); + Table copy = ArrayTable.create(original); assertEquals(original, copy); original.put("foo", 1, 'd'); assertEquals((Character) 'd', original.get("foo", 1)); @@ -258,7 +258,7 @@ public void testCreateCopyHashBasedTable() { original.put("foo", 1, 'a'); original.put("bar", 1, 'b'); original.put("foo", 3, 'c'); - Table copy = ArrayTable.create(original); + Table copy = ArrayTable.create(original); assertEquals(4, copy.size()); assertEquals((Character) 'a', copy.get("foo", 1)); assertEquals((Character) 'b', copy.get("bar", 1)); @@ -281,7 +281,7 @@ public void testCreateCopyEmptyTable() { } public void testCreateCopyEmptyArrayTable() { - Table original = + Table original = ArrayTable.create(Arrays.asList(), Arrays.asList()); ArrayTable copy = ArrayTable.create(original); assertThat(copy).isEqualTo(original); diff --git a/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java b/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java index daa929df6c2c..b021a0de47f0 100644 --- a/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java +++ b/guava-tests/test/com/google/common/collect/ForwardingSortedMapImplementsMapTest.java @@ -52,7 +52,7 @@ public ForwardingSortedMapImplementsMapTest() { @Override protected SortedMap makeEmptyMap() { return new SimpleForwardingSortedMap<>( - new TreeMap(Ordering.natural().nullsFirst())); + new TreeMap(Ordering.natural().nullsFirst())); } @Override diff --git a/guava-tests/test/com/google/common/collect/GeneralRangeTest.java b/guava-tests/test/com/google/common/collect/GeneralRangeTest.java index d2b1d488878d..c50f1f5c46d1 100644 --- a/guava-tests/test/com/google/common/collect/GeneralRangeTest.java +++ b/guava-tests/test/com/google/common/collect/GeneralRangeTest.java @@ -35,7 +35,8 @@ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault public class GeneralRangeTest extends TestCase { - private static final Ordering<@Nullable Integer> ORDERING = Ordering.natural().nullsFirst(); + private static final Ordering<@Nullable Integer> ORDERING = + Ordering.natural().nullsFirst(); private static final List<@Nullable Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5); @@ -150,7 +151,7 @@ public void testIntersectAgainstBiggerRange() { assertEquals( GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), - range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED))); + range.intersect(GeneralRange.<@Nullable Integer>range(ORDERING, null, OPEN, 5, CLOSED))); assertEquals( GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), diff --git a/guava-tests/test/com/google/common/collect/HashBasedTableTest.java b/guava-tests/test/com/google/common/collect/HashBasedTableTest.java index fac4794dfff2..15ca3482d2e6 100644 --- a/guava-tests/test/com/google/common/collect/HashBasedTableTest.java +++ b/guava-tests/test/com/google/common/collect/HashBasedTableTest.java @@ -23,6 +23,7 @@ import com.google.common.annotations.J2ktIncompatible; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link HashBasedTable}. @@ -31,10 +32,10 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class HashBasedTableTest extends AbstractTableTest { +public class HashBasedTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table table = HashBasedTable.create(); table.put("foo", 4, 'a'); table.put("cat", 1, 'b'); diff --git a/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java b/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java index acd02621bb5d..1c4520ea2aa5 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java @@ -389,10 +389,10 @@ public void testCopyOfNullKey() { } public void testCopyOfNullValue() { - ArrayListMultimap input = ArrayListMultimap.create(); - input.putAll("foo", Arrays.asList(1, null, 3)); + ArrayListMultimap input = ArrayListMultimap.create(); + input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3)); try { - ImmutableListMultimap.copyOf(input); + ImmutableListMultimap.copyOf((ArrayListMultimap) input); fail(); } catch (NullPointerException expected) { } diff --git a/guava-tests/test/com/google/common/collect/ImmutableListTest.java b/guava-tests/test/com/google/common/collect/ImmutableListTest.java index d804778b5fb6..c0bb5fab9ede 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableListTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableListTest.java @@ -633,9 +633,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() { } builder = ImmutableList.builder(); - Iterator iteratorWithNulls = asList("a", null, "b").iterator(); + Iterator<@Nullable String> iteratorWithNulls = + Arrays.<@Nullable String>asList("a", null, "b").iterator(); try { - builder.addAll(iteratorWithNulls); + builder.addAll((Iterator) iteratorWithNulls); fail("expected NullPointerException"); } catch (NullPointerException expected) { } diff --git a/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java b/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java index 3e556c35c9fc..2ef1c8a46b7c 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableMultisetTest.java @@ -38,6 +38,7 @@ import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; @@ -282,9 +283,10 @@ public void testCopyOf_multiset_general() { } public void testCopyOf_multisetContainingNull() { - Multiset c = HashMultiset.create(asList("a", null, "b")); + Multiset<@Nullable String> c = + HashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b")); try { - ImmutableMultiset.copyOf(c); + ImmutableMultiset.copyOf((Multiset) c); fail(); } catch (NullPointerException expected) { } @@ -309,9 +311,10 @@ public void testCopyOf_iterator_general() { } public void testCopyOf_iteratorContainingNull() { - Iterator iterator = asList("a", null, "b").iterator(); + Iterator<@Nullable String> iterator = + Arrays.<@Nullable String>asList("a", null, "b").iterator(); try { - ImmutableMultiset.copyOf(iterator); + ImmutableMultiset.copyOf((Iterator) iterator); fail(); } catch (NullPointerException expected) { } @@ -536,9 +539,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() { } builder = ImmutableMultiset.builder(); - Multiset multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b")); + Multiset<@Nullable String> multisetWithNull = + LinkedHashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b")); try { - builder.addAll(multisetWithNull); + builder.addAll((Multiset) multisetWithNull); fail("expected NullPointerException"); } catch (NullPointerException expected) { } diff --git a/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java b/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java index 6115cdddd584..776215293ba0 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java @@ -402,10 +402,10 @@ public void testCopyOfNullKey() { } public void testCopyOfNullValue() { - HashMultimap input = HashMultimap.create(); - input.putAll("foo", Arrays.asList(1, null, 3)); + HashMultimap input = HashMultimap.create(); + input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3)); try { - ImmutableSetMultimap.copyOf(input); + ImmutableSetMultimap.copyOf((Multimap) input); fail(); } catch (NullPointerException expected) { } diff --git a/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java b/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java index badaf835ccdf..5c923fbf6545 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableSortedMapTest.java @@ -517,7 +517,7 @@ public void testCopyOf() { } public void testCopyOfExplicitComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); Map original = new LinkedHashMap<>(); original.put("one", 1); original.put("two", 2); @@ -530,7 +530,7 @@ public void testCopyOfExplicitComparator() { } public void testCopyOfImmutableSortedSetDifferentComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); Map original = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3); ImmutableSortedMap copy = ImmutableSortedMap.copyOf(original, comparator); assertMapEquals(copy, "two", 2, "three", 3, "one", 1); @@ -551,7 +551,7 @@ public void testCopyOfSortedNatural() { } public void testCopyOfSortedExplicit() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); SortedMap original = Maps.newTreeMap(comparator); original.put("one", 1); original.put("two", 2); @@ -619,11 +619,11 @@ public void testBuilderReverseOrder() { .put("five", 5) .build(); assertMapEquals(map, "two", 2, "three", 3, "one", 1, "four", 4, "five", 5); - assertEquals(Ordering.natural().reverse(), map.comparator()); + assertEquals(Ordering.natural().reverse(), map.comparator()); } public void testBuilderComparator() { - Comparator comparator = Ordering.natural().reverse(); + Comparator comparator = Ordering.natural().reverse(); ImmutableSortedMap map = new ImmutableSortedMap.Builder(comparator) .put("one", 1) diff --git a/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java b/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java index 32c1b81dcb00..4bb9736e3a7e 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java @@ -298,8 +298,8 @@ public void testSingle_headSet() { SortedSet set = of("e"); assertTrue(set.headSet("g") instanceof ImmutableSortedSet); assertThat(set.headSet("g")).contains("e"); - assertSame(of(), set.headSet("c")); - assertSame(of(), set.headSet("e")); + assertSame(this.of(), set.headSet("c")); + assertSame(this.of(), set.headSet("e")); } public void testSingle_tailSet() { @@ -307,7 +307,7 @@ public void testSingle_tailSet() { assertTrue(set.tailSet("c") instanceof ImmutableSortedSet); assertThat(set.tailSet("c")).contains("e"); assertThat(set.tailSet("e")).contains("e"); - assertSame(of(), set.tailSet("g")); + assertSame(this.of(), set.tailSet("g")); } public void testSingle_subSet() { @@ -315,9 +315,9 @@ public void testSingle_subSet() { assertTrue(set.subSet("c", "g") instanceof ImmutableSortedSet); assertThat(set.subSet("c", "g")).contains("e"); assertThat(set.subSet("e", "g")).contains("e"); - assertSame(of(), set.subSet("f", "g")); - assertSame(of(), set.subSet("c", "e")); - assertSame(of(), set.subSet("c", "d")); + assertSame(this.of(), set.subSet("f", "g")); + assertSame(this.of(), set.subSet("c", "e")); + assertSame(this.of(), set.subSet("c", "d")); } public void testSingle_first() { @@ -403,8 +403,8 @@ public void testOf_headSet() { assertTrue(set.headSet("e") instanceof ImmutableSortedSet); assertThat(set.headSet("e")).containsExactly("b", "c", "d").inOrder(); assertThat(set.headSet("g")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.headSet("a")); - assertSame(of(), set.headSet("b")); + assertSame(this.of(), set.headSet("a")); + assertSame(this.of(), set.headSet("b")); } public void testOf_tailSet() { @@ -412,7 +412,7 @@ public void testOf_tailSet() { assertTrue(set.tailSet("e") instanceof ImmutableSortedSet); assertThat(set.tailSet("e")).containsExactly("e", "f").inOrder(); assertThat(set.tailSet("a")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.tailSet("g")); + assertSame(this.of(), set.tailSet("g")); } public void testOf_subSet() { @@ -420,9 +420,9 @@ public void testOf_subSet() { assertTrue(set.subSet("c", "e") instanceof ImmutableSortedSet); assertThat(set.subSet("c", "e")).containsExactly("c", "d").inOrder(); assertThat(set.subSet("a", "g")).containsExactly("b", "c", "d", "e", "f").inOrder(); - assertSame(of(), set.subSet("a", "b")); - assertSame(of(), set.subSet("g", "h")); - assertSame(of(), set.subSet("c", "c")); + assertSame(this.of(), set.subSet("a", "b")); + assertSame(this.of(), set.subSet("g", "h")); + assertSame(this.of(), set.subSet("c", "c")); try { set.subSet("e", "c"); fail(); @@ -1191,7 +1191,7 @@ private static ImmutableList sortedNumberNames(int i, int j) { ImmutableList.of("one", "two", "three", "four", "five", "six", "seven"); private static final ImmutableList SORTED_NUMBER_NAMES = - Ordering.natural().immutableSortedCopy(NUMBER_NAMES); + Ordering.natural().immutableSortedCopy(NUMBER_NAMES); private static class SelfComparableExample implements Comparable { @Override diff --git a/guava-tests/test/com/google/common/collect/ImmutableTableTest.java b/guava-tests/test/com/google/common/collect/ImmutableTableTest.java index 22d151fb088c..5baf6344e454 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableTableTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableTableTest.java @@ -34,9 +34,9 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class ImmutableTableTest extends AbstractTableReadTest { +public class ImmutableTableTest extends AbstractTableReadTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { ImmutableTable.Builder builder = ImmutableTable.builder(); for (int i = 0; i < data.length; i = i + 3) { builder.put((String) data[i], (Integer) data[i + 1], (Character) data[i + 2]); diff --git a/guava-tests/test/com/google/common/collect/IterablesTest.java b/guava-tests/test/com/google/common/collect/IterablesTest.java index 2e7d89712808..68158d86c132 100644 --- a/guava-tests/test/com/google/common/collect/IterablesTest.java +++ b/guava-tests/test/com/google/common/collect/IterablesTest.java @@ -179,7 +179,7 @@ public void testGetOnlyElement_withDefault_empty() { public void testGetOnlyElement_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getOnlyElement(iterable, null)); + assertNull(Iterables.<@Nullable String>getOnlyElement(iterable, null)); } public void testGetOnlyElement_withDefault_multiple() { @@ -508,7 +508,7 @@ public void testPaddedPartition_basic() { List list = asList(1, 2, 3, 4, 5); Iterable> partitions = Iterables.paddedPartition(list, 2); assertEquals(3, Iterables.size(partitions)); - assertEquals(asList(5, null), Iterables.getLast(partitions)); + assertEquals(Arrays.<@Nullable Integer>asList(5, null), Iterables.getLast(partitions)); } public void testPaddedPartitionRandomAccessInput() { @@ -566,7 +566,7 @@ public void testElementsEqual() throws Exception { assertFalse(Iterables.elementsEqual(a, b)); // null versus non-null. - a = asList(4, 8, 15, null, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42); b = asList(4, 8, 15, 16, 23, 42); assertFalse(Iterables.elementsEqual(a, b)); assertFalse(Iterables.elementsEqual(b, a)); @@ -853,7 +853,7 @@ public void testGetFirst_withDefault_empty() { public void testGetFirst_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getFirst(iterable, null)); + assertNull(Iterables.<@Nullable String>getFirst(iterable, null)); } public void testGetFirst_withDefault_multiple() { @@ -892,7 +892,7 @@ public void testGetLast_withDefault_empty() { public void testGetLast_withDefault_empty_null() { Iterable iterable = Collections.emptyList(); - assertNull(Iterables.getLast(iterable, null)); + assertNull(Iterables.<@Nullable String>getLast(iterable, null)); } public void testGetLast_withDefault_multiple() { @@ -1387,7 +1387,7 @@ public void testMergeSorted_pyramid() { list.add(j); allIntegers.add(j); } - iterables.add(Ordering.natural().sortedCopy(list)); + iterables.add(Ordering.natural().sortedCopy(list)); } verifyMergeSorted(iterables, allIntegers); @@ -1404,7 +1404,7 @@ public void testMergeSorted_skipping_pyramid() { list.add(j * i); allIntegers.add(j * i); } - iterables.add(Ordering.natural().sortedCopy(list)); + iterables.add(Ordering.natural().sortedCopy(list)); } verifyMergeSorted(iterables, allIntegers); @@ -1422,7 +1422,7 @@ public void testIterables_nullCheck() throws Exception { private static void verifyMergeSorted( Iterable> iterables, Iterable unsortedExpected) { - Iterable expected = Ordering.natural().sortedCopy(unsortedExpected); + Iterable expected = Ordering.natural().sortedCopy(unsortedExpected); Iterable mergedIterator = Iterables.mergeSorted(iterables, Ordering.natural()); diff --git a/guava-tests/test/com/google/common/collect/IteratorsTest.java b/guava-tests/test/com/google/common/collect/IteratorsTest.java index 6d4b4307f237..7e173ab6a0bd 100644 --- a/guava-tests/test/com/google/common/collect/IteratorsTest.java +++ b/guava-tests/test/com/google/common/collect/IteratorsTest.java @@ -162,7 +162,7 @@ public void testSize_partiallyConsumed() { } public void test_contains_nonnull_yes() { - Iterator set = asList("a", null, "b").iterator(); + Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator(); assertTrue(Iterators.contains(set, "b")); } @@ -172,7 +172,7 @@ public void test_contains_nonnull_no() { } public void test_contains_null_yes() { - Iterator set = asList("a", null, "b").iterator(); + Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator(); assertTrue(Iterators.contains(set, null)); } @@ -241,7 +241,7 @@ public void testGetOnlyElement_withDefault_empty() { public void testGetOnlyElement_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getOnlyElement(iterator, null)); + assertNull(Iterators.<@Nullable String>getOnlyElement(iterator, null)); } public void testGetOnlyElement_withDefault_two() { @@ -513,7 +513,7 @@ public Integer apply(String from) { } public void testNullFriendlyTransform() { - Iterator input = asList(1, 2, null, 3).iterator(); + Iterator<@Nullable Integer> input = Arrays.<@Nullable Integer>asList(1, 2, null, 3).iterator(); Iterator result = Iterators.transform( input, @@ -821,7 +821,10 @@ public void testConcatPartiallyAdvancedFirst() { /** Illustrates the somewhat bizarre behavior when a null is passed in. */ public void testConcatContainingNull() { - Iterator> input = asList(iterateOver(1, 2), null, iterateOver(3)).iterator(); + Iterator> input = + (Iterator>) + Arrays.<@Nullable Iterator>asList(iterateOver(1, 2), null, iterateOver(3)) + .iterator(); Iterator result = Iterators.concat(input); assertEquals(1, (int) result.next()); assertEquals(2, (int) result.next()); @@ -939,8 +942,8 @@ public void testElementsEqual() { assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); // The same, but with nulls. - a = asList(4, 8, null, 16, 23, 42); - b = asList(4, 8, null, 16, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42); + b = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42); assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); // Different Iterable types (still equal elements, though). @@ -954,7 +957,7 @@ public void testElementsEqual() { assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); // null versus non-null. - a = asList(4, 8, 15, null, 23, 42); + a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42); b = asList(4, 8, 15, 16, 23, 42); assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator())); @@ -1072,17 +1075,17 @@ public void testPaddedPartition_singleton2() { Iterator> partitions = Iterators.paddedPartition(source, 2); assertTrue(partitions.hasNext()); assertTrue(partitions.hasNext()); - assertEquals(asList(1, null), partitions.next()); + assertEquals(Arrays.<@Nullable Integer>asList(1, null), partitions.next()); assertFalse(partitions.hasNext()); } @GwtIncompatible // fairly slow (~50s) public void testPaddedPartition_general() { + ImmutableList> expectedElements = + ImmutableList.of( + asList(1, 2, 3), asList(4, 5, 6), Arrays.<@Nullable Integer>asList(7, null, null)); new IteratorTester>( - 5, - IteratorFeature.UNMODIFIABLE, - ImmutableList.of(asList(1, 2, 3), asList(4, 5, 6), asList(7, null, null)), - IteratorTester.KnownOrder.KNOWN_ORDER) { + 5, IteratorFeature.UNMODIFIABLE, expectedElements, IteratorTester.KnownOrder.KNOWN_ORDER) { @Override protected Iterator> newTargetIterator() { Iterator source = Iterators.forArray(1, 2, 3, 4, 5, 6, 7); @@ -1287,7 +1290,8 @@ public void testToString() { } public void testToStringWithNull() { - Iterator iterator = Lists.newArrayList("hello", null, "world").iterator(); + Iterator<@Nullable String> iterator = + Lists.<@Nullable String>newArrayList("hello", null, "world").iterator(); assertEquals("[hello, null, world]", Iterators.toString(iterator)); } @@ -1355,7 +1359,7 @@ public void testGetNext_withDefault_empty() { public void testGetNext_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getNext(iterator, null)); + assertNull(Iterators.<@Nullable String>getNext(iterator, null)); } public void testGetNext_withDefault_two() { @@ -1391,7 +1395,7 @@ public void testGetLast_withDefault_empty() { public void testGetLast_withDefault_empty_null() { Iterator iterator = Iterators.emptyIterator(); - assertNull(Iterators.getLast(iterator, null)); + assertNull(Iterators.<@Nullable String>getLast(iterator, null)); } public void testGetLast_withDefault_two() { diff --git a/guava-tests/test/com/google/common/collect/MapsTest.java b/guava-tests/test/com/google/common/collect/MapsTest.java index 18a0e7d40f23..daf688f41627 100644 --- a/guava-tests/test/com/google/common/collect/MapsTest.java +++ b/guava-tests/test/com/google/common/collect/MapsTest.java @@ -95,8 +95,7 @@ public void testHashMapGeneralizesTypes() { original.put("a", 1); original.put("b", 2); original.put("c", 3); - HashMap map = - Maps.newHashMap((Map) original); + HashMap map = Maps.newHashMap(original); assertEquals(original, map); } diff --git a/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java b/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java index 049c1ea7df85..3583c5675848 100644 --- a/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java +++ b/guava-tests/test/com/google/common/collect/MapsTransformValuesUnmodifiableIteratorTest.java @@ -358,11 +358,12 @@ public void testTransformEntrySetContains() { Set> entries = map.entrySet(); assertTrue(entries.contains(Maps.immutableEntry("a", true))); - assertTrue(entries.contains(Maps.immutableEntry("b", (Boolean) null))); - assertTrue(entries.contains(Maps.immutableEntry((String) null, (Boolean) null))); + assertTrue(entries.contains(Maps.immutableEntry("b", null))); + assertTrue( + entries.contains(Maps.<@Nullable String, @Nullable Boolean>immutableEntry(null, null))); - assertFalse(entries.contains(Maps.immutableEntry("c", (Boolean) null))); - assertFalse(entries.contains(Maps.immutableEntry((String) null, true))); + assertFalse(entries.contains(Maps.immutableEntry("c", null))); + assertFalse(entries.contains(Maps.<@Nullable String, Boolean>immutableEntry(null, true))); } @Override diff --git a/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java b/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java index 83a835609a3a..ca19020ab9fd 100644 --- a/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java +++ b/guava-tests/test/com/google/common/collect/MinMaxPriorityQueueTest.java @@ -59,7 +59,7 @@ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault public class MinMaxPriorityQueueTest extends TestCase { - private static final Ordering SOME_COMPARATOR = Ordering.natural().reverse(); + private static final Ordering SOME_COMPARATOR = Ordering.natural().reverse(); @J2ktIncompatible @GwtIncompatible // suite diff --git a/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java b/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java index f6b327000c92..9244c0c11fb8 100644 --- a/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java +++ b/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java @@ -88,7 +88,8 @@ public void testOnlyElementSingleton() { } public void testOnlyElementNull() { - assertThat(Stream.of((Object) null).collect(MoreCollectors.onlyElement())).isNull(); + assertThat(Stream.<@Nullable Object>of((Object) null).collect(MoreCollectors.onlyElement())) + .isNull(); } public void testOnlyElementMultiple() { diff --git a/guava-tests/test/com/google/common/collect/MultimapsTest.java b/guava-tests/test/com/google/common/collect/MultimapsTest.java index 72b1413e492c..dce5cae26730 100644 --- a/guava-tests/test/com/google/common/collect/MultimapsTest.java +++ b/guava-tests/test/com/google/common/collect/MultimapsTest.java @@ -179,7 +179,8 @@ public void testUnmodifiableMultimapShortCircuit() { @GwtIncompatible // slow (~10s) public void testUnmodifiableArrayListMultimap() { - checkUnmodifiableMultimap(ArrayListMultimap.create(), true); + checkUnmodifiableMultimap( + ArrayListMultimap.<@Nullable String, @Nullable Integer>create(), true); } @J2ktIncompatible @@ -210,7 +211,7 @@ public void testUnmodifiableLinkedListMultimapRandomAccess() { @GwtIncompatible // slow (~10s) public void testUnmodifiableHashMultimap() { - checkUnmodifiableMultimap(HashMultimap.create(), false); + checkUnmodifiableMultimap(HashMultimap.<@Nullable String, @Nullable Integer>create(), false); } @J2ktIncompatible @@ -237,7 +238,9 @@ public void testSerializingUnmodifiableTreeMultimap() { @GwtIncompatible // slow (~10s) public void testUnmodifiableSynchronizedArrayListMultimap() { checkUnmodifiableMultimap( - Multimaps.synchronizedListMultimap(ArrayListMultimap.create()), true); + Multimaps.synchronizedListMultimap( + ArrayListMultimap.<@Nullable String, @Nullable Integer>create()), + true); } @J2ktIncompatible @@ -255,7 +258,9 @@ public void testSerializingUnmodifiableSynchronizedArrayListMultimap() { @GwtIncompatible // slow (~10s) public void testUnmodifiableSynchronizedHashMultimap() { checkUnmodifiableMultimap( - Multimaps.synchronizedSetMultimap(HashMultimap.create()), false); + Multimaps.synchronizedSetMultimap( + HashMultimap.<@Nullable String, @Nullable Integer>create()), + false); } @J2ktIncompatible @@ -332,7 +337,7 @@ public void testUnmodifiableMultimapEntries() { * multimap must support null keys and values. */ private static void checkUnmodifiableMultimap( - Multimap multimap, boolean permitsDuplicates) { + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates) { checkUnmodifiableMultimap(multimap, permitsDuplicates, null, null); } @@ -342,7 +347,7 @@ private static void checkUnmodifiableMultimap( * involving nulls. */ private static void checkUnmodifiableMultimap( - Multimap multimap, + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates, @Nullable String nullKey, @Nullable Integer nullValue) { @@ -371,8 +376,8 @@ private static void checkUnmodifiableMultimap( } /** Prepares the multimap for unmodifiable tests, returning an unmodifiable view of the map. */ - private static Multimap prepareUnmodifiableTests( - Multimap multimap, + private static Multimap<@Nullable String, @Nullable Integer> prepareUnmodifiableTests( + Multimap<@Nullable String, @Nullable Integer> multimap, boolean permitsDuplicates, @Nullable String nullKey, @Nullable Integer nullValue) { @@ -393,21 +398,26 @@ private static Multimap prepareUnmodifiableTests( assertEquals(8, multimap.size()); } - Multimap unmodifiable; + Multimap<@Nullable String, @Nullable Integer> unmodifiable; if (multimap instanceof SortedSetMultimap) { unmodifiable = - Multimaps.unmodifiableSortedSetMultimap((SortedSetMultimap) multimap); + Multimaps.unmodifiableSortedSetMultimap( + (SortedSetMultimap<@Nullable String, @Nullable Integer>) multimap); } else if (multimap instanceof SetMultimap) { - unmodifiable = Multimaps.unmodifiableSetMultimap((SetMultimap) multimap); + unmodifiable = + Multimaps.unmodifiableSetMultimap( + (SetMultimap<@Nullable String, @Nullable Integer>) multimap); } else if (multimap instanceof ListMultimap) { - unmodifiable = Multimaps.unmodifiableListMultimap((ListMultimap) multimap); + unmodifiable = + Multimaps.unmodifiableListMultimap( + (ListMultimap<@Nullable String, @Nullable Integer>) multimap); } else { unmodifiable = Multimaps.unmodifiableMultimap(multimap); } return unmodifiable; } - private static void assertUnmodifiableIterableInTandem( + private static void assertUnmodifiableIterableInTandem( Iterable unmodifiable, Iterable modifiable) { UnmodifiableCollectionTests.assertIteratorIsUnmodifiable(unmodifiable.iterator()); UnmodifiableCollectionTests.assertIteratorsInOrder( @@ -579,7 +589,7 @@ public void testForMapGetIteration() { IteratorTester tester = new IteratorTester( 4, MODIFIABLE, newHashSet(1), IteratorTester.KnownOrder.KNOWN_ORDER) { - private Multimap multimap; + private @Nullable Multimap multimap; @Override protected Iterator newTargetIterator() { @@ -878,9 +888,9 @@ public Integer apply(String input) { } public void testIndex_nullValue() { - List values = Arrays.asList(1, null); + List<@Nullable Integer> values = Arrays.asList(1, null); try { - Multimaps.index(values, Functions.identity()); + Multimaps.index((List) values, Functions.identity()); fail(); } catch (NullPointerException expected) { } diff --git a/guava-tests/test/com/google/common/collect/NewCustomTableTest.java b/guava-tests/test/com/google/common/collect/NewCustomTableTest.java index b048e4648a38..fc905f43ed90 100644 --- a/guava-tests/test/com/google/common/collect/NewCustomTableTest.java +++ b/guava-tests/test/com/google/common/collect/NewCustomTableTest.java @@ -22,6 +22,7 @@ import com.google.common.base.Supplier; import java.util.Map; import java.util.TreeMap; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Tables#newCustomTable}. @@ -30,10 +31,10 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public class NewCustomTableTest extends AbstractTableTest { +public class NewCustomTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Supplier> factory = new Supplier>() { @Override diff --git a/guava-tests/test/com/google/common/collect/ObjectArraysTest.java b/guava-tests/test/com/google/common/collect/ObjectArraysTest.java index edbcf2a9e55e..c3ad40278fb2 100644 --- a/guava-tests/test/com/google/common/collect/ObjectArraysTest.java +++ b/guava-tests/test/com/google/common/collect/ObjectArraysTest.java @@ -22,10 +22,10 @@ import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.testing.NullPointerTester; -import java.io.Serializable; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Unit test for {@code ObjectArrays}. @@ -58,6 +58,7 @@ public void testNewArray_fromClass_Nonempty() { assertNull(array[0]); } + @J2ktIncompatible // Array::class literal not available in Kotlin KMP @GwtIncompatible // ObjectArrays.newArray(Class, int) public void testNewArray_fromClass_OfArray() { String[][] array = ObjectArrays.newArray(String[].class, 1); @@ -117,14 +118,14 @@ public void testConcatBasic() { @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) public void testConcatWithMoreGeneralType() { - Serializable[] result = ObjectArrays.concat(new String[0], new String[0], Serializable.class); - assertEquals(Serializable[].class, result.getClass()); + CharSequence[] result = ObjectArrays.concat(new String[0], new String[0], CharSequence.class); + assertEquals(CharSequence[].class, result.getClass()); } public void testToArrayImpl1() { doTestToArrayImpl1(Lists.newArrayList()); doTestToArrayImpl1(Lists.newArrayList(1)); - doTestToArrayImpl1(Lists.newArrayList(1, null, 3)); + doTestToArrayImpl1(Lists.<@Nullable Integer>newArrayList(1, null, 3)); } private void doTestToArrayImpl1(List list) { @@ -142,15 +143,17 @@ public void testToArrayImpl2() { doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true); doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] {2, 3}, true); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false); - doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[0], false); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[2], false); + doTestToArrayImpl2(Lists.<@Nullable Integer>newArrayList(1, null, 3), new Integer[3], true); } private void doTestToArrayImpl2(List list, Integer[] array1, boolean expectModify) { Integer[] starting = Arrays.copyOf(array1, array1.length); Integer[] array2 = Arrays.copyOf(array1, array1.length); - Object[] reference = list.toArray(array1); + // TODO b/283448200 - Remove temporary variable when Kotlin smartcast issue is resolved. + Integer[] array1Tmp = array1; + Object[] reference = list.toArray(array1Tmp); Object[] target = ObjectArrays.toArrayImpl(list, array2); diff --git a/guava-tests/test/com/google/common/collect/OrderingTest.java b/guava-tests/test/com/google/common/collect/OrderingTest.java index 311552735a1e..3a50b54e648f 100644 --- a/guava-tests/test/com/google/common/collect/OrderingTest.java +++ b/guava-tests/test/com/google/common/collect/OrderingTest.java @@ -97,9 +97,9 @@ public void testComplicatedOrderingExample() { // [[null, null], [null], [1, null, 2], [1, 1], [1, 2], [1], [2], [], null] assertThat(sorted) .containsExactly( - Lists.newArrayList(nullInt, nullInt), - Lists.newArrayList(nullInt), - Lists.newArrayList(1, null, 2), + Lists.<@Nullable Integer>newArrayList(nullInt, nullInt), + Lists.<@Nullable Integer>newArrayList(nullInt), + Lists.<@Nullable Integer>newArrayList(1, null, 2), Lists.newArrayList(1, 1), Lists.newArrayList(1, 2), Lists.newArrayList(1), @@ -291,7 +291,7 @@ public Character apply(String string) { } private static Ordering byCharAt(int index) { - return Ordering.natural().onResultOf(CharAtFunction.values()[index]); + return Ordering.natural().onResultOf(CharAtFunction.values()[index]); } public void testCompound_static() { @@ -385,18 +385,18 @@ public Integer apply(String string) { } } - private static final Ordering DECREASING_INTEGER = Ordering.natural().reverse(); + private static final Ordering DECREASING_INTEGER = Ordering.natural().reverse(); public void testOnResultOf_natural() { Comparator comparator = - Ordering.natural().onResultOf(StringLengthFunction.StringLength); + Ordering.natural().onResultOf(StringLengthFunction.StringLength); assertTrue(comparator.compare("to", "be") == 0); assertTrue(comparator.compare("or", "not") < 0); assertTrue(comparator.compare("that", "to") > 0); new EqualsTester() .addEqualityGroup( - comparator, Ordering.natural().onResultOf(StringLengthFunction.StringLength)) + comparator, Ordering.natural().onResultOf(StringLengthFunction.StringLength)) .addEqualityGroup(DECREASING_INTEGER) .testEquals(); reserializeAndAssert(comparator); @@ -440,8 +440,8 @@ public void testLexicographical() { } public void testNullsFirst() { - Ordering ordering = Ordering.natural().nullsFirst(); - Helpers.testComparator(ordering, null, Integer.MIN_VALUE, 0, 1); + Ordering<@Nullable Integer> ordering = Ordering.natural().nullsFirst(); + Helpers.<@Nullable Integer>testComparator(ordering, null, Integer.MIN_VALUE, 0, 1); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsFirst()) @@ -451,8 +451,8 @@ public void testNullsFirst() { } public void testNullsLast() { - Ordering ordering = Ordering.natural().nullsLast(); - Helpers.testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); + Ordering<@Nullable Integer> ordering = Ordering.natural().nullsLast(); + Helpers.<@Nullable Integer>testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsLast()) @@ -467,9 +467,10 @@ public void testBinarySearch() { } public void testSortedCopy() { - List unsortedInts = Collections.unmodifiableList(Arrays.asList(5, 0, 3, null, 0, 9)); - List sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts); - assertEquals(Arrays.asList(0, 0, 3, 5, 9, null), sortedInts); + List<@Nullable Integer> unsortedInts = + Collections.unmodifiableList(Arrays.<@Nullable Integer>asList(5, 0, 3, null, 0, 9)); + List<@Nullable Integer> sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts); + assertEquals(Arrays.<@Nullable Integer>asList(0, 0, 3, 5, 9, null), sortedInts); assertEquals( Collections.emptyList(), numberOrdering.sortedCopy(Collections.emptyList())); @@ -484,9 +485,9 @@ public void testImmutableSortedCopy() { Collections.emptyList(), numberOrdering.immutableSortedCopy(Collections.emptyList())); - List listWithNull = Arrays.asList(5, 3, null, 9); + List<@Nullable Integer> listWithNull = Arrays.asList(5, 3, null, 9); try { - Ordering.natural().nullsFirst().immutableSortedCopy(listWithNull); + Ordering.natural().nullsFirst().immutableSortedCopy((List) listWithNull); fail(); } catch (NullPointerException expected) { } @@ -599,16 +600,17 @@ public void testLeastOfIterator_simple_1() { } public void testLeastOfIterable_simple_nMinusOne_withNullElement() { - List list = Arrays.asList(3, null, 5, -1); - List result = Ordering.natural().nullsLast().leastOf(list, list.size() - 1); + List<@Nullable Integer> list = Arrays.asList(3, null, 5, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list, list.size() - 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); } public void testLeastOfIterator_simple_nMinusOne_withNullElement() { - Iterator itr = Iterators.forArray(3, null, 5, -1); - List result = Ordering.natural().nullsLast().leastOf(itr, 3); + Iterator<@Nullable Integer> itr = Iterators.forArray(3, null, 5, -1); + List<@Nullable Integer> result = Ordering.natural().nullsLast().leastOf(itr, 3); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); @@ -647,19 +649,21 @@ public void testLeastOfIterator_simple_n() { } public void testLeastOfIterable_simple_n_withNullElement() { - List list = Arrays.asList(3, 4, 5, null, -1); - List result = Ordering.natural().nullsLast().leastOf(list, list.size()); + List<@Nullable Integer> list = Arrays.asList(3, 4, 5, null, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list, list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); - assertEquals(Arrays.asList(-1, 3, 4, 5, null), result); + assertEquals(Arrays.<@Nullable Integer>asList(-1, 3, 4, 5, null), result); } public void testLeastOfIterator_simple_n_withNullElement() { - List list = Arrays.asList(3, 4, 5, null, -1); - List result = Ordering.natural().nullsLast().leastOf(list.iterator(), list.size()); + List<@Nullable Integer> list = Arrays.asList(3, 4, 5, null, -1); + List<@Nullable Integer> result = + Ordering.natural().nullsLast().leastOf(list.iterator(), list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); - assertEquals(Arrays.asList(-1, 3, 4, 5, null), result); + assertEquals(Arrays.<@Nullable Integer>asList(-1, 3, 4, 5, null), result); } public void testLeastOfIterable_simple_nPlusOne() { @@ -730,14 +734,15 @@ private static void runLeastOfComparison(int iterations, int elements, int seeds public void testLeastOfIterableLargeK() { List list = Arrays.asList(4, 2, 3, 5, 1); - assertEquals(Arrays.asList(1, 2, 3, 4, 5), Ordering.natural().leastOf(list, Integer.MAX_VALUE)); + assertEquals( + Arrays.asList(1, 2, 3, 4, 5), Ordering.natural().leastOf(list, Integer.MAX_VALUE)); } public void testLeastOfIteratorLargeK() { List list = Arrays.asList(4, 2, 3, 5, 1); assertEquals( Arrays.asList(1, 2, 3, 4, 5), - Ordering.natural().leastOf(list.iterator(), Integer.MAX_VALUE)); + Ordering.natural().leastOf(list.iterator(), Integer.MAX_VALUE)); } public void testGreatestOfIterable_simple() { @@ -1094,7 +1099,7 @@ public T apply(Integer from) { composites.add(new Composite(t, 2)); } Ordering> ordering = - Ordering.natural() + Ordering.>natural() .compound(scenario.ordering.onResultOf(Composite.getValueFunction())); return new Scenario>( ordering, composites, (Composite[]) new Composite[0]); diff --git a/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java b/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java index d070936c3bac..22686633c6ae 100644 --- a/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java +++ b/guava-tests/test/com/google/common/collect/PeekingIteratorTest.java @@ -106,7 +106,7 @@ public void testPeekingIteratorBehavesLikeIteratorOnThreeElementIterable() { @GwtIncompatible // works but takes 5 minutes to run public void testPeekingIteratorAcceptsNullElements() { - actsLikeIteratorHelper(Lists.newArrayList(null, "A", null)); + actsLikeIteratorHelper(Lists.<@Nullable String>newArrayList(null, "A", null)); } public void testPeekOnEmptyList() { diff --git a/guava-tests/test/com/google/common/collect/SetsTest.java b/guava-tests/test/com/google/common/collect/SetsTest.java index 3e6f01d2b67c..278127a40374 100644 --- a/guava-tests/test/com/google/common/collect/SetsTest.java +++ b/guava-tests/test/com/google/common/collect/SetsTest.java @@ -894,7 +894,7 @@ public void testPowerSetContents() { assertTrue(powerSet.contains(subset)); } assertFalse(powerSet.contains(ImmutableSet.of(1, 2, 4))); - assertFalse(powerSet.contains(singleton(null))); + assertFalse(powerSet.contains(Collections.<@Nullable Integer>singleton(null))); assertFalse(powerSet.contains(null)); assertFalse(powerSet.contains((Object) "notASet")); } diff --git a/guava-tests/test/com/google/common/collect/StreamsTest.java b/guava-tests/test/com/google/common/collect/StreamsTest.java index fc64f84d893a..ca607e1a38db 100644 --- a/guava-tests/test/com/google/common/collect/StreamsTest.java +++ b/guava-tests/test/com/google/common/collect/StreamsTest.java @@ -40,6 +40,7 @@ import java.util.stream.LongStream; import java.util.stream.Stream; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** Unit test for {@link Streams}. */ @GwtCompatible(emulated = true) @@ -307,7 +308,9 @@ public void testMapWithIndex_linkedHashSetSource() { public void testMapWithIndex_unsizedSource() { testMapWithIndex( - elems -> Stream.of((Object) null).flatMap(unused -> ImmutableList.copyOf(elems).stream())); + elems -> + Stream.<@Nullable Object>of((Object) null) + .flatMap(unused -> ImmutableList.copyOf(elems).stream())); } public void testMapWithIndex_closeIsPropagated_sizedSource() { @@ -316,7 +319,7 @@ public void testMapWithIndex_closeIsPropagated_sizedSource() { public void testMapWithIndex_closeIsPropagated_unsizedSource() { testMapWithIndex_closeIsPropagated( - Stream.of((Object) null).flatMap(unused -> Stream.of("a", "b", "c"))); + Stream.<@Nullable Object>of((Object) null).flatMap(unused -> Stream.of("a", "b", "c"))); } private void testMapWithIndex_closeIsPropagated(Stream source) { diff --git a/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java b/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java index 216f32b2e6cf..aaa7d1662fc6 100644 --- a/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java +++ b/guava-tests/test/com/google/common/collect/SynchronizedTableTest.java @@ -22,7 +22,7 @@ import java.util.Set; import org.checkerframework.checker.nullness.qual.Nullable; -public class SynchronizedTableTest extends AbstractTableTest { +public class SynchronizedTableTest extends AbstractTableTest { private static final class TestTable implements Table, Serializable { final Table delegate = HashBasedTable.create(); public final Object mutex = new Integer(1); // something Serializable @@ -164,7 +164,7 @@ public Map> rowMap() { } @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { TestTable table = new TestTable<>(); Table synced = Synchronized.table(table, table.mutex); populate(synced, data); diff --git a/guava-tests/test/com/google/common/collect/TableCollectorsTest.java b/guava-tests/test/com/google/common/collect/TableCollectorsTest.java index 8ecfcde69cb1..cfd9161e98df 100644 --- a/guava-tests/test/com/google/common/collect/TableCollectorsTest.java +++ b/guava-tests/test/com/google/common/collect/TableCollectorsTest.java @@ -19,6 +19,7 @@ import static com.google.common.collect.Tables.immutableCell; import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Equivalence; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -210,15 +211,22 @@ public void testToTableNullMerge() { ImmutableTable.of(), immutableCell("one", "uno", 1), immutableCell("one", "uno", 2)); } + // https://youtrack.jetbrains.com/issue/KT-58242/. Crash when getValue result (null) is unboxed + @J2ktIncompatible public void testToTableNullValues() { Collector, ?, Table> collector = TableCollectors.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, - () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"))); + () -> { + Table table = + ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno")); + return (Table) table; + }); try { - Stream.of(immutableCell("one", "uno", (Integer) null)).collect(collector); + Cell cell = immutableCell("one", "uno", null); + Stream.of((Cell) cell).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } @@ -260,7 +268,8 @@ public void testToTableMerging() { // This function specifically returns a BiPredicate, because Guava7’s Equivalence class does not // actually implement BiPredicate, and CollectorTests expects a BiPredicate. - static > BiPredicate pairwiseOnResultOf(Function arg) { + static > + BiPredicate pairwiseOnResultOf(Function arg) { Equivalence equivalence = Equivalence.equals().pairwise().onResultOf(arg); return equivalence::equivalent; } diff --git a/guava-tests/test/com/google/common/collect/TablesTest.java b/guava-tests/test/com/google/common/collect/TablesTest.java index 4469be090a9f..1eeb3afd6f0f 100644 --- a/guava-tests/test/com/google/common/collect/TablesTest.java +++ b/guava-tests/test/com/google/common/collect/TablesTest.java @@ -22,6 +22,7 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.SerializableTester; import junit.framework.TestCase; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Tests for {@link Tables}. @@ -41,7 +42,8 @@ public void testImmutableEntryToString() { Cell entry = Tables.immutableCell("foo", 1, 'a'); assertEquals("(foo,1)=a", entry.toString()); - Cell nullEntry = Tables.immutableCell(null, null, null); + Cell<@Nullable String, @Nullable Integer, @Nullable Character> nullEntry = + Tables.immutableCell(null, null, null); assertEquals("(null,null)=null", nullEntry.toString()); } @@ -53,18 +55,27 @@ public void testEntryEquals() { .addEqualityGroup(Tables.immutableCell("bar", 1, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 2, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'b')) - .addEqualityGroup(Tables.immutableCell(null, null, null)) + .addEqualityGroup( + Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell( + null, null, null)) .testEquals(); } public void testEntryEqualsNull() { - Cell entry = Tables.immutableCell(null, null, null); + Cell<@Nullable String, @Nullable Integer, @Nullable Character> entry = + Tables.immutableCell(null, null, null); new EqualsTester() - .addEqualityGroup(entry, Tables.immutableCell(null, null, null)) - .addEqualityGroup(Tables.immutableCell("bar", null, null)) - .addEqualityGroup(Tables.immutableCell(null, 2, null)) - .addEqualityGroup(Tables.immutableCell(null, null, 'b')) + .addEqualityGroup( + entry, + Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell( + null, null, null)) + .addEqualityGroup( + Tables.immutableCell("bar", null, null)) + .addEqualityGroup( + Tables.<@Nullable Object, Integer, @Nullable Object>immutableCell(null, 2, null)) + .addEqualityGroup( + Tables.<@Nullable Object, @Nullable Object, Character>immutableCell(null, null, 'b')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'a')) .testEquals(); } diff --git a/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java b/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java index 20251795ce9a..8039b1dec5c1 100644 --- a/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java +++ b/guava-tests/test/com/google/common/collect/TablesTransformValuesTest.java @@ -31,7 +31,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class TablesTransformValuesTest extends AbstractTableTest { +public class TablesTransformValuesTest extends AbstractTableTest { private static final Function<@Nullable String, @Nullable Character> FIRST_CHARACTER = new Function<@Nullable String, @Nullable Character>() { @@ -42,7 +42,7 @@ public class TablesTransformValuesTest extends AbstractTableTest { }; @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table table = HashBasedTable.create(); checkArgument(data.length % 3 == 0); for (int i = 0; i < data.length; i += 3) { diff --git a/guava-tests/test/com/google/common/collect/TopKSelectorTest.java b/guava-tests/test/com/google/common/collect/TopKSelectorTest.java index 2cebdc338b08..05ecb2040fe4 100644 --- a/guava-tests/test/com/google/common/collect/TopKSelectorTest.java +++ b/guava-tests/test/com/google/common/collect/TopKSelectorTest.java @@ -37,12 +37,12 @@ public class TopKSelectorTest extends TestCase { public void testNegativeK() { try { - TopKSelector.least(-1); + TopKSelector.least(-1); fail(); } catch (IllegalArgumentException expected) { } try { - TopKSelector.greatest(-1); + TopKSelector.greatest(-1); fail(); } catch (IllegalArgumentException expected) { } diff --git a/guava-tests/test/com/google/common/collect/TransposedTableTest.java b/guava-tests/test/com/google/common/collect/TransposedTableTest.java index e556ea88fa9a..c560aa2dda19 100644 --- a/guava-tests/test/com/google/common/collect/TransposedTableTest.java +++ b/guava-tests/test/com/google/common/collect/TransposedTableTest.java @@ -17,6 +17,7 @@ package com.google.common.collect; import com.google.common.annotations.GwtCompatible; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link Tables#transpose}. @@ -25,10 +26,10 @@ */ @GwtCompatible @ElementTypesAreNonnullByDefault -public class TransposedTableTest extends AbstractTableTest { +public class TransposedTableTest extends AbstractTableTest { @Override - protected Table create(Object... data) { + protected Table create(@Nullable Object... data) { Table original = HashBasedTable.create(); Table table = Tables.transpose(original); table.clear(); diff --git a/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java b/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java index f42673766a21..c1a9f0066b9f 100644 --- a/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java +++ b/guava-tests/test/com/google/common/collect/TreeBasedTableTest.java @@ -35,6 +35,7 @@ import java.util.SortedMap; import junit.framework.Test; import junit.framework.TestSuite; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Test cases for {@link TreeBasedTable}. @@ -44,7 +45,7 @@ */ @GwtCompatible(emulated = true) @ElementTypesAreNonnullByDefault -public class TreeBasedTableTest extends AbstractTableTest { +public class TreeBasedTableTest extends AbstractTableTest { @J2ktIncompatible @GwtIncompatible // suite public static Test suite() { @@ -90,7 +91,7 @@ protected TreeBasedTable create( } @Override - protected TreeBasedTable create(Object... data) { + protected TreeBasedTable create(@Nullable Object... data) { TreeBasedTable table = TreeBasedTable.create(); table.put("foo", 4, 'a'); table.put("cat", 1, 'b'); @@ -285,7 +286,7 @@ public void testColumnKeySet_isSortedWithRealComparator() { table = create( String.CASE_INSENSITIVE_ORDER, - Ordering.natural().reverse(), + Ordering.natural().reverse(), "a", 2, 'X', diff --git a/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java b/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java index 601709b105e6..e887c0fadfa4 100644 --- a/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java +++ b/guava-tests/test/com/google/common/collect/TreeMultimapExplicitTest.java @@ -150,10 +150,10 @@ public void testOrderedEntries() { TreeMultimap<@Nullable String, @Nullable Integer> multimap = createPopulate(); assertThat(multimap.entries()) .containsExactly( - Maps.immutableEntry((String) null, 7), - Maps.immutableEntry((String) null, 3), - Maps.immutableEntry((String) null, 1), - Maps.immutableEntry("tree", (Integer) null), + Maps.<@Nullable String, Integer>immutableEntry(null, 7), + Maps.<@Nullable String, Integer>immutableEntry(null, 3), + Maps.<@Nullable String, Integer>immutableEntry(null, 1), + Maps.immutableEntry("tree", null), Maps.immutableEntry("tree", 0), Maps.immutableEntry("google", 6), Maps.immutableEntry("google", 2)) @@ -193,7 +193,7 @@ public void testSortedKeySet() { assertEquals(null, keySet.first()); assertEquals("google", keySet.last()); assertEquals(StringLength.COMPARATOR, keySet.comparator()); - assertEquals(Sets.newHashSet(null, "tree"), keySet.headSet("yahoo")); + assertEquals(Sets.<@Nullable String>newHashSet(null, "tree"), keySet.headSet("yahoo")); assertEquals(Sets.newHashSet("google"), keySet.tailSet("yahoo")); assertEquals(Sets.newHashSet("tree"), keySet.subSet("ask", "yahoo")); } diff --git a/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java b/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java index 012a14c5aab6..f475e87f996c 100644 --- a/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java +++ b/guava-tests/test/com/google/common/collect/TreeMultimapNaturalTest.java @@ -354,7 +354,7 @@ public void testMultimapConstructor() { private static final Comparator KEY_COMPARATOR = Ordering.natural(); private static final Comparator VALUE_COMPARATOR = - Ordering.natural().reverse().nullsFirst(); + Ordering.natural().reverse().nullsFirst(); /** * Test that creating one TreeMultimap from another does not copy the comparators from the source diff --git a/guava-tests/test/com/google/common/collect/TreeMultisetTest.java b/guava-tests/test/com/google/common/collect/TreeMultisetTest.java index 0077b75d168a..6f9651a9df51 100644 --- a/guava-tests/test/com/google/common/collect/TreeMultisetTest.java +++ b/guava-tests/test/com/google/common/collect/TreeMultisetTest.java @@ -287,8 +287,8 @@ public int compare(String o1, String o2) { } public void testNullAcceptingComparator() throws Exception { - Comparator comparator = Ordering.natural().nullsFirst(); - TreeMultiset ms = TreeMultiset.create(comparator); + Comparator<@Nullable String> comparator = Ordering.natural().nullsFirst(); + TreeMultiset<@Nullable String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add(null); diff --git a/guava-tests/test/com/google/common/io/CloserTest.java b/guava-tests/test/com/google/common/io/CloserTest.java index f30d771a8743..90c4eefe88af 100644 --- a/guava-tests/test/com/google/common/io/CloserTest.java +++ b/guava-tests/test/com/google/common/io/CloserTest.java @@ -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; @@ -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); } } diff --git a/guava-tests/test/com/google/common/math/LongMathTest.java b/guava-tests/test/com/google/common/math/LongMathTest.java index 25ad1dc325f0..21de0f55a837 100644 --- a/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/guava-tests/test/com/google/common/math/LongMathTest.java @@ -786,6 +786,7 @@ public void testBinomialNegative() { } + @J2ktIncompatible // slow enough to cause flakiness @GwtIncompatible // far too slow public void testSqrtOfPerfectSquareAsDoubleIsPerfect() { // This takes just over a minute on my machine. diff --git a/guava-tests/test/com/google/common/net/InternetDomainNameTest.java b/guava-tests/test/com/google/common/net/InternetDomainNameTest.java index c440ee53f509..b4cdb9b3b856 100644 --- a/guava-tests/test/com/google/common/net/InternetDomainNameTest.java +++ b/guava-tests/test/com/google/common/net/InternetDomainNameTest.java @@ -19,6 +19,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Ascii; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; @@ -501,6 +502,7 @@ private static InternetDomainName idn(String domain) { return InternetDomainName.from(domain); } + @J2ktIncompatible @GwtIncompatible // NullPointerTester public void testNulls() { final NullPointerTester tester = new NullPointerTester(); diff --git a/guava-tests/test/com/google/common/net/MediaTypeTest.java b/guava-tests/test/com/google/common/net/MediaTypeTest.java index a3428b32f53d..84ad5bc19fa7 100644 --- a/guava-tests/test/com/google/common/net/MediaTypeTest.java +++ b/guava-tests/test/com/google/common/net/MediaTypeTest.java @@ -35,6 +35,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; @@ -57,6 +58,7 @@ */ @GwtCompatible(emulated = true) public class MediaTypeTest extends TestCase { + @J2ktIncompatible @GwtIncompatible // reflection public void testParse_useConstants() throws Exception { for (MediaType constant : getConstants()) { @@ -64,6 +66,7 @@ public void testParse_useConstants() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testCreate_useConstants() throws Exception { for (MediaType constant : getConstants()) { @@ -74,6 +77,7 @@ public void testCreate_useConstants() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testConstants_charset() throws Exception { for (Field field : getConstantFields()) { @@ -86,11 +90,13 @@ public void testConstants_charset() throws Exception { } } + @J2ktIncompatible @GwtIncompatible // reflection public void testConstants_areUnique() { assertThat(getConstants()).containsNoDuplicates(); } + @J2ktIncompatible @GwtIncompatible // reflection private static FluentIterable getConstantFields() { return FluentIterable.from(asList(MediaType.class.getDeclaredFields())) @@ -107,6 +113,7 @@ && isFinal(modifiers) }); } + @J2ktIncompatible @GwtIncompatible // reflection private static FluentIterable getConstants() { return getConstantFields() @@ -525,6 +532,7 @@ public void testGetCharset() { assertThat(MediaType.parse("text/plain; charset=utf-8").charset()).hasValue(UTF_8); } + @J2ktIncompatible @GwtIncompatible // Non-UTF-8 Charset public void testGetCharset_utf16() { assertThat(MediaType.parse("text/plain; charset=utf-16").charset()).hasValue(UTF_16); @@ -604,6 +612,7 @@ public void testEquals() { .testEquals(); } + @J2ktIncompatible @GwtIncompatible // Non-UTF-8 Charset public void testEquals_nonUtf8Charsets() { new EqualsTester() @@ -613,6 +622,7 @@ public void testEquals_nonUtf8Charsets() { .testEquals(); } + @J2ktIncompatible @GwtIncompatible // com.google.common.testing.NullPointerTester public void testNullPointer() { NullPointerTester tester = new NullPointerTester(); diff --git a/guava-tests/test/com/google/common/primitives/BooleansTest.java b/guava-tests/test/com/google/common/primitives/BooleansTest.java index a968353a9f57..e6fbd1aed90d 100644 --- a/guava-tests/test/com/google/common/primitives/BooleansTest.java +++ b/guava-tests/test/com/google/common/primitives/BooleansTest.java @@ -579,9 +579,9 @@ public void testAsListCanonicalValues() { assertThat(list.get(0)).isSameInstanceAs(true); assertThat(list.get(1)).isSameInstanceAs(false); @SuppressWarnings("deprecation") - Boolean anotherTrue = true; + Boolean anotherTrue = new Boolean(true); @SuppressWarnings("deprecation") - Boolean anotherFalse = false; + Boolean anotherFalse = new Boolean(false); list.set(0, anotherTrue); assertThat(list.get(0)).isSameInstanceAs(true); list.set(1, anotherFalse); diff --git a/guava/src/com/google/common/base/Throwables.java b/guava/src/com/google/common/base/Throwables.java index 1f1c346e0256..73dad9790a85 100644 --- a/guava/src/com/google/common/base/Throwables.java +++ b/guava/src/com/google/common/base/Throwables.java @@ -162,7 +162,10 @@ public static void propagateIfPossible(@CheckForNull Throwable throwable) { * * @param throwable the Throwable to possibly propagate * @param declaredType the single checked exception type declared by the calling method + * @deprecated Use a combination of {@link #throwIfInstanceOf} and {@link #throwIfUnchecked}, + * which togther provide the same behavior except that they reject {@code null}. */ + @Deprecated @J2ktIncompatible @GwtIncompatible // propagateIfInstanceOf public static void propagateIfPossible( @@ -175,13 +178,14 @@ public static void propagateIfPossible( * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. * - *

Discouraged in favor of calling {@link #throwIfInstanceOf} and {@link - * #throwIfUnchecked}. - * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method + * @deprecated Use a combination of two calls to {@link #throwIfInstanceOf} and one call to {@link + * #throwIfUnchecked}, which togther provide the same behavior except that they reject {@code + * null}. */ + @Deprecated @J2ktIncompatible @GwtIncompatible // propagateIfInstanceOf public static void propagateIfPossible( diff --git a/guava/src/com/google/common/collect/TableCollectors.java b/guava/src/com/google/common/collect/TableCollectors.java index 0257954ee802..1164e822de53 100644 --- a/guava/src/com/google/common/collect/TableCollectors.java +++ b/guava/src/com/google/common/collect/TableCollectors.java @@ -83,7 +83,7 @@ final class TableCollectors { T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, @@ -104,7 +104,7 @@ final class TableCollectors { T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, diff --git a/guava/src/com/google/common/collect/Tables.java b/guava/src/com/google/common/collect/Tables.java index 326ae35153d6..d8aabde00ab3 100644 --- a/guava/src/com/google/common/collect/Tables.java +++ b/guava/src/com/google/common/collect/Tables.java @@ -70,7 +70,7 @@ private Tables() {} T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, @@ -99,7 +99,7 @@ private Tables() {} T extends @Nullable Object, R extends @Nullable Object, C extends @Nullable Object, - V extends @Nullable Object, + V, I extends Table> Collector toTable( java.util.function.Function rowFunction, diff --git a/guava/src/com/google/common/io/Closer.java b/guava/src/com/google/common/io/Closer.java index 8e1db7a83096..b1512ae14ca7 100644 --- a/guava/src/com/google/common/io/Closer.java +++ b/guava/src/com/google/common/io/Closer.java @@ -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; @@ -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); } @@ -171,8 +173,9 @@ public RuntimeException rethrow(Throwable e, Class 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); } @@ -195,8 +198,10 @@ public RuntimeException rethrow( Throwable e, Class declaredType1, Class 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); } @@ -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 } } diff --git a/guava/src/com/google/common/util/concurrent/ExecutionError.java b/guava/src/com/google/common/util/concurrent/ExecutionError.java index 303383039fc0..a5a2a1a3aae9 100644 --- a/guava/src/com/google/common/util/concurrent/ExecutionError.java +++ b/guava/src/com/google/common/util/concurrent/ExecutionError.java @@ -31,25 +31,55 @@ @ElementTypesAreNonnullByDefault public class ExecutionError extends Error { /* - * Ideally, this class would have exposed only constructors that require a non-null cause. We - * might try to move in that direction, but there are complications. See + * Ideally, this class would have exposed only constructors that require a non-null cause. See * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789 + * and https://github.com/jspecify/jspecify/issues/490. + * + * (That would also have ensured that its cause was always an Error, rather than possibly another + * kind of Throwable that was later passed to initCause. Then we could have declared the override + * `public final Error getCause()`.) */ - /** Creates a new instance with {@code null} as its detail message. */ + /** + * Creates a new instance with {@code null} as its detail message and no cause. + * + * @deprecated Prefer {@linkplain ExecutionError(Error)} a constructor that accepts a cause: Users + * of this class typically expect for instances to have a non-null cause. At the moment, you + * can usually still preserve behavior by passing an explicit {@code null} cause. Note, + * however, that passing an explicit {@code null} cause prevents anyone from calling {@link + * #initCause} later, so it is not quite equivalent to using a constructor that omits the + * cause. + */ + @Deprecated protected ExecutionError() {} - /** Creates a new instance with the given detail message. */ + /** + * Creates a new instance with the given detail message and no cause. + * + * @deprecated Prefer {@linkplain ExecutionError(String, Error)} a constructor that accepts a + * cause: Users of this class typically expect for instances to have a non-null cause. At the + * moment, you can usually still preserve behavior by passing an explicit {@code null} + * cause. Note, however, that passing an explicit {@code null} cause prevents anyone from + * calling {@link #initCause} later, so it is not quite equivalent to using a constructor that + * omits the cause. + */ + @Deprecated protected ExecutionError(@CheckForNull String message) { super(message); } - /** Creates a new instance with the given detail message and cause. */ + /** + * Creates a new instance with the given detail message and cause. Prefer to provide a + * non-nullable {@code cause}, as many users expect to find one. + */ public ExecutionError(@CheckForNull String message, @CheckForNull Error cause) { super(message, cause); } - /** Creates a new instance with the given cause. */ + /** + * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to + * provide a non-nullable {@code cause}, as many users expect to find one. + */ public ExecutionError(@CheckForNull Error cause) { super(cause); } diff --git a/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java b/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java index a0ec427eeca4..bb6f5e04d054 100644 --- a/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java +++ b/guava/src/com/google/common/util/concurrent/UncheckedExecutionException.java @@ -36,25 +36,55 @@ @ElementTypesAreNonnullByDefault public class UncheckedExecutionException extends RuntimeException { /* - * Ideally, this class would have exposed only constructors that require a non-null cause. We - * might try to move in that direction, but there are complications. See + * Ideally, this class would have exposed only constructors that require a non-null cause. See * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789 + * and https://github.com/jspecify/jspecify/issues/490. + * + * (Perhaps it should also have required that its cause was a RuntimeException. However, that + * would have required that we throw a different kind of exception for wrapping *checked* + * exceptions in methods like Futures.getUnchecked and LoadingCache.get.) */ - /** Creates a new instance with {@code null} as its detail message. */ + /** + * Creates a new instance with {@code null} as its detail message and no cause. + * + * @deprecated Prefer {@linkplain UncheckedExecutionException(Throwable)} a constructor that + * accepts a cause: Users of this class typically expect for instances to have a non-null + * cause. At the moment, you can usually still preserve behavior by passing an explicit + * {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents + * anyone from calling {@link #initCause} later, so it is not quite equivalent to using a + * constructor that omits the cause. + */ + @Deprecated protected UncheckedExecutionException() {} - /** Creates a new instance with the given detail message. */ + /** + * Creates a new instance with the given detail message and no cause. + * + * @deprecated Prefer {@linkplain UncheckedExecutionException(String, Throwable)} a constructor + * that accepts a cause: Users of this class typically expect for instances to have a non-null + * cause. At the moment, you can usually still preserve behavior by passing an explicit + * {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents + * anyone from calling {@link #initCause} later, so it is not quite equivalent to using a + * constructor that omits the cause. + */ + @Deprecated protected UncheckedExecutionException(@CheckForNull String message) { super(message); } - /** Creates a new instance with the given detail message and cause. */ + /** + * Creates a new instance with the given detail message and cause. Prefer to provide a + * non-nullable {@code cause}, as many users expect to find one. + */ public UncheckedExecutionException(@CheckForNull String message, @CheckForNull Throwable cause) { super(message, cause); } - /** Creates a new instance with the given cause. */ + /** + * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to + * provide a non-nullable {@code cause}, as many users expect to find one. + */ public UncheckedExecutionException(@CheckForNull Throwable cause) { super(cause); }