Skip to content

Commit

Permalink
A little bit closer
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Sep 17, 2024
1 parent b239101 commit f104ad7
Showing 1 changed file with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.TreeVisitingPrinter;
import org.openrewrite.java.search.UsesMethod;
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 org.openrewrite.java.tree.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Collections.singletonList;

public class MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode extends Recipe {

private static final String HTTP_STATUS_FQ = "org.springframework.http.HttpStatus";
private static final String HTTP_STATUS_CODE_FQ = "org.springframework.http.HttpStatusCode";
private static final String RESPONSE_ENTITY_EXCEPTION_HANDLER_FQ = "org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler";

@Override
public String getDisplayName() {
Expand All @@ -49,21 +53,34 @@ public String getDescription() {
private static final MethodMatcher HANDLER_METHOD = new MethodMatcher(
"org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler *(..)", true);

private static final MethodMatcher HTTP_STATUS_VALUE_OF = new MethodMatcher(
HTTP_STATUS_FQ + " valueOf(java.lang.int)", true);

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(HANDLER_METHOD),
new JavaIsoVisitor<ExecutionContext>() {

// TODO remove debug
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
System.out.println(TreeVisitingPrinter.printTree(cd));
return cd;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
J.MethodDeclaration m = method;
if (HANDLER_METHOD.matches(m.getMethodType()) && hasHttpStatusParameter(m)) {
final JavaType.Method met = m.getMethodType().withParameterTypes(ListUtils.map(m.getMethodType().getParameterTypes(), type -> {
if (TypeUtils.isAssignableTo(HTTP_STATUS_FQ, type)) {
return JavaType.buildType(HTTP_STATUS_CODE_FQ);
}
return type;
}));

m = m.withMethodType(met);
m = m.withParameters(ListUtils.map(m.getParameters(), var -> {
if (var instanceof J.VariableDeclarations) {
Expand All @@ -76,16 +93,57 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
v = v.withTypeExpression(TypeTree.build(httpStatusCodeSimpleName)
.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ)));
v = v.withVariables(singletonList(declaredVar.withType(JavaType.buildType(HTTP_STATUS_CODE_FQ))));

return v;
}
}
return var;
}));
// updateCursor(m);
}
maybeAddImport(HTTP_STATUS_CODE_FQ);
maybeRemoveImport(HTTP_STATUS_FQ);
return m;
return super.visitMethodDeclaration(m, ctx);
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations vd = super.visitVariableDeclarations(multiVariable, ctx);
if (TypeUtils.isAssignableTo(HTTP_STATUS_FQ, vd.getType())) {
if (vd.getVariables().get(0).getInitializer() instanceof J.MethodInvocation) {
J.MethodInvocation init = (J.MethodInvocation) vd.getVariables().get(0).getInitializer();
if (init != null && !HTTP_STATUS_VALUE_OF.matches(init.getMethodType())) {
vd = JavaTemplate.builder("HttpStatus.valueOf(#{any()})")
.imports(HTTP_STATUS_FQ)
.contextSensitive()
.doBeforeParseTemplate(System.out::println)
.build()
.apply(getCursor(), init.getCoordinates().replace(), init);
}

};
}
return vd;
}

@Override
public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
J.Return r = super.visitReturn(_return, ctx);
J.MethodDeclaration method = getCursor().firstEnclosing(J.MethodDeclaration.class);
if (r.getExpression() instanceof J.MethodInvocation) {
J.MethodInvocation returnMethod = (J.MethodInvocation) r.getExpression();
if (returnMethod.getSelect() instanceof J.Identifier) {
J.Identifier returnSelect = (J.Identifier)returnMethod.getSelect();
if (returnSelect.getSimpleName().equals("super") && TypeUtils.isAssignableTo(RESPONSE_ENTITY_EXCEPTION_HANDLER_FQ, returnMethod.getMethodType().getDeclaringType())) {
((J.VariableDeclarations)method.getParameters().get(0)).getVariables().get(0).getName();
List<Expression> expressions = new ArrayList<>();
for (Statement stmt : method.getParameters()) {
expressions.add(((J.VariableDeclarations)stmt).getVariables().get(0).getName());
}
return maybeAutoFormat(r.withExpression((((J.MethodInvocation) r.getExpression()).withArguments(expressions))), _return, ctx);
}
}
}
return super.visitReturn(_return, ctx);
}

private boolean hasHttpStatusParameter(J.MethodDeclaration m) {
Expand Down

0 comments on commit f104ad7

Please sign in to comment.