Skip to content

Commit 56aac6e

Browse files
authored
Bugfix/852 static import family (#853)
* Add failing test * Replace the enum constants of Family using ReplaceConstantWithAnotherConstant, simplify test
1 parent c67936c commit 56aac6e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

applications/spring-shell/src/test/java/org/springframework/sbm/recipes/MigrateJaxRsAnnotationsRecipeIntegrationTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ protected String getTestSubDir() {
3535
private final String expectedJavaSource =
3636
"""
3737
package com.example.jee.app;
38-
38+
39+
import org.springframework.http.HttpStatus;
40+
import org.springframework.http.HttpStatus.Series;
3941
import org.springframework.http.MediaType;
4042
import org.springframework.web.bind.annotation.*;
41-
43+
4244
import org.springframework.web.bind.annotation.PathVariable;
4345
import org.springframework.web.bind.annotation.RequestParam;
4446
@@ -63,6 +65,10 @@ public String getHelloWorldXML(@PathVariable("name") String name) throws Excepti
6365
System.out.println("name: " + name);
6466
return "<xml>Hello "+name+"</xml>";
6567
}
68+
69+
private boolean isResponseStatusSuccessful(HttpStatus.Series family) {
70+
return family == Series.SUCCESSFUL;
71+
}
6672
6773
}
6874
""";

applications/spring-shell/src/test/resources/testcode/bootify-jaxrs/src/main/java/com/example/jee/app/PersonController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import javax.ws.rs.*;
44
import javax.ws.rs.core.MediaType;
5+
import javax.ws.rs.core.Response;
56
import java.util.stream.Collectors;
67

78
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
9+
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
810

911
@Path("/")
1012
public class PersonController {
@@ -36,4 +38,8 @@ public String getHelloWorldXML(@PathParam("name") String name) throws Exception
3638
return "<xml>Hello "+name+"</xml>";
3739
}
3840

41+
private boolean isResponseStatusSuccessful(Response.Status.Family family) {
42+
return family == SUCCESSFUL;
43+
}
44+
3945
}

components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/recipes/SwapFamilyForSeries.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@
1919
import org.openrewrite.java.ChangeType;
2020
import org.openrewrite.java.JavaTemplate;
2121
import org.springframework.sbm.java.migration.recipes.RewriteMethodInvocation;
22+
import org.springframework.sbm.java.migration.recipes.openrewrite.ReplaceConstantWithAnotherConstant;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
2226

2327
public class SwapFamilyForSeries extends Recipe {
2428

2529
public SwapFamilyForSeries() {
30+
Map<String, String> fieldsMapping = new HashMap<>();
31+
fieldsMapping.put("INFORMATIONAL", "INFORMATIONAL");
32+
fieldsMapping.put("SUCCESSFUL", "SUCCESSFUL");
33+
fieldsMapping.put("REDIRECTION", "REDIRECTION");
34+
fieldsMapping.put("CLIENT_ERROR", "CLIENT_ERROR");
35+
fieldsMapping.put("SERVER_ERROR", "SERVER_ERROR");
36+
fieldsMapping.forEach(
37+
(key, value) -> doNext(new ReplaceConstantWithAnotherConstant("javax.ws.rs.core.Response.Status.Family." + key,"org.springframework.http.HttpStatus.Series." + value))
38+
);
2639

2740
// All constants seem to match on both types - let ChangeType take care of type changing for field accesses
2841
doNext(new RewriteMethodInvocation(

0 commit comments

Comments
 (0)