Skip to content

Commit

Permalink
Add org.openrewrite.java.migrate.lang.StringFormatted to Java 17 reci…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ build/
.idea/
.boot-releases
out/
.classpath
.project
.settings/
bin/
2 changes: 1 addition & 1 deletion .sdkmanrc
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
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);
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java-version-17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ recipeList:
key: maven.compiler.target
newValue: 17
addIfMissing: false
- org.openrewrite.java.migrate.lang.StringFormatted
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");
}
""")
)

}

0 comments on commit a300600

Please sign in to comment.