From 190153b771a6ac0aaeb12b9d9b9d1428d1272acb Mon Sep 17 00:00:00 2001 From: amishra-u <119983081+amishra-u@users.noreply.github.com> Date: Sat, 7 Dec 2024 05:46:41 -0800 Subject: [PATCH] fix: FlowPath Analysis with chained lambda expressions (#73) --- .../openrewrite/analysis/util/CursorUtil.java | 2 +- .../FindLocalFlowPathsNumericTest.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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(); + } + } + """ + ) + ); + } }