Skip to content

Commit

Permalink
Add BeanMethodReturnNull recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
SiBorea committed Nov 6, 2024
1 parent 28b79a8 commit 4056d3f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Statement> statements = md.getBody().getStatements();
if (statements.isEmpty() || !(statements.get(statements.size() - 1) instanceof J.Return)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
""")
);
}

Expand All @@ -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";
}
}
""")
);
}
}

0 comments on commit 4056d3f

Please sign in to comment.