Skip to content

Commit

Permalink
Make J2CL super-source'd classes pass nullness checking.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 601121536
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jan 24, 2024
1 parent 4f394e6 commit fccfe96
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public Iterator<E> iterator() {
}

@Override
@SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return standardToArray(array);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ public boolean contains(@Nullable Object object) {
}

@Override
@SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
T[] result = super.toArray(array);
if (size() < result.length) {
// It works around a GWT bug where elements after last is not
// properly null'ed.
result[size()] = null;
@Nullable Object[] unsoundlyCovariantArray = result;
unsoundlyCovariantArray[size()] = null;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ public ImmutableSortedMap<K, V> tailMap(K fromKey) {
return newView(sortedDelegate.tailMap(fromKey));
}

public ImmutableSortedMap<K, V> tailMap(K fromKey, boolean inclusive) {
public ImmutableSortedMap<K, V> tailMap(K fromKeyParam, boolean inclusive) {
// Declare a "true" local variable so that the Checker Framework will infer nullness.
K fromKey = fromKeyParam;
checkNotNull(fromKey);
if (!inclusive) {
fromKey = higher(fromKey);
Expand Down Expand Up @@ -522,6 +524,6 @@ private static <K, V> SortedMap<K, V> newModifiableDelegate(Comparator<? super K
}

private static <E> Comparator<@Nullable E> nullAccepting(Comparator<E> comparator) {
return Ordering.from(comparator).nullsFirst();
return Ordering.from(comparator).<E>nullsFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,18 @@ public UnmodifiableIterator<E> iterator() {

@Override
public Object[] toArray() {
return ObjectArrays.toArrayImpl(this);
/*
* ObjectArrays.toArrayImpl returns `@Nullable Object[]` rather than `Object[]` but only because
* it can be used with collections that may contain null. This collection is a collection of
* non-null elements, so we can treat it as a plain `Object[]`.
*/
@SuppressWarnings("nullness")
Object[] result = ObjectArrays.toArrayImpl(this);
return result;
}

@Override
@SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] other) {
return ObjectArrays.toArrayImpl(this, other);
}
Expand Down

0 comments on commit fccfe96

Please sign in to comment.