Skip to content

Commit

Permalink
Bugfix/852 static import family (#853)
Browse files Browse the repository at this point in the history
* Add failing test

* Replace the enum constants of Family using ReplaceConstantWithAnotherConstant, simplify test
  • Loading branch information
bottemav authored Aug 5, 2023
1 parent c67936c commit 56aac6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ protected String getTestSubDir() {
private final String expectedJavaSource =
"""
package com.example.jee.app;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus.Series;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -63,6 +65,10 @@ public String getHelloWorldXML(@PathVariable("name") String name) throws Excepti
System.out.println("name: " + name);
return "<xml>Hello "+name+"</xml>";
}
private boolean isResponseStatusSuccessful(HttpStatus.Series family) {
return family == Series.SUCCESSFUL;
}
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.stream.Collectors;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;

@Path("/")
public class PersonController {
Expand Down Expand Up @@ -36,4 +38,8 @@ public String getHelloWorldXML(@PathParam("name") String name) throws Exception
return "<xml>Hello "+name+"</xml>";
}

private boolean isResponseStatusSuccessful(Response.Status.Family family) {
return family == SUCCESSFUL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@
import org.openrewrite.java.ChangeType;
import org.openrewrite.java.JavaTemplate;
import org.springframework.sbm.java.migration.recipes.RewriteMethodInvocation;
import org.springframework.sbm.java.migration.recipes.openrewrite.ReplaceConstantWithAnotherConstant;

import java.util.HashMap;
import java.util.Map;

public class SwapFamilyForSeries extends Recipe {

public SwapFamilyForSeries() {
Map<String, String> fieldsMapping = new HashMap<>();
fieldsMapping.put("INFORMATIONAL", "INFORMATIONAL");
fieldsMapping.put("SUCCESSFUL", "SUCCESSFUL");
fieldsMapping.put("REDIRECTION", "REDIRECTION");
fieldsMapping.put("CLIENT_ERROR", "CLIENT_ERROR");
fieldsMapping.put("SERVER_ERROR", "SERVER_ERROR");
fieldsMapping.forEach(
(key, value) -> doNext(new ReplaceConstantWithAnotherConstant("javax.ws.rs.core.Response.Status.Family." + key,"org.springframework.http.HttpStatus.Series." + value))
);

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

0 comments on commit 56aac6e

Please sign in to comment.