Skip to content

Commit b6c609d

Browse files
authored
Document null-probing semantic differences in singleton migrations (#1081)
`List.of(..)`, `Set.of(..)`, and `Map.of(..)` throw `NullPointerException` when probed with `contains(null)` and friends, whereas the `Collections.singleton*` factories return `false`/`-1`/`null`. Call this out in each recipe description so users can decide whether the migration is safe for their code. Also fixes a `Set.Of` typo.
1 parent 915f92e commit b6c609d

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonList.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public class MigrateCollectionsSingletonList extends Recipe {
3838
final String displayName = "Prefer `List.of(..)`";
3939

4040
@Getter
41-
final String description = "Prefer `List.of(..)` instead of using `Collections.singletonList()` in Java 9 or higher.";
41+
final String description = "Prefer `List.of(..)` instead of using `Collections.singletonList()` in Java 9 or higher. " +
42+
"Note that the resulting `List` is not behaviorally equivalent: `List.of(..)` throws `NullPointerException` when probed with `contains(null)`, `indexOf(null)`, or `lastIndexOf(null)`, " +
43+
"whereas `Collections.singletonList(..)` returns `false`/`-1`.";
4244

4345
@Override
4446
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public class MigrateCollectionsSingletonMap extends Recipe {
4040
final String displayName = "Prefer `Map.of(..)`";
4141

4242
@Getter
43-
final String description = "Prefer `Map.of(..)` instead of using `Collections.singletonMap()` in Java 9 or higher.";
43+
final String description = "Prefer `Map.of(..)` instead of using `Collections.singletonMap()` in Java 9 or higher. " +
44+
"Note that the resulting `Map` is not behaviorally equivalent: `Map.of(..)` throws `NullPointerException` when probed with `containsKey(null)`, `containsValue(null)`, or `get(null)`, " +
45+
"whereas `Collections.singletonMap(..)` returns `false`/`null`.";
4446

4547
@Override
4648
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonSet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class MigrateCollectionsSingletonSet extends Recipe {
3636
final String displayName = "Prefer `Set.of(..)`";
3737

3838
@Getter
39-
final String description = "Prefer `Set.Of(..)` instead of using `Collections.singleton()` in Java 9 or higher.";
39+
final String description = "Prefer `Set.of(..)` instead of using `Collections.singleton()` in Java 9 or higher. " +
40+
"Note that the resulting `Set` is not behaviorally equivalent: `Set.of(..)` throws `NullPointerException` when probed with `contains(null)`, " +
41+
"whereas `Collections.singleton(..)` returns `false`.";
4042

4143
@Override
4244
public TreeVisitor<?, ExecutionContext> getVisitor() {

0 commit comments

Comments
 (0)