Skip to content

Commit

Permalink
Retain prefix before type expression in MigrateResponseEntityExceptio…
Browse files Browse the repository at this point in the history
…nHandlerHttpStatusToHttpStatusCode
  • Loading branch information
timtebeek committed Jan 6, 2025
1 parent 471d2f8 commit 5f5b5b7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.java.tree.*;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

public class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode extends Recipe {

Expand Down Expand Up @@ -78,7 +79,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
.withName(newName)
.withVariableType(declaredVar.getVariableType().withOwner(met));
return v.withVariables(singletonList(declaredVar.withType(javaTypeHttpStatusCode)))
.withTypeExpression(TypeTree.build("HttpStatusCode").withType(javaTypeHttpStatusCode));
.withTypeExpression(TypeTree.build("HttpStatusCode")
.withType(javaTypeHttpStatusCode)
.withPrefix(requireNonNull(v.getTypeExpression()).getPrefix()));
}
}
return var;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCodeTest implem
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode())
.parser(JavaParser.fromJavaVersion().classpath("spring-web", "spring-webmvc"));
.parser(JavaParser.fromJavaVersion().classpath("spring-web", "spring-webmvc", "jspecify"));
}

@DocumentExample
Expand All @@ -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) {
// Imagine we log or manipulate the status here somehow
Expand All @@ -59,9 +59,9 @@ 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) {
// Imagine we log or manipulate the status here somehow
Expand All @@ -73,6 +73,51 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
);
}

@Test
void migrateHttpStatustoHttpStatusCodeWithAnnotatedArguments() {
//language=java
rewriteRun(
java(
"""
import org.jspecify.annotations.Nullable;
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,
@Nullable HttpStatus status, WebRequest request) {
// Imagine we log or manipulate the status here somehow
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
""",
"""
import org.jspecify.annotations.Nullable;
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,
@Nullable HttpStatusCode status, WebRequest request) {
// Imagine we log or manipulate the status here somehow
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
"""
)
);
}

@Test
void changeTypeOfValueCallAndRemoveImport() {
//language=java
Expand All @@ -84,9 +129,9 @@ void changeTypeOfValueCallAndRemoveImport() {
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();
Expand All @@ -100,9 +145,9 @@ 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) {
int value = status.value();
Expand All @@ -125,9 +170,9 @@ void shouldNotChangeEnumUsageAndImport() {
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 = HttpStatus.INTERNAL_SERVER_ERROR;
Expand All @@ -142,9 +187,9 @@ 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) {
HttpStatus enumValue = HttpStatus.INTERNAL_SERVER_ERROR;
Expand All @@ -167,9 +212,9 @@ void noSuperCallChangeMethodSignature() {
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) {
Expand All @@ -183,9 +228,9 @@ protected ResponseEntity<Object> handleExceptionInternal(
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) {
Expand Down

0 comments on commit 5f5b5b7

Please sign in to comment.