Skip to content

Commit 6e59237

Browse files
Tim te Beekpway99
Tim te Beek
authored andcommitted
Do not match String.format(Locale, String, Object...) and maintain original argument prefixes
1 parent 53c6fb1 commit 6e59237

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
import org.openrewrite.java.tree.J;
2828

2929
import java.time.Duration;
30-
import java.util.Collections;
3130
import java.util.List;
3231

3332
public class StringFormatted extends Recipe {
3433

35-
private static final MethodMatcher STRING_FORMAT = new MethodMatcher("java.lang.String format(..)");
34+
private static final MethodMatcher STRING_FORMAT = new MethodMatcher("java.lang.String format(String, ..)");
3635

3736
@Override
3837
public String getDisplayName() {
@@ -66,7 +65,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) {
6665
String template = String.format(wrapperNotNeeded(arguments.get(0))
6766
? "#{any(java.lang.String)}.formatted(%s)"
6867
: "(#{any(java.lang.String)}).formatted(%s)",
69-
String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any()}")));
68+
varargsTemplateWithOriginalWhitespace(arguments));
7069
maybeRemoveImport("java.lang.String.format");
7170
return mi.withTemplate(
7271
JavaTemplate.builder(this::getCursor, template)
@@ -81,6 +80,14 @@ private static boolean wrapperNotNeeded(Expression expression) {
8180
|| expression instanceof J.Literal
8281
|| expression instanceof J.MethodInvocation;
8382
}
83+
84+
private static String varargsTemplateWithOriginalWhitespace(List<Expression> arguments) {
85+
StringBuilder stringBuilder = new StringBuilder();
86+
for (Expression expression : arguments.subList(1, arguments.size())) {
87+
stringBuilder.append(expression.getPrefix().format("#{any()},"));
88+
}
89+
return stringBuilder.toString();
90+
}
8491
}
8592

8693
@Override

src/test/kotlin/org/openrewrite/java/migrate/lang/StringFormattedTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,55 @@ class StringFormattedTest : RewriteTest {
193193
""")
194194
)
195195

196+
@Test
197+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/122")
198+
fun doNotMatchLocale() = rewriteRun(
199+
java("""
200+
import java.util.Locale;
201+
class A {
202+
String str = String.format(Locale.US, "foo %s", "a");
203+
}
204+
""")
205+
)
206+
207+
@Test
208+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/122")
209+
fun doNotChangeLackOfWhitespace() = rewriteRun(
210+
java("""
211+
class A {
212+
String str = String.format("foo %s","a","b");
213+
}
214+
""",
215+
"""
216+
class A {
217+
String str = "foo %s".formatted("a","b");
218+
}
219+
""")
220+
)
221+
222+
@Test
223+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/122")
224+
fun doNotChangeWhitespaceWithNewlines() = rewriteRun(
225+
java("""
226+
class A {
227+
String str = String.format("foo %s",
228+
"a",
229+
"b");
230+
}
231+
""")
232+
)
233+
234+
@Test
235+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/122")
236+
fun doNotChangeWhitespaceWithNewlinesAndComments() = rewriteRun(
237+
java("""
238+
class A {
239+
String str = String.format("foo %s",
240+
"a",
241+
// B
242+
"b");
243+
}
244+
""")
245+
)
246+
196247
}

0 commit comments

Comments
 (0)