Skip to content

Commit

Permalink
Use method matcher instead of string comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 12, 2024
1 parent 39ac758 commit b90bdf4
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@
*/
package org.openrewrite.java.spring.framework;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.java.tree.TypeUtils;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

public class MigrateHandlerInterceptor extends Recipe {

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

private static final MethodMatcher PRE_HANDLE = new MethodMatcher("org.springframework.web.servlet.HandlerInterceptor preHandle(..)");
private static final MethodMatcher POST_HANDLE = new MethodMatcher("org.springframework.web.servlet.HandlerInterceptor postHandle(..)");
private static final MethodMatcher AFTER_COMPLETION = new MethodMatcher("org.springframework.web.servlet.HandlerInterceptor afterCompletion(..)");

@Override
public String getDisplayName() {
Expand All @@ -55,28 +60,27 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
return cd;
}

maybeAddImport(HANDLER_INTERCEPTOR);
maybeAddImport(HANDLER_INTERCEPTOR_INTERFACE);
maybeRemoveImport(HANDLER_INTERCEPTOR_ADAPTER);

cd = cd.withExtends(null)
.withImplements(singletonList(TypeTree.build("HandlerInterceptor")
.withType(JavaType.buildType(HANDLER_INTERCEPTOR))));

return autoFormat(cd, requireNonNull(cd.getImplements()).get(0), ctx, getCursor().getParentOrThrow());
TypeTree implments = TypeTree.build("HandlerInterceptor")
.withType(JavaType.buildType(HANDLER_INTERCEPTOR_INTERFACE));
cd = cd.withExtends(null).withImplements(singletonList(implments));
return autoFormat(cd, implments, ctx, getCursor().getParentOrThrow());
}

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public @Nullable J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (mi.getMethodType() != null &&
TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), HANDLER_INTERCEPTOR) &&
mi.getSelect() instanceof J.Identifier) {
if ("super".equals(((J.Identifier) mi.getSelect()).getSimpleName())) {
if ("preHandle".equals(mi.getSimpleName())) {
return JavaTemplate.builder("true").build().apply(getCursor(), mi.getCoordinates().replace());
} else if ("postHandle".equals(mi.getSimpleName()) || "afterCompletion".equals(mi.getSimpleName())) {
return null;
}
TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), HANDLER_INTERCEPTOR_INTERFACE) &&
mi.getSelect() instanceof J.Identifier && "super".equals(((J.Identifier) mi.getSelect()).getSimpleName())) {
if (PRE_HANDLE.matches(mi)) {
// No need to call super for the hardcoded `true` return value there
return JavaTemplate.apply("true", getCursor(), mi.getCoordinates().replace());
}
if (POST_HANDLE.matches(mi) || AFTER_COMPLETION.matches(mi)) {
return null; // No need to call super for empty methods there
}
}
return mi;
Expand Down

0 comments on commit b90bdf4

Please sign in to comment.