diff --git a/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableList.java b/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableList.java index 5a5c0b8956..a61ce7081d 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableList.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableList.java @@ -76,7 +76,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu StringJoiner setOf = new StringJoiner(", ", "List.of(", ")"); List args = arraysInvocation.getArguments(); - args.forEach(o -> setOf.add("#{}")); + args.forEach(o -> setOf.add("#{any()}")); return autoFormat(m.withTemplate( JavaTemplate diff --git a/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSet.java b/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSet.java index e85b0118e4..7cb50a95c7 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSet.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSet.java @@ -77,7 +77,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu StringJoiner setOf = new StringJoiner(", ", "Set.of(", ")"); List args = ((J.MethodInvocation) newSet.getArguments().get(0)).getArguments(); - args.forEach(o -> setOf.add("#{}")); + args.forEach(o -> setOf.add("#{any()}")); return autoFormat(m.withTemplate( JavaTemplate diff --git a/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableListTest.kt b/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableListTest.kt index f0ccb4ee15..89283ca6ba 100644 --- a/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableListTest.kt +++ b/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableListTest.kt @@ -42,4 +42,25 @@ class MigrateCollectionsUnmodifiableListTest : JavaRecipeTest { } """ ) + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/67") + @Test + fun unmodifiableListTyped() = assertChanged( + before = """ + import java.util.*; + import java.time.LocalDate; + + class Test { + List s = Collections.unmodifiableList(Arrays.asList(LocalDate.of(2010,1,1),LocalDate.now())); + } + """, + after = """ + import java.util.List; + import java.time.LocalDate; + + class Test { + List s = List.of(LocalDate.of(2010, 1, 1), LocalDate.now()); + } + """ + ) } diff --git a/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSetTest.kt b/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSetTest.kt index ae1e05524a..2cff440ecf 100644 --- a/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSetTest.kt +++ b/src/test/kotlin/org/openrewrite/java/migrate/lang/MigrateCollectionsUnmodifiableSetTest.kt @@ -42,4 +42,25 @@ class MigrateCollectionsUnmodifiableSetTest : JavaRecipeTest { } """ ) + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/67") + @Test + fun unmodifiableSetTyped() = assertChanged( + before = """ + import java.util.*; + import java.time.LocalDate; + + class Test { + Set s = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(LocalDate.of(2010,1,1),LocalDate.now()))); + } + """, + after = """ + import java.util.Set; + import java.time.LocalDate; + + class Test { + Set s = Set.of(LocalDate.of(2010, 1, 1), LocalDate.now()); + } + """ + ) }