-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate recipe to convert getStatus to getStatusCode with correct types
- Loading branch information
Showing
3 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
.../openrewrite/java/spring/framework/MigrateResponseStatusExceptionGetStatusCodeMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.spring.framework; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.MethodMatcher; | ||
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.stream.Collectors.toList; | ||
|
||
public class MigrateResponseStatusExceptionGetStatusCodeMethod extends Recipe { | ||
private static final MethodMatcher GET_STATUS_METHOD_MATCHER = new MethodMatcher( | ||
"org.springframework.web.server.ResponseStatusException getStatus()" | ||
); | ||
private static final String HTTP_STATUS_CODE_CLASS = "HttpStatusCode"; | ||
private static final String FULL_HTTP_STATUS_CLASS = "org.springframework.http.HttpStatus"; | ||
private static final String FULL_HTTP_STATUS_CODE_CLASS = "org.springframework.http.HttpStatusCode"; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Migrate `ResponseStatusException#getStatus()` to `getStatusCode()`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Migrate Spring Framework 5.3's `ResponseStatusException` method `getStatus()` to Spring Framework 6's `getStatusCode()`."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
if (GET_STATUS_METHOD_MATCHER.matches(method)) { | ||
return JavaTemplate.builder("#{any()}.getStatusCode()") | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-core-6", "spring-beans-6", "spring-web-6")) | ||
.build() | ||
.apply(getCursor(), method.getCoordinates().replace(), method.getSelect()); | ||
} | ||
return super.visitMethodInvocation(method, ctx); | ||
} | ||
|
||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { | ||
if (multiVariable.getTypeExpression() != null && TypeUtils.isOfClassType(multiVariable.getTypeExpression().getType(), FULL_HTTP_STATUS_CLASS)) { | ||
multiVariable = changeTypeToHttpStatusCode(multiVariable); | ||
maybeAddImport(FULL_HTTP_STATUS_CODE_CLASS); | ||
maybeRemoveImport(FULL_HTTP_STATUS_CLASS); | ||
} | ||
return super.visitVariableDeclarations(multiVariable, ctx); | ||
} | ||
}; | ||
} | ||
|
||
private static J.VariableDeclarations changeTypeToHttpStatusCode(J.VariableDeclarations variableDeclarations) { | ||
final JavaType.ShallowClass httpStatusCodeJavaType = JavaType.ShallowClass.build(FULL_HTTP_STATUS_CODE_CLASS); | ||
return variableDeclarations | ||
.withTypeExpression( | ||
TypeTree.build(HTTP_STATUS_CODE_CLASS).withType(httpStatusCodeJavaType) | ||
).withVariables( | ||
variableDeclarations.getVariables().stream() | ||
.map(variable -> { | ||
if (variable.getVariableType() != null && TypeUtils.isAssignableTo(FULL_HTTP_STATUS_CLASS, variable.getType())) { | ||
return variable | ||
.withVariableType(variable.getVariableType().withType(httpStatusCodeJavaType)) | ||
.withName(variable.getName().withType(httpStatusCodeJavaType).withFieldType(variable.getName().getFieldType().withType(httpStatusCodeJavaType))); | ||
} | ||
return variable; | ||
}) | ||
.collect(toList()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters