Skip to content

Commit

Permalink
Make null-unsafety explicit in `TableCollectorsTest#testToTableNullVa…
Browse files Browse the repository at this point in the history
…lues`

Casting `ArrayTable` to `Table` is necessary as of cl 612591080, casting the immutable cell was overlooked because it was masked by incorrect type inference in J2KT (which meant that for J2KT, a different NPE was caught than the expected NPE). But now the test crashes on Kotlin Native due to robustness issues when it comes to incorrect unchecked casts.

PiperOrigin-RevId: 612842812
  • Loading branch information
martinkretzschmar authored and Google Java Core Libraries committed Mar 5, 2024
1 parent 4f7989d commit c96c7d4
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,16 +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<Cell<String, String, Integer>, ?, Table<String, String, @Nullable Integer>>
collector =
TableCollectors.toTable(
Cell::getRowKey,
Cell::getColumnKey,
Cell::getValue,
() -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno")));
Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
TableCollectors.toTable(
Cell::getRowKey,
Cell::getColumnKey,
Cell::getValue,
() -> {
Table<String, String, @Nullable Integer> table =
ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"));
return (Table<String, String, Integer>) table;
});
try {
Stream.of(immutableCell("one", "uno", (Integer) null)).collect(collector);
Cell<String, String, @Nullable Integer> cell = immutableCell("one", "uno", null);
Stream.of((Cell<String, String, Integer>) cell).collect(collector);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
Expand Down

0 comments on commit c96c7d4

Please sign in to comment.