Skip to content

Commit d348f55

Browse files
authored
preserve-whitespace-in-MigrateCollectionsUnmodifiableList (#806)
* Preserve original argument formatting in `MigrateCollectionsUnmodifiableList` Fixes #805 * Remove unused imports
1 parent e35d8e1 commit d348f55

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@
2424
import org.openrewrite.java.MethodMatcher;
2525
import org.openrewrite.java.search.UsesJavaVersion;
2626
import org.openrewrite.java.search.UsesMethod;
27-
import org.openrewrite.java.tree.Expression;
2827
import org.openrewrite.java.tree.J;
2928

30-
import java.util.List;
31-
import java.util.StringJoiner;
32-
3329
public class MigrateCollectionsUnmodifiableList extends Recipe {
3430
private static final MethodMatcher UNMODIFIABLE_LIST = new MethodMatcher("java.util.Collections unmodifiableList(java.util.List)", true);
3531
private static final MethodMatcher ARRAYS_AS_LIST = new MethodMatcher("java.util.Arrays asList(..)", true);
@@ -59,15 +55,16 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5955
maybeRemoveImport("java.util.Collections");
6056
maybeRemoveImport("java.util.Arrays");
6157
maybeAddImport("java.util.List");
62-
StringJoiner setOf = new StringJoiner(", ", "List.of(", ")");
63-
List<Expression> args = arraysInvocation.getArguments();
64-
args.forEach(o -> setOf.add("#{any()}"));
6558

66-
return JavaTemplate.builder(setOf.toString())
67-
.contextSensitive()
59+
// Build the List.of() invocation while preserving the original method's formatting
60+
J.MethodInvocation listOf = JavaTemplate.builder("List.of()")
6861
.imports("java.util.List")
6962
.build()
70-
.apply(updateCursor(m), m.getCoordinates().replace(), args.toArray());
63+
.apply(updateCursor(m), m.getCoordinates().replace());
64+
65+
// Replace the arguments while preserving their original formatting, comments, and padding
66+
return listOf.withArguments(arraysInvocation.getArguments())
67+
.getPadding().withArguments(arraysInvocation.getPadding().getArguments());
7168
}
7269
}
7370
}

src/test/java/org/openrewrite/java/migrate/util/MigrateCollectionsUnmodifiableListTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,40 @@ class Test {
7777
import java.time.LocalDate;
7878
7979
class Test {
80-
List<LocalDate> s = List.of(LocalDate.of(2010, 1, 1), LocalDate.now());
80+
List<LocalDate> s = List.of(LocalDate.of(2010,1,1),LocalDate.now());
81+
}
82+
"""
83+
),
84+
9
85+
)
86+
);
87+
}
88+
89+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/805")
90+
@Test
91+
void preserveComments() {
92+
//language=java
93+
rewriteRun(
94+
version(
95+
java(
96+
"""
97+
import java.util.*;
98+
99+
class Test {
100+
List<String> bar = Collections.unmodifiableList(Arrays.asList(
101+
"1", // this is one
102+
"2" // this is two
103+
));
104+
}
105+
""",
106+
"""
107+
import java.util.List;
108+
109+
class Test {
110+
List<String> bar = List.of(
111+
"1", // this is one
112+
"2" // this is two
113+
);
81114
}
82115
"""
83116
),

0 commit comments

Comments
 (0)