Skip to content

Commit 5f5b5b7

Browse files
committed
Retain prefix before type expression in MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode
1 parent 471d2f8 commit 5f5b5b7

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

src/main/java/org/openrewrite/java/spring/framework/MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openrewrite.java.tree.*;
2727

2828
import static java.util.Collections.singletonList;
29+
import static java.util.Objects.requireNonNull;
2930

3031
public class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode extends Recipe {
3132

@@ -78,7 +79,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
7879
.withName(newName)
7980
.withVariableType(declaredVar.getVariableType().withOwner(met));
8081
return v.withVariables(singletonList(declaredVar.withType(javaTypeHttpStatusCode)))
81-
.withTypeExpression(TypeTree.build("HttpStatusCode").withType(javaTypeHttpStatusCode));
82+
.withTypeExpression(TypeTree.build("HttpStatusCode")
83+
.withType(javaTypeHttpStatusCode)
84+
.withPrefix(requireNonNull(v.getTypeExpression()).getPrefix()));
8285
}
8386
}
8487
return var;

src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/framework/MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCodeTest.java

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCodeTest implem
2828
@Override
2929
public void defaults(RecipeSpec spec) {
3030
spec.recipe(new MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode())
31-
.parser(JavaParser.fromJavaVersion().classpath("spring-web", "spring-webmvc"));
31+
.parser(JavaParser.fromJavaVersion().classpath("spring-web", "spring-webmvc", "jspecify"));
3232
}
3333

3434
@DocumentExample
@@ -43,9 +43,9 @@ void migrateHttpStatustoHttpStatusCode() {
4343
import org.springframework.http.ResponseEntity;
4444
import org.springframework.web.context.request.WebRequest;
4545
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
46-
46+
4747
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
48-
48+
4949
@Override
5050
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
5151
// Imagine we log or manipulate the status here somehow
@@ -59,9 +59,9 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
5959
import org.springframework.http.ResponseEntity;
6060
import org.springframework.web.context.request.WebRequest;
6161
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
62-
62+
6363
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
64-
64+
6565
@Override
6666
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
6767
// Imagine we log or manipulate the status here somehow
@@ -73,6 +73,51 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
7373
);
7474
}
7575

76+
@Test
77+
void migrateHttpStatustoHttpStatusCodeWithAnnotatedArguments() {
78+
//language=java
79+
rewriteRun(
80+
java(
81+
"""
82+
import org.jspecify.annotations.Nullable;
83+
import org.springframework.http.HttpHeaders;
84+
import org.springframework.http.HttpStatus;
85+
import org.springframework.http.ResponseEntity;
86+
import org.springframework.web.context.request.WebRequest;
87+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
88+
89+
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
90+
91+
@Override
92+
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
93+
@Nullable HttpStatus status, WebRequest request) {
94+
// Imagine we log or manipulate the status here somehow
95+
return super.handleExceptionInternal(ex, body, headers, status, request);
96+
}
97+
}
98+
""",
99+
"""
100+
import org.jspecify.annotations.Nullable;
101+
import org.springframework.http.HttpHeaders;
102+
import org.springframework.http.HttpStatusCode;
103+
import org.springframework.http.ResponseEntity;
104+
import org.springframework.web.context.request.WebRequest;
105+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
106+
107+
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
108+
109+
@Override
110+
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
111+
@Nullable HttpStatusCode status, WebRequest request) {
112+
// Imagine we log or manipulate the status here somehow
113+
return super.handleExceptionInternal(ex, body, headers, status, request);
114+
}
115+
}
116+
"""
117+
)
118+
);
119+
}
120+
76121
@Test
77122
void changeTypeOfValueCallAndRemoveImport() {
78123
//language=java
@@ -84,9 +129,9 @@ void changeTypeOfValueCallAndRemoveImport() {
84129
import org.springframework.http.ResponseEntity;
85130
import org.springframework.web.context.request.WebRequest;
86131
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
87-
132+
88133
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
89-
134+
90135
@Override
91136
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
92137
int value = status.value();
@@ -100,9 +145,9 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
100145
import org.springframework.http.ResponseEntity;
101146
import org.springframework.web.context.request.WebRequest;
102147
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
103-
148+
104149
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
105-
150+
106151
@Override
107152
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
108153
int value = status.value();
@@ -125,9 +170,9 @@ void shouldNotChangeEnumUsageAndImport() {
125170
import org.springframework.http.ResponseEntity;
126171
import org.springframework.web.context.request.WebRequest;
127172
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
128-
173+
129174
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
130-
175+
131176
@Override
132177
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
133178
HttpStatus enumValue = HttpStatus.INTERNAL_SERVER_ERROR;
@@ -142,9 +187,9 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
142187
import org.springframework.http.ResponseEntity;
143188
import org.springframework.web.context.request.WebRequest;
144189
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
145-
190+
146191
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
147-
192+
148193
@Override
149194
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
150195
HttpStatus enumValue = HttpStatus.INTERNAL_SERVER_ERROR;
@@ -167,9 +212,9 @@ void noSuperCallChangeMethodSignature() {
167212
import org.springframework.http.ResponseEntity;
168213
import org.springframework.web.context.request.WebRequest;
169214
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
170-
215+
171216
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
172-
217+
173218
@Override
174219
protected ResponseEntity<Object> handleExceptionInternal(
175220
Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
@@ -183,9 +228,9 @@ protected ResponseEntity<Object> handleExceptionInternal(
183228
import org.springframework.http.ResponseEntity;
184229
import org.springframework.web.context.request.WebRequest;
185230
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
186-
231+
187232
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
188-
233+
189234
@Override
190235
protected ResponseEntity<Object> handleExceptionInternal(
191236
Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {

0 commit comments

Comments
 (0)