Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HttpStatus to HttpStatusCode in ResponseEntityExceptionHandler implementations #589

Merged
merged 21 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ dependencies {
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.boot:spring-boot-starter-test:3.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework:spring-context:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework:spring-web:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework:spring-webmvc:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.batch:spring-batch-core:4.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.batch:spring-batch-core:5.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.batch:spring-batch-infrastructure:4.+")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.framework;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

import static java.util.Collections.singletonList;

public class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode extends Recipe {

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() {
return "Migrate `ResponseEntityExceptionHandler` from HttpStatus to HttpStatusCode";
}

@Override
public String getDescription() {
return "With Spring 6 `HttpStatus` was replaced by `HttpStatusCode` in most method signatures in the `ResponseEntityExceptionHandler`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesType<>(RESPONSE_ENTITY_EXCEPTION_HANDLER_FQ, true),
new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = method;
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 && 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;
}));
}
updateCursor(m);
maybeAddImport(HTTP_STATUS_CODE_FQ);
maybeRemoveImport(HTTP_STATUS_FQ);
return super.visitMethodDeclaration(m, ctx);
}

@Override
public J.Identifier visitIdentifier(J.Identifier identifier, ExecutionContext ctx) {
J.Identifier ident = super.visitIdentifier(identifier, ctx);
J.MethodDeclaration methodScope = getCursor().firstEnclosing(J.MethodDeclaration.class);
if (methodScope != null) {
for (Statement stmt : methodScope.getParameters()) {
if (stmt instanceof J.VariableDeclarations) {
J.VariableDeclarations vd = (J.VariableDeclarations) stmt;
for (J.VariableDeclarations.NamedVariable var : vd.getVariables()) {
if (var.getName().getSimpleName().equals(ident.getSimpleName())) {
if (!TypeUtils.isOfType(var.getName().getType(), ident.getType())) {
ident = ident.withType(var.getName().getType());
}
}
}
}
}
}
return super.visitIdentifier(ident, ctx);
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ recipeList:
- org.openrewrite.java.spring.framework.MigrateSpringAssert
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5
- org.openrewrite.java.spring.framework.HttpComponentsClientHttpRequestFactoryReadTimeout
- org.openrewrite.java.spring.framework.MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.framework;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCodeTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode())
.parser(JavaParser.fromJavaVersion().classpath("spring-web", "spring-webmvc"));
}

@DocumentExample
@Test
void migrateHttpStatustoHttpStatusCode() {
//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) {
// Imagine we log or manipulate the status here somehow
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) {
// Imagine we log or manipulate the status here somehow
return super.handleExceptionInternal(ex, body, headers, status, request);
}
}
"""
)
);
}

@Test
void changeTypeOfValueCallAndRemoveImport() {
//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 shouldNotChangeEnumUsageAndImport() {
//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 = HttpStatus.INTERNAL_SERVER_ERROR;
return super.handleExceptionInternal(ex, body, headers, enumValue, request);
}
}
""",
"""
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
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.INTERNAL_SERVER_ERROR;
return super.handleExceptionInternal(ex, body, headers, enumValue, request);
}
}
"""
)
);
}

@Test
void noSuperCallChangeMethodSignature() {
//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) {
return ResponseEntity.ok().build();
}
}
""",
"""
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) {
return ResponseEntity.ok().build();
}
}
"""
)
);
}

}
Loading