Skip to content

Commit

Permalink
Add recipe to migrate ResponseStatusException changes (#598)
Browse files Browse the repository at this point in the history
* Add recipe to migrate `ResponseStatusException` changes

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Add MigrateResponseStatusException in UpgradeSpringFramework_6_0

* Remove contextSensitive

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Oct 6, 2024
1 parent 14a48ce commit 6e703ee
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.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<?, ExecutionContext> 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()")
.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;
});
}
}
12 changes: 11 additions & 1 deletion src/main/resources/META-INF/rewrite/spring-framework-60.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.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();
}
}
"""
)
);
}

}

0 comments on commit 6e703ee

Please sign in to comment.