Skip to content

Commit

Permalink
Add new migration case of WebMvcConfigurer (#616)
Browse files Browse the repository at this point in the history
* Add new migration case of WebMvcConfigurer

* Minor polish to reduce diff

* Take out the ParameterizedType handling for now

* Update src/main/java/org/openrewrite/java/spring/framework/MigrateWebMvcConfigurerAdapter.java

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

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 5, 2024
1 parent b6b1532 commit 132e79a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.openrewrite.java.tree.TypeUtils;

public class MigrateWebMvcConfigurerAdapter extends Recipe {
private static final String WEB_MVC_CONFIGURER_ADAPTER = "org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter";
private static final String WEB_MVC_CONFIGURER = "org.springframework.web.servlet.config.annotation.WebMvcConfigurer";

@Override
public String getDisplayName() {
return "Replace `WebMvcConfigurerAdapter` with `WebMvcConfigurer`";
Expand All @@ -43,11 +46,13 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter", false), new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesType<>(WEB_MVC_CONFIGURER_ADAPTER, false), new JavaIsoVisitor<ExecutionContext>() {
private final JavaType WEB_MVC_CONFIGURER_TYPE = JavaType.buildType(WEB_MVC_CONFIGURER);

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getExtends() != null && TypeUtils.isOfClassType(cd.getExtends().getType(), "org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter")) {
if (cd.getExtends() != null && TypeUtils.isOfClassType(cd.getExtends().getType(), WEB_MVC_CONFIGURER_ADAPTER)) {
cd = cd.withExtends(null);
updateCursor(cd);
// This is an interesting one... WebMvcConfigurerAdapter implements WebMvcConfigurer
Expand All @@ -59,18 +64,51 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
}
cd = JavaTemplate.builder("WebMvcConfigurer")
.contextSensitive()
.imports("org.springframework.web.servlet.config.annotation.WebMvcConfigurer")
.imports(WEB_MVC_CONFIGURER)
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "spring-webmvc-5.*"))
.classpathFromResources(ctx, "spring-webmvc-5"))
.build().apply(getCursor(), cd.getCoordinates().addImplementsClause());
updateCursor(cd);
cd = (J.ClassDeclaration) new RemoveSuperStatementVisitor().visitNonNull(cd, ctx, getCursor().getParentOrThrow());
maybeRemoveImport("org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter");
maybeAddImport("org.springframework.web.servlet.config.annotation.WebMvcConfigurer");
maybeRemoveImport(WEB_MVC_CONFIGURER_ADAPTER);
maybeAddImport(WEB_MVC_CONFIGURER);
}
return cd;
}

@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
if (newClass.getClazz() != null && TypeUtils.isOfClassType(newClass.getClazz().getType(), WEB_MVC_CONFIGURER_ADAPTER)) {
if (newClass.getClazz() instanceof J.Identifier) {
J.Identifier identifier = (J.Identifier) newClass.getClazz();
newClass = newClass.withClazz(identifier
.withType(WEB_MVC_CONFIGURER_TYPE)
.withSimpleName(((JavaType.ShallowClass) WEB_MVC_CONFIGURER_TYPE).getClassName())
);
}
maybeRemoveImport(WEB_MVC_CONFIGURER_ADAPTER);
maybeAddImport(WEB_MVC_CONFIGURER);
}
return super.visitNewClass(newClass, ctx);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx) {
if (md.getMethodType() != null && TypeUtils.isOfClassType(md.getType(), WEB_MVC_CONFIGURER_ADAPTER)) {
if (md.getReturnTypeExpression() instanceof J.Identifier) {
J.Identifier identifier = (J.Identifier) md.getReturnTypeExpression();
md = md.withReturnTypeExpression(identifier
.withType(WEB_MVC_CONFIGURER_TYPE)
.withSimpleName(((JavaType.ShallowClass) WEB_MVC_CONFIGURER_TYPE).getClassName())
);
}

maybeRemoveImport(WEB_MVC_CONFIGURER_ADAPTER);
maybeAddImport(WEB_MVC_CONFIGURER);
}
return super.visitMethodDeclaration(md, ctx);
}

class RemoveSuperStatementVisitor extends JavaIsoVisitor<ExecutionContext> {
final MethodMatcher wm = new MethodMatcher("org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter *(..)");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class MigrateWebMvcConfigurerAdapterTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "spring-webmvc-5.*", "spring-core-5.*", "spring-web-5.*"))
spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
"spring-webmvc-5", "spring-core-5", "spring-web-5"))
.recipe(new MigrateWebMvcConfigurerAdapter());
}

Expand All @@ -39,30 +39,55 @@ void transformSimple() {
rewriteRun(
//language=java
java(
"""
package a.b.c;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
public class CustomMvcConfigurer extends WebMvcConfigurerAdapter {
private final String someArg;
public CustomMvcConfigurer(String someArg) {
super();
this.someArg = someArg;
}
}
""", """
package a.b.c;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class CustomMvcConfigurer implements WebMvcConfigurer {
private final String someArg;
public CustomMvcConfigurer(String someArg) {
this.someArg = someArg;
}
}
""")
"""
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
public class CustomMvcConfigurer extends WebMvcConfigurerAdapter {
private final String someArg;
public CustomMvcConfigurer(String someArg) {
super();
this.someArg = someArg;
}
}
""", """
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class CustomMvcConfigurer implements WebMvcConfigurer {
private final String someArg;
public CustomMvcConfigurer(String someArg) {
this.someArg = someArg;
}
}
""")
);
}

@Test
void transformBean() {
// language=java
rewriteRun(
java(
"""
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
class WebConfig {
WebMvcConfigurerAdapter forwardToIndex() {
return new WebMvcConfigurerAdapter() {
};
}
}
""",
"""
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
class WebConfig {
WebMvcConfigurer forwardToIndex() {
return new WebMvcConfigurer() {
};
}
}
"""
)
);
}
}

0 comments on commit 132e79a

Please sign in to comment.