diff --git a/src/main/java/org/openrewrite/analysis/util/CursorUtil.java b/src/main/java/org/openrewrite/analysis/util/CursorUtil.java index fcaedf165..88e1f916c 100644 --- a/src/main/java/org/openrewrite/analysis/util/CursorUtil.java +++ b/src/main/java/org/openrewrite/analysis/util/CursorUtil.java @@ -38,7 +38,7 @@ public static Option findCallableBlockCursor(Cursor start) { Object next = nextCursor.getValue(); if (next instanceof J.Block) { methodDeclarationBlockCursor = nextCursor; - if (J.Block.isStaticOrInitBlock(nextCursor)) { + if (J.Block.isStaticOrInitBlock(nextCursor) || nextCursor.getParentTreeCursor().getValue() instanceof J.Lambda) { return Option.some(nextCursor); } } else if (next instanceof J.MethodDeclaration) { diff --git a/src/test/java/org/openrewrite/analysis/dataflow/FindLocalFlowPathsNumericTest.java b/src/test/java/org/openrewrite/analysis/dataflow/FindLocalFlowPathsNumericTest.java index 9ca9ebaf8..9abe1830f 100644 --- a/src/test/java/org/openrewrite/analysis/dataflow/FindLocalFlowPathsNumericTest.java +++ b/src/test/java/org/openrewrite/analysis/dataflow/FindLocalFlowPathsNumericTest.java @@ -107,4 +107,47 @@ void test() { ) ); } + + @Test + void flowPathDetectionWithChainedLambdas() { + //language=java + rewriteRun( + spec -> spec.expectedCyclesThatMakeChanges(1).cycles(1), + java( + """ + import java.util.List; + import java.util.stream.Stream; + + class Test { + void test() { + List list = Stream.of(1, 2, 3).peek(i -> { + System.out.println("Number " + i); + }).peek(i -> { + int n = 42; + int o = n; + System.out.println(o); + int p = o; + }).toList(); + } + } + """, """ + import java.util.List; + import java.util.stream.Stream; + + class Test { + void test() { + List list = Stream.of(1, 2, 3).peek(i -> { + System.out.println("Number " + i); + }).peek(i -> { + int n = /*~~>*/42; + int o = /*~~>*/n; + System.out.println(/*~~>*/o); + int p = /*~~>*/o; + }).toList(); + } + } + """ + ) + ); + } }