Skip to content

Commit

Permalink
Use MethodMatcher and check arguments; expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 11, 2024
1 parent 3a69ac2 commit 753bb51
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeTree;
Expand All @@ -44,21 +45,19 @@ 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(
new UsesType<>("org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler", true),
new UsesMethod<>(HANDLER_METHOD),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
boolean isOverride = false;
for (J.Annotation ann : method.getLeadingAnnotations()) {
if (TypeUtils.isAssignableTo("java.lang.Override", ann.getType())) {
isOverride = true;
}
}
if (isOverride) {
if (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);
Expand Down Expand Up @@ -88,6 +87,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
maybeRemoveImport(HTTP_STATUS_FQ);
return m;
}

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 @@ -43,9 +43,9 @@ void migrateHttpStatustoHttpStatusCode() {
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
return super.handleExceptionInternal(ex, body, headers, status, request);
Expand All @@ -58,11 +58,93 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
"""
)
);
}

@Test
void convertHttpUsageAsInt() {
//language=java
rewriteRun(
java(
"""
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
int value = status.value();
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
""",
"""
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
int value = status.value();
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
"""
)
);
}

@Test
void convertHttpUsageAsEnum() {
//language=java
rewriteRun(
java(
"""
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
HttpStatus enumValue = status.value();
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
""",
"""
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
HttpStatus enumValue = HttpStatus.valueOf(status.value());
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
Expand Down

0 comments on commit 753bb51

Please sign in to comment.