|
32 | 32 | import org.openrewrite.java.search.UsesType; |
33 | 33 | import org.openrewrite.java.tree.Expression; |
34 | 34 | import org.openrewrite.java.tree.J; |
| 35 | +import org.openrewrite.java.tree.JRightPadded; |
35 | 36 | import org.openrewrite.java.tree.JavaType; |
| 37 | +import org.openrewrite.java.tree.Space; |
36 | 38 | import org.openrewrite.java.tree.TypeUtils; |
37 | 39 |
|
38 | 40 | import java.util.Arrays; |
|
45 | 47 | import java.util.stream.IntStream; |
46 | 48 |
|
47 | 49 | import static java.util.stream.Collectors.toList; |
| 50 | +import static java.util.stream.Collectors.toMap; |
| 51 | +import static java.util.stream.Collectors.toSet; |
48 | 52 |
|
49 | 53 | public class UpdateMockWebServerMockResponse extends Recipe { |
50 | 54 | private static final String OLD_PACKAGE_NAME = "okhttp3.mockwebserver"; |
@@ -202,17 +206,29 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu |
202 | 206 | return replacement; |
203 | 207 | } |
204 | 208 |
|
| 209 | + List<Integer> indices = methodInvocationsToAdjust.remove(mi.getId()); |
| 210 | + if (indices == null || indices.isEmpty()) { |
| 211 | + return mi; |
| 212 | + } |
| 213 | + |
205 | 214 | // Wrap MockResponse.Builder arguments with .build() |
206 | 215 | Cursor methodCursor = getCursor(); |
207 | | - replacement = mi.withArguments(ListUtils.map(mi.getArguments(), arg -> { |
208 | | - if (TypeUtils.isAssignableTo(NEW_MOCKRESPONSE_FQN_BUILDER, arg.getType())) { |
| 216 | + replacement = mi.withArguments(ListUtils.map(mi.getArguments(), (index, arg) -> { |
| 217 | + if (indices.contains(index) && TypeUtils.isAssignableTo(NEW_MOCKRESPONSE_FQN_BUILDER, arg.getType())) { |
209 | 218 | Cursor argCursor = new Cursor(methodCursor, arg); |
210 | | - return JavaTemplate |
211 | | - .builder("#{any(mockwebserver3.MockResponse$Builder)}.build()") |
| 219 | + boolean isChainedCall = arg instanceof J.MethodInvocation; |
| 220 | + String nl = isChainedCall ? "\n" : ""; |
| 221 | + J.MethodInvocation transformed = JavaTemplate |
| 222 | + .builder("#{any(mockwebserver3.MockResponse$Builder)}" + nl + ".build()") |
212 | 223 | .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockwebserver3")) |
213 | 224 | .imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder") |
214 | 225 | .build() |
215 | 226 | .apply(argCursor, arg.getCoordinates().replace(), arg); |
| 227 | + |
| 228 | + if (isChainedCall) { |
| 229 | + transformed = transformed.withPrefix(arg.getPrefix()); |
| 230 | + } |
| 231 | + return transformed; |
216 | 232 | } |
217 | 233 | return arg; |
218 | 234 | })); |
|
0 commit comments