diff --git a/src/main/java/org/openrewrite/java/spring/boot3/BeanMethodReturnNull.java b/src/main/java/org/openrewrite/java/spring/boot3/BeanMethodReturnNull.java index 2384631a7..d4538299c 100644 --- a/src/main/java/org/openrewrite/java/spring/boot3/BeanMethodReturnNull.java +++ b/src/main/java/org/openrewrite/java/spring/boot3/BeanMethodReturnNull.java @@ -16,7 +16,6 @@ package org.openrewrite.java.spring.boot3; import org.openrewrite.ExecutionContext; -import org.openrewrite.NlsRewrite; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; @@ -62,9 +61,6 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut md.getReturnTypeExpression() != null && md.getReturnTypeExpression().getType() == JavaType.Primitive.Void ) { md = md.withReturnTypeExpression(TypeTree.build(" Object")); - - getCursor().putMessage(MSG_RETURN_VOID, true); - // Add `return null;` if the method does not have a return statement List statements = md.getBody().getStatements(); if (statements.isEmpty() || !(statements.get(statements.size() - 1) instanceof J.Return)) { @@ -73,6 +69,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut .build() .apply(updateCursor(md), md.getBody().getCoordinates().lastStatement()); } + + getCursor().putMessage(MSG_RETURN_VOID, true); } return super.visitMethodDeclaration(md, ctx); diff --git a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/BeanMethodReturnVoidTest.java b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/BeanMethodReturnVoidTest.java index 3992e91b2..55c233a37 100644 --- a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/BeanMethodReturnVoidTest.java +++ b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/BeanMethodReturnVoidTest.java @@ -36,25 +36,25 @@ void transformReturnType() { rewriteRun( java( """ - import org.springframework.context.annotation.Bean; + import org.springframework.context.annotation.Bean; - public class Test { + public class Test { - @Bean - public void myBean() { - } - } - """, """ - import org.springframework.context.annotation.Bean; + @Bean + public void myBean() { + } + } + """, """ + import org.springframework.context.annotation.Bean; - public class Test { + public class Test { - @Bean - public Object myBean() { - return null; - } - } - """) + @Bean + public Object myBean() { + return null; + } + } + """) ); } @@ -64,44 +64,43 @@ void transformAllReturn() { rewriteRun( java( """ - import org.springframework.context.annotation.Bean; + import org.springframework.context.annotation.Bean; - public class Test { + public class Test { - @Bean - public void myBean() { - if (true) { - return; - } - int i = 1; - return; - } + @Bean + public void bar() { + if (true) { + return; + } + int i = 1; + } - @Bean - public Object foo() { - return "foo"; - } - } - """, """ - import org.springframework.context.annotation.Bean; + @Bean + public Object foo() { + return "foo"; + } + } + """, """ + import org.springframework.context.annotation.Bean; - public class Test { + public class Test { - @Bean - public Object myBean() { - if (true) { - return null; - } - int i = 1; - return null; - } + @Bean + public Object bar() { + if (true) { + return null; + } + int i = 1; + return null; + } - @Bean - public Object foo() { - return "foo"; - } - } - """) + @Bean + public Object foo() { + return "foo"; + } + } + """) ); } }