-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add org.openrewrite.java.migrate.lang.StringFormatted to Java 17 reci…
…pes (#119) * Bup SDKman to use 11.0.16 * Update .gitignore to support Spring Tool Suite * Add StringFormatted starter, with two items still left TODO * Resolve TODO items * Add org.openrewrite.java.migrate.lang.StringFormatted to Java 17 recipes * No need for StringBuilder * Also test a split second argument
- Loading branch information
Tim te Beek
authored
Aug 22, 2022
1 parent
bba4904
commit a300600
Showing
5 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ build/ | |
.idea/ | ||
.boot-releases | ||
out/ | ||
.classpath | ||
.project | ||
.settings/ | ||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Enable auto-env through the sdkman_auto_env config | ||
# Add key=value pairs of SDKs to use below | ||
java=11.0.14-tem | ||
java=11.0.16-tem |
85 changes: 85 additions & 0 deletions
85
src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lang; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class StringFormatted extends Recipe { | ||
|
||
private static final MethodMatcher STRING_FORMAT = new MethodMatcher("java.lang.String format(..)"); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace `String#format(String, Object...)` with `String#formatted(Object...)`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Call `String#formatted(Object...)` on first argument to `String#format(String, Object...)`."; | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { | ||
return new UsesMethod<>(STRING_FORMAT); | ||
} | ||
|
||
@Override | ||
protected StringFormattedVisitor getVisitor() { | ||
return new StringFormattedVisitor(); | ||
} | ||
|
||
private static class StringFormattedVisitor extends JavaVisitor<ExecutionContext> { | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) { | ||
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, p); | ||
if (!STRING_FORMAT.matches(mi)) { | ||
return mi; | ||
} | ||
|
||
List<Expression> arguments = mi.getArguments(); | ||
String template = String.format( | ||
arguments.get(0) instanceof J.Literal | ||
? "#{any(java.lang.String)}.formatted(%s)" | ||
: "(#{any(java.lang.String)}).formatted(%s)", | ||
String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any()}"))); | ||
return mi.withTemplate( | ||
JavaTemplate.builder(this::getCursor, template) | ||
.javaParser(() -> JavaParser.fromJavaVersion().build()) | ||
.build(), | ||
mi.getCoordinates().replace(), | ||
arguments.toArray()); | ||
} | ||
} | ||
|
||
@Override | ||
public Duration getEstimatedEffortPerOccurrence() { | ||
return Duration.ofMinutes(1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/kotlin/org/openrewrite/java/migrate/lang/StringFormattedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lang | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.openrewrite.Issue | ||
import org.openrewrite.java.Assertions.java | ||
import org.openrewrite.java.JavaParser | ||
import org.openrewrite.test.RecipeSpec | ||
import org.openrewrite.test.RewriteTest | ||
|
||
class StringFormattedTest : RewriteTest { | ||
override fun defaults(spec: RecipeSpec) { | ||
spec.recipe(StringFormatted()) | ||
spec.parser(JavaParser.fromJavaVersion().build()) | ||
} | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun oneArgument() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo"); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = "foo".formatted(); | ||
} | ||
""") | ||
) | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun twoArguments() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo %s", "a"); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = "foo %s".formatted("a"); | ||
} | ||
""") | ||
) | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun threeArguments() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo %s %d", "a", 1); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = "foo %s %d".formatted("a", 1); | ||
} | ||
""") | ||
) | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun fourArguments() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo %s %d %f", "a", 1, 2.0); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = "foo %s %d %f".formatted("a", 1, 2.0); | ||
} | ||
""") | ||
) | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun splitFirstArgument() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo " + "%s", "a"); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = ("foo " + "%s").formatted("a"); | ||
} | ||
""") | ||
) | ||
|
||
@Test | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/77") | ||
fun splitSecondArgument() = rewriteRun( | ||
java(""" | ||
class A { | ||
String str = String.format("foo %s", "a" + "b"); | ||
} | ||
""", | ||
""" | ||
class A { | ||
String str = "foo %s".formatted("a" + "b"); | ||
} | ||
""") | ||
) | ||
|
||
} |