|
16 | 16 | package org.openrewrite.java;
|
17 | 17 |
|
18 | 18 | import org.junit.jupiter.api.Test;
|
| 19 | +import org.junitpioneer.jupiter.ExpectedToFail; |
19 | 20 | import org.openrewrite.ExecutionContext;
|
20 | 21 | import org.openrewrite.java.tree.Expression;
|
21 | 22 | import org.openrewrite.java.tree.J;
|
@@ -288,4 +289,118 @@ Stream<Integer> test() {
|
288 | 289 | )
|
289 | 290 | );
|
290 | 291 | }
|
| 292 | + |
| 293 | + @Test |
| 294 | + @ExpectedToFail |
| 295 | + void replaceMemberReferenceToLambda() { |
| 296 | + //noinspection Convert2MethodRef |
| 297 | + rewriteRun( |
| 298 | + spec -> spec |
| 299 | + .expectedCyclesThatMakeChanges(1).cycles(1) |
| 300 | + .recipe(toRecipe(() -> new JavaVisitor<>() { |
| 301 | + final JavaTemplate refTemplate = JavaTemplate.builder("T::toString") |
| 302 | + .bindType("java.util.function.Function<T, String>") |
| 303 | + .genericTypes("T") |
| 304 | + .build(); |
| 305 | + final JavaTemplate lambdaTemplate = JavaTemplate.builder("e -> e.toString()") |
| 306 | + .bindType("java.util.function.Function<T, String>") |
| 307 | + .genericTypes("T") |
| 308 | + .build(); |
| 309 | + |
| 310 | + @Override |
| 311 | + public J visitMemberReference(J.MemberReference memberRef, ExecutionContext executionContext) { |
| 312 | + JavaTemplate.Matcher matcher = refTemplate.matcher(getCursor()); |
| 313 | + if (matcher.find()) { |
| 314 | + return lambdaTemplate.apply(getCursor(), memberRef.getCoordinates().replace(), matcher.getMatchResult().getMatchedParameters().toArray()); |
| 315 | + } else { |
| 316 | + return super.visitMemberReference(memberRef, executionContext); |
| 317 | + } |
| 318 | + } |
| 319 | + })), |
| 320 | + //language=java |
| 321 | + java( |
| 322 | + """ |
| 323 | + import java.util.function.Function; |
| 324 | + |
| 325 | + class Foo { |
| 326 | + void test() { |
| 327 | + test(Object::toString); |
| 328 | + } |
| 329 | +
|
| 330 | + void test(Function<Object, String> fn) { |
| 331 | + } |
| 332 | + } |
| 333 | + """, |
| 334 | + """ |
| 335 | + import java.util.function.Function; |
| 336 | + |
| 337 | + class Foo { |
| 338 | + void test() { |
| 339 | + test(e -> e.toString()); |
| 340 | + } |
| 341 | +
|
| 342 | + void test(Function<Object, String> fn) { |
| 343 | + } |
| 344 | + } |
| 345 | + """ |
| 346 | + ) |
| 347 | + ); |
| 348 | + } |
| 349 | + |
| 350 | + @Test |
| 351 | + @ExpectedToFail |
| 352 | + void replaceLambdaToMemberReference() { |
| 353 | + //noinspection Convert2MethodRef |
| 354 | + rewriteRun( |
| 355 | + spec -> spec |
| 356 | + .expectedCyclesThatMakeChanges(1).cycles(1) |
| 357 | + .recipe(toRecipe(() -> new JavaVisitor<>() { |
| 358 | + final JavaTemplate lambdaTemplate = JavaTemplate.builder("e -> e.toString()") |
| 359 | + .bindType("java.util.function.Function<T, String>") |
| 360 | + .genericTypes("T") |
| 361 | + .build(); |
| 362 | + final JavaTemplate refTemplate = JavaTemplate.builder("T::toString") |
| 363 | + .bindType("java.util.function.Function<T, String>") |
| 364 | + .genericTypes("T") |
| 365 | + .build(); |
| 366 | + |
| 367 | + @Override |
| 368 | + public J visitLambda(J.Lambda lambda, ExecutionContext executionContext) { |
| 369 | + JavaTemplate.Matcher matcher = lambdaTemplate.matcher(getCursor()); |
| 370 | + if (matcher.find()) { |
| 371 | + return refTemplate.apply(getCursor(), lambda.getCoordinates().replace(), matcher.getMatchResult().getMatchedParameters().toArray()); |
| 372 | + } else { |
| 373 | + return super.visitLambda(lambda, executionContext); |
| 374 | + } |
| 375 | + } |
| 376 | + })), |
| 377 | + //language=java |
| 378 | + java( |
| 379 | + """ |
| 380 | + import java.util.function.Function; |
| 381 | + |
| 382 | + class Foo { |
| 383 | + void test() { |
| 384 | + test(e -> e.toString()); |
| 385 | + } |
| 386 | +
|
| 387 | + void test(Function<Object, String> fn) { |
| 388 | + } |
| 389 | + } |
| 390 | + """, |
| 391 | + """ |
| 392 | + import java.util.function.Function; |
| 393 | + |
| 394 | + class Foo { |
| 395 | + void test() { |
| 396 | + test(Object::toString); |
| 397 | + } |
| 398 | +
|
| 399 | + void test(Function<Object, String> fn) { |
| 400 | + } |
| 401 | + } |
| 402 | + """ |
| 403 | + ) |
| 404 | + ); |
| 405 | + } |
291 | 406 | }
|
0 commit comments