Skip to content

Commit

Permalink
Improve MigrateHandlerInterceptor with superclass case
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 8, 2024
1 parent 9498250 commit 409ea62
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.RemoveImport;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

Expand All @@ -30,6 +29,9 @@

public class MigrateHandlerInterceptor extends Recipe {

public static final String HANDLER_INTERCEPTOR_ADAPTER = "org.springframework.web.servlet.handler.HandlerInterceptorAdapter";
public static final String HANDLER_INTERCEPTOR = "org.springframework.web.servlet.HandlerInterceptor";

@Override
public String getDisplayName() {
return "Migrate `HandlerInterceptorAdapter` to `HandlerInterceptor`";
Expand All @@ -40,46 +42,45 @@ public String getDescription() {
return "Deprecated as of 5.3 in favor of implementing `HandlerInterceptor` and/or `AsyncHandlerInterceptor`.";
}

// It will therefore be necessary to use the interfaces of these classes, respectively org.springframework.web.servlet.HandlerInterceptor and org.springframework.web.servlet.config.annotation.WebMvcConfigurer.
//In order to replace these Adapter classes with interfaces you would have to:
//- Edit import
//- Replace extends <className> by implements <interfaceName>
//- Replace super.<MethodName> calls by <InterfaceName>.super.<MethodName>

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.springframework.web.servlet.handler.HandlerInterceptorAdapter", false), new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesType<>(HANDLER_INTERCEPTOR_ADAPTER, false), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (classDecl.getExtends() != null && TypeUtils.isOfClassType(classDecl.getExtends().getType(),
"org.springframework.web.servlet.handler.HandlerInterceptorAdapter")) {
cd = cd.withExtends(null);
cd = cd.withImplements(singletonList(TypeTree.build("HandlerInterceptor")
.withType(JavaType.buildType("org.springframework.web.servlet.HandlerInterceptor")))
);
if (cd.getExtends() == null || !TypeUtils.isOfClassType(cd.getExtends().getType(), HANDLER_INTERCEPTOR_ADAPTER)) {
return cd;
}

// temporary
cd = cd.getPadding().withImplements(JContainer.withElements(requireNonNull(cd.getPadding().getImplements()),
ListUtils.mapFirst(cd.getPadding().getImplements().getElements(),
anImplements -> anImplements.withPrefix(Space.format(" ")))));
maybeAddImport(HANDLER_INTERCEPTOR);
maybeRemoveImport(HANDLER_INTERCEPTOR_ADAPTER);

maybeAddImport("org.springframework.web.servlet.HandlerInterceptor");
doAfterVisit(new RemoveImport<>("org.springframework.web.servlet.handler.HandlerInterceptorAdapter", true));
return autoFormat(cd, requireNonNull(cd.getImplements()).get(0), ctx, getCursor().getParentOrThrow());
}
return cd;
cd = cd.withExtends(null);
cd = cd.withImplements(singletonList(TypeTree.build("HandlerInterceptor")
.withType(JavaType.buildType(HANDLER_INTERCEPTOR)))
);

// temporary
cd = cd.getPadding().withImplements(JContainer.withElements(requireNonNull(cd.getPadding().getImplements()),
ListUtils.mapFirst(cd.getPadding().getImplements().getElements(),
anImplements -> anImplements.withPrefix(Space.format(" ")))));

return autoFormat(cd, requireNonNull(cd.getImplements()).get(0), ctx, getCursor().getParentOrThrow());
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (method.getSelect() instanceof J.Identifier) {
if ("super".equals(((J.Identifier) method.getSelect()).getSimpleName())) {
return method.withSelect(TypeTree.build("HandlerInterceptor.super")
.withType(JavaType.buildType("org.springframework.web.servlet.HandlerInterceptor")));
}
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (mi.getMethodType() != null &&
TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), HANDLER_INTERCEPTOR) &&
mi.getSelect() instanceof J.Identifier &&
"super".equals(((J.Identifier) mi.getSelect()).getSimpleName())
) {
return mi.withSelect(TypeTree.build("HandlerInterceptor.super")
.withType(JavaType.buildType(HANDLER_INTERCEPTOR)));

}
return super.visitMethodInvocation(method, ctx);
return mi;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,35 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
)
);
}

@Test
void unusedImportOfHandlerInterceptorAdapterAndHasASuperCallShouldDoNothing() {
//language=java
rewriteRun(
java(
"""
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
class MyInterceptorLike extends MySuperInterceptor {
@Override
public boolean test() {
return super.test();
}
@Override
public boolean test2() {
return super.test();
}
}
class MySuperInterceptor {
public boolean test() {
return true;
}
public boolean test2() {
return true;
}
}"""
)
);
}
}

0 comments on commit 409ea62

Please sign in to comment.