-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recipe to migrate
ResponseStatusException
changes (#598)
* 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
1 parent
14a48ce
commit 6e703ee
Showing
3 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...enrewrite/java/spring/framework/MigrateResponseStatusExceptionGetRawStatusCodeMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/org/openrewrite/java/spring/framework/MigrateResponseStatusExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
} |