Skip to content

Commit

Permalink
Polish visitMethodDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 19, 2024
1 parent 98f2821 commit cff5324
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode ext
private static final String HTTP_STATUS_FQ = "org.springframework.http.HttpStatus";
private static final String HTTP_STATUS_CODE_FQ = "org.springframework.http.HttpStatusCode";
private static final String RESPONSE_ENTITY_EXCEPTION_HANDLER_FQ = "org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler";
private static final MethodMatcher HANDLER_METHOD = new MethodMatcher(
"org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler *(..)", true);

@Override
public String getDisplayName() {
Expand All @@ -45,10 +47,6 @@ public String getDescription() {
return "With Spring 6 `HttpStatus` was replaced by `HttpStatusCode` in most method signatures in the `ResponseEntityExceptionHandler`.";
}


private static final MethodMatcher HANDLER_METHOD = new MethodMatcher(
"org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler *(..)", true);

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Expand All @@ -58,43 +56,31 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = method;
if (m.getMethodType() != null && HANDLER_METHOD.matches(m.getMethodType()) && hasHttpStatusParameter(m)) {
final JavaType.Method met = m.getMethodType().withParameterTypes(ListUtils.map(m.getMethodType().getParameterTypes(), type -> {
if (TypeUtils.isAssignableTo(HTTP_STATUS_FQ, type)) {
return JavaType.buildType(HTTP_STATUS_CODE_FQ);
}
return type;
}));
if (HANDLER_METHOD.matches(m.getMethodType())) {
JavaType javaTypeHttpStatusCode = JavaType.buildType(HTTP_STATUS_CODE_FQ);
//noinspection DataFlowIssue
JavaType.Method met = m.getMethodType().withParameterTypes(ListUtils.map(m.getMethodType().getParameterTypes(),
type -> TypeUtils.isAssignableTo(HTTP_STATUS_FQ, type) ? javaTypeHttpStatusCode : type));
if (met == m.getMethodType()) {
// There was no parameter to change
return m;
}

m = m.withMethodType(met);
m = m.withParameters(ListUtils.map(m.getParameters(), var -> {
if (var instanceof J.VariableDeclarations) {
J.VariableDeclarations v = (J.VariableDeclarations) var;
J.VariableDeclarations.NamedVariable declaredVar = v.getVariables().get(0);
if (declaredVar.getVariableType() != null) {
declaredVar = declaredVar
.withVariableType(declaredVar
.getVariableType()
.withOwner(met))
.withName(declaredVar
.getName()
.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ)));
if (declaredVar.getName().getFieldType() != null) {
declaredVar = declaredVar.withName(declaredVar.getName()
.withFieldType(declaredVar
.getName()
.getFieldType()
.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ)))
);
}
v = v.withVariables(singletonList(declaredVar));
if (TypeUtils.isOfType(v.getType(), JavaType.buildType(HTTP_STATUS_FQ))) {
String httpStatusCodeSimpleName = HTTP_STATUS_CODE_FQ.substring(HTTP_STATUS_CODE_FQ.lastIndexOf("."));
v = v.withTypeExpression(TypeTree.build(httpStatusCodeSimpleName)
.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ)));
v = v.withVariables(singletonList(declaredVar.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ))));
return v;
if (declaredVar.getVariableType() != null && TypeUtils.isAssignableTo(HTTP_STATUS_FQ, v.getType())) {
J.Identifier newName = declaredVar.getName().withType(javaTypeHttpStatusCode);
if (newName.getFieldType() != null) {
newName = newName.withFieldType(newName.getFieldType().withType(javaTypeHttpStatusCode));
}
declaredVar = declaredVar
.withName(newName)
.withVariableType(declaredVar.getVariableType().withOwner(met));
return v.withVariables(singletonList(declaredVar.withType(javaTypeHttpStatusCode)))
.withTypeExpression(TypeTree.build("HttpStatusCode").withType(javaTypeHttpStatusCode));
}
}
return var;
Expand Down Expand Up @@ -159,12 +145,6 @@ public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
}
return super.visitReturn(_return, ctx);
}

private boolean hasHttpStatusParameter(J.MethodDeclaration m) {
return m.getParameters().stream().anyMatch(p ->
p instanceof J.VariableDeclarations &&
TypeUtils.isAssignableTo(HTTP_STATUS_FQ, ((J.VariableDeclarations) p).getType()));
}
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.List;

import static org.openrewrite.java.Assertions.java;

class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCodeTest implements RewriteTest {
Expand Down Expand Up @@ -48,6 +52,7 @@ class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
// Imagine we log or manipulate the status here somehow
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
Expand All @@ -63,10 +68,16 @@ class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
// Imagine we log or manipulate the status here somehow
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
"""
""",
spec -> spec.afterRecipe(cu -> {

J.MethodDeclaration md = (J.MethodDeclaration) cu.getClasses().get(0).getBody().getStatements().get(0);
List<Statement> parameters = md.getParameters();
})
)
);
}
Expand Down Expand Up @@ -169,7 +180,8 @@ void noSuperCallChangeMethodSignature() {
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
return ResponseEntity.ok().build();
}
}
Expand All @@ -184,7 +196,8 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
return ResponseEntity.ok().build();
}
}
Expand Down

0 comments on commit cff5324

Please sign in to comment.