Skip to content

Commit 37ee6dc

Browse files
committed
Annotate the parameter of List.sort as @Nullable.
This has always struck me as a weird feature, but people use it, and it "works" in the sense of "does not cause NPE" (though it may cause CCE...). I see both calls to `sort(null)` and calls like `sort(priorityQueue.comparator())` (which _might_ be `null`; [example](https://github.com/google/guava/blob/e82e2a2c07c68108f318958ee0355cc835c97743/guava-testlib/src/com/google/common/collect/testing/testers/SortedSetNavigationTester.java#L57)). And while I prefer a world in which methods like `comparator()` never return `null`, as we arranged for [in `SortedMultiset`](https://guava.dev/SortedMultiset#comparator()), there are apparently [downsides](google/guava#6187) to using a `Comparator` that implements natural order rather than using `null`. Is any of that convincing? :) This is definitely a case in which I can see how eisop would want to stay on its own path. My main motivation for doing this now is that I hear that the current signature is causing us trouble during Java->Kotlin transpilation. I can get more details.
1 parent f9395fb commit 37ee6dc

File tree

1 file changed

+1
-1
lines changed
  • src/java.base/share/classes/java/util

1 file changed

+1
-1
lines changed

src/java.base/share/classes/java/util/List.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ default void replaceAll(UnaryOperator<E> operator) {
514514
* @since 1.8
515515
*/
516516
@SuppressWarnings({"unchecked", "rawtypes"})
517-
default void sort(Comparator<? super E> c) {
517+
default void sort(@Nullable Comparator<? super E> c) {
518518
Object[] a = this.toArray();
519519
Arrays.sort(a, (Comparator) c);
520520
ListIterator<E> i = this.listIterator();

0 commit comments

Comments
 (0)