Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Mar 8, 2024
1 parent 531cb40 commit 5822165
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.openrewrite.java.migrate.lang;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand All @@ -25,20 +27,21 @@

import java.time.Duration;
import java.util.List;
import java.util.Optional;

@Value
@EqualsAndHashCode(callSuper = false)
public class StringFormatted extends Recipe {

private static final MethodMatcher STRING_FORMAT = new MethodMatcher("java.lang.String format(String, ..)");

@Override
public String getDisplayName() {
return "Prefer `String#formatted(Object...)`";
return "Prefer `String.formatted(Object...)`";
}

@Override
public String getDescription() {
return "Prefer `String#formatted(Object...)` over `String#format(String, Object...)` in Java 17 or higher.";
return "Prefer `String.formatted(Object...)` over `String.format(String, Object...)` in Java 17 or higher.";
}

@Override
Expand All @@ -50,27 +53,29 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

private static class StringFormattedVisitor extends JavaVisitor<ExecutionContext> {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!STRING_FORMAT.matches(method) || method.getMethodType() == null) {
return method;
public J visitMethodInvocation(J.MethodInvocation m, ExecutionContext ctx) {
m = (J.MethodInvocation) super.visitMethodInvocation(m, ctx);
if (!STRING_FORMAT.matches(m) || m.getMethodType() == null) {
return m;
}

List<Expression> arguments = method.getArguments();
List<Expression> arguments = m.getArguments();
boolean wrapperNotNeeded = wrapperNotNeeded(arguments.get(0));
maybeRemoveImport("java.lang.String.format");
J.MethodInvocation mi = method.withName(method.getName().withSimpleName("formatted"));
Optional<JavaType.Method> formatted = method.getMethodType().getDeclaringType().getMethods().stream()
.filter(m -> m.getName().equals("formatted")).findAny();
mi = mi.withMethodType(formatted.orElse(null));
J.MethodInvocation mi = m.withName(m.getName().withSimpleName("formatted"));
JavaType.Method formatted = m.getMethodType().getDeclaringType().getMethods().stream()
.filter(it -> it.getName().equals("formatted"))
.findAny()
.orElse(null);
mi = mi.withMethodType(formatted);
if (mi.getName().getType() != null) {
mi = mi.withName(mi.getName().withType(mi.getMethodType()));
}
Expression select = wrapperNotNeeded ? arguments.get(0) :
new J.Parentheses<>(Tree.randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(arguments.get(0)));
mi = mi.withSelect(select);
mi = mi.withArguments(arguments.subList(1, arguments.size()));
return maybeAutoFormat(method, mi, ctx);
return maybeAutoFormat(m, mi, ctx);
}

private static boolean wrapperNotNeeded(Expression expression) {
Expand All @@ -85,5 +90,4 @@ private static boolean wrapperNotNeeded(Expression expression) {
public Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(1);
}

}

0 comments on commit 5822165

Please sign in to comment.