From 0fffa63f95dc7aa1631b91db89ebf8e198c8960a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20S=C5=82owiak?= Date: Thu, 3 Oct 2024 20:03:08 +0200 Subject: [PATCH 1/4] Add recipe to migrate `ResponseStatusException` changes --- ...StatusExceptionGetRawStatusCodeMethod.java | 37 +++++++++ .../META-INF/rewrite/spring-framework-60.yml | 11 ++- .../MigrateResponseStatusExceptionTest.java | 77 +++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java create mode 100644 src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java diff --git a/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java new file mode 100644 index 000000000..2a72b350b --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java @@ -0,0 +1,37 @@ +package org.openrewrite.java.spring.framework; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.trait.Traits; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.MethodCall; + +public class MigrateResponseStatusExceptionGetRawStatusCodeMethod extends Recipe { + @Override + public String getDisplayName() { + return "Migrate `ResponseStatusException#getRawStatusCode()` to `getStatusCode().value()`"; + } + + @Override + public String getDescription() { + return "Migrate Spring Framework 5.3's `ResponseStatusException` method `getRawStatusCode()` to Spring Framework 6's `getStatusCode().value()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Traits.methodAccess("org.springframework.web.server.ResponseStatusException getRawStatusCode()") + .asVisitor((mc, ctx) -> { + MethodCall tree = mc.getTree(); + if (tree instanceof J.MethodInvocation) { + return JavaTemplate.builder("#{any()}.getStatusCode().value()") + .contextSensitive() + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-core-6", "spring-beans-6", "spring-web-6")) + .build().apply(mc.getCursor(), tree.getCoordinates().replace(), ((J.MethodInvocation) tree).getSelect()); + } + return tree; + }); + } +} diff --git a/src/main/resources/META-INF/rewrite/spring-framework-60.yml b/src/main/resources/META-INF/rewrite/spring-framework-60.yml index cded8b6bf..0450f6727 100644 --- a/src/main/resources/META-INF/rewrite/spring-framework-60.yml +++ b/src/main/resources/META-INF/rewrite/spring-framework-60.yml @@ -31,7 +31,16 @@ recipeList: - org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5 - org.openrewrite.java.spring.framework.HttpComponentsClientHttpRequestFactoryReadTimeout - org.openrewrite.java.spring.framework.MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode - +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.spring.framework.MigrateResponseStatusException +displayName: Migrate breaking changes in `ResponseStatusException` +description: Migrate Spring Framework 5.3's `ResponseStatusException` method `getRawStatusCode()` to Spring Framework 6's `getStatusCode().value()` and `ResponseStatusException` method `getStatus()` to Spring Framework 6's `getStatusCode()` . +recipeList: + - org.openrewrite.java.spring.framework.MigrateResponseStatusExceptionGetRawStatusCodeMethod + - org.openrewrite.java.ChangeMethodName: + methodPattern: "org.springframework.web.server.ResponseStatusException getStatus()" + newMethodName: "getStatusCode" --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.spring.framework.MigrateSpringAssert diff --git a/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java new file mode 100644 index 000000000..c4fbdb483 --- /dev/null +++ b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java @@ -0,0 +1,77 @@ +package org.openrewrite.java.spring.framework; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class MigrateResponseStatusExceptionTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResources("org.openrewrite.java.spring.framework.MigrateResponseStatusException") + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-core-5.3", "spring-beans-5.3", "spring-web-5.3")); + } + + @DocumentExample + @Issue("https://github.com/openrewrite/rewrite-spring/issues/554") + @Test + void migrateResponseStatusExceptionGetStatusMethod() { + rewriteRun( + //language=java + java( + """ + import org.springframework.http.HttpStatus; + import org.springframework.web.server.ResponseStatusException; + class A { + void foo(ResponseStatusException e) { + HttpStatus i = e.getStatus(); + } + } + """, + """ + import org.springframework.http.HttpStatus; + import org.springframework.web.server.ResponseStatusException; + class A { + void foo(ResponseStatusException e) { + HttpStatus i = e.getStatusCode(); + } + } + """ + ) + ); + } + + @DocumentExample + @Issue("https://github.com/openrewrite/rewrite-spring/issues/554") + @Test + void migrateResponseStatusExceptionGetRawStatusCodeMethod() { + rewriteRun( + //language=java + java( + """ + import org.springframework.web.server.ResponseStatusException; + class A { + void foo(ResponseStatusException e) { + int i = e.getRawStatusCode(); + } + } + """, + """ + import org.springframework.web.server.ResponseStatusException; + class A { + void foo(ResponseStatusException e) { + int i = e.getStatusCode().value(); + } + } + """ + ) + ); + } + +} From 0c9b2577dce88d58a679ede625104f6ff0d193b4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 5 Oct 2024 23:44:57 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- ...onseStatusExceptionGetRawStatusCodeMethod.java | 15 +++++++++++++++ .../MigrateResponseStatusExceptionTest.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java index 2a72b350b..ded9f7bc0 100644 --- a/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java +++ b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; diff --git a/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java index c4fbdb483..55f9fd520 100644 --- a/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java +++ b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; From 582a3eabd5b342362e0d4105e1a47b20d633aa0f Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 01:54:41 +0200 Subject: [PATCH 3/4] Add MigrateResponseStatusException in UpgradeSpringFramework_6_0 --- src/main/resources/META-INF/rewrite/spring-framework-60.yml | 1 + .../spring/framework/MigrateResponseStatusExceptionTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/rewrite/spring-framework-60.yml b/src/main/resources/META-INF/rewrite/spring-framework-60.yml index 0450f6727..d861e2ec9 100644 --- a/src/main/resources/META-INF/rewrite/spring-framework-60.yml +++ b/src/main/resources/META-INF/rewrite/spring-framework-60.yml @@ -31,6 +31,7 @@ recipeList: - org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5 - org.openrewrite.java.spring.framework.HttpComponentsClientHttpRequestFactoryReadTimeout - org.openrewrite.java.spring.framework.MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode + - org.openrewrite.java.spring.framework.MigrateResponseStatusException --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.spring.framework.MigrateResponseStatusException diff --git a/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java index 55f9fd520..bb400b9b7 100644 --- a/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java +++ b/src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java @@ -30,7 +30,7 @@ class MigrateResponseStatusExceptionTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipeFromResources("org.openrewrite.java.spring.framework.MigrateResponseStatusException") - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-core-5.3", "spring-beans-5.3", "spring-web-5.3")); + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-core-5.3", "spring-beans-5.3", "spring-web-5.3")); } @DocumentExample From c999306772707c1e8b61f4c5b348b3ab73df994c Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 10:28:49 +0200 Subject: [PATCH 4/4] Remove contextSensitive --- .../MigrateResponseStatusExceptionGetRawStatusCodeMethod.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java index ded9f7bc0..800ae1743 100644 --- a/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java +++ b/src/main/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java @@ -42,7 +42,6 @@ public TreeVisitor getVisitor() { MethodCall tree = mc.getTree(); if (tree instanceof J.MethodInvocation) { return JavaTemplate.builder("#{any()}.getStatusCode().value()") - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-core-6", "spring-beans-6", "spring-web-6")) .build().apply(mc.getCursor(), tree.getCoordinates().replace(), ((J.MethodInvocation) tree).getSelect()); }