diff --git a/src/main/java/org/openrewrite/java/migrate/util/OptionalStreamRecipe.java b/src/main/java/org/openrewrite/java/migrate/util/OptionalStreamRecipe.java index df57a47eb..9b7d01ebe 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/OptionalStreamRecipe.java +++ b/src/main/java/org/openrewrite/java/migrate/util/OptionalStreamRecipe.java @@ -50,10 +50,6 @@ public TreeVisitor getVisitor() { } private static class OptionalStreamVisitor extends JavaIsoVisitor { - private static final JavaTemplate template = - JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)") - .imports("java.util.Optional") - .build(); @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, ExecutionContext ctx) { @@ -77,6 +73,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, E JRightPadded mapSelect = mapInvocation.getPadding().getSelect(); JavaType.Method mapInvocationType = mapInvocation.getMethodType(); Space flatMapComments = getFlatMapComments(mapSelect, filterSelect); + JavaTemplate template = + JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)") + .imports("java.util.Optional") + .build(); J.MethodInvocation flatMapInvocation = template .apply(updateCursor(mapInvocation), mapInvocation.getCoordinates().replace(), filterInvocation.getSelect()); return flatMapInvocation.getPadding()