diff --git a/src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java b/src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java index d702643dc2..8e7fb92d8b 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/StringFormatted.java @@ -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; @@ -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 @@ -50,19 +53,21 @@ public TreeVisitor getVisitor() { private static class StringFormattedVisitor extends JavaVisitor { @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 arguments = method.getArguments(); + List arguments = m.getArguments(); boolean wrapperNotNeeded = wrapperNotNeeded(arguments.get(0)); maybeRemoveImport("java.lang.String.format"); - J.MethodInvocation mi = method.withName(method.getName().withSimpleName("formatted")); - Optional 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())); } @@ -70,7 +75,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) 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) { @@ -85,5 +90,4 @@ private static boolean wrapperNotNeeded(Expression expression) { public Duration getEstimatedEffortPerOccurrence() { return Duration.ofMinutes(1); } - }