-
Notifications
You must be signed in to change notification settings - Fork 66
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
Spring Framework 6.0 - ResponseStatusException method changes #554
Labels
Comments
Thanks for logging the suggested improvement; I've had a quick look at the 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 MigrateResponseStatusException 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()")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web-6"))
.build().apply(mc.getCursor(), tree.getCoordinates().replace(), ((J.MethodInvocation) tree).getSelect());
}
return tree;
});
}
} 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.recipe(new MigrateResponseStatusException())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-web-5.3"));
}
@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-spring/issues/554")
@Test
void migrateResponseStatusException() {
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();
}
}
"""
)
);
}
} The |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What problem are you trying to solve?
In Spring Framework 5.3,
ResponseStatusException
had methodsgetRawStatusCode()
andgetStatus()
:https://docs.spring.io/spring-framework/docs/5.3.37/javadoc-api/org/springframework/web/server/ResponseStatusException.html
In Spring Framework 6.0, these are removed. The
getStatusCode()
method from superclassErrorResponseException
can be used as a direct replacement forgetStatus()
, andgetStatusCode().value()
can replacegetRawStatusCode()
:https://docs.spring.io/spring-framework/docs/6.0.0/javadoc-api/org/springframework/web/server/ResponseStatusException.html
What precondition(s) should be checked before applying this recipe?
None
Describe the situation before applying the recipe
Describe the situation after applying the recipe
Have you considered any alternatives or workarounds?
Any additional context
Are you interested in contributing this recipe to OpenRewrite?
Not urgent for me; probably a good first issue?
The text was updated successfully, but these errors were encountered: