Skip to content

Commit

Permalink
Refactor mutable field in ChangeEmbeddedServletContainerCustomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 11, 2024
1 parent 9d3a3f0 commit e742517
Showing 1 changed file with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@

public class ChangeEmbeddedServletContainerCustomizer extends Recipe {


private static J.@Nullable ParameterizedType webFactoryCustomizerIdentifier;

private static final String DEPRECATED_INTERFACE_FQN = "org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer";

@Override
Expand All @@ -47,6 +44,8 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(DEPRECATED_INTERFACE_FQN, false), new JavaIsoVisitor<ExecutionContext>() {
private J.@Nullable ParameterizedType webFactoryCustomizerIdentifier;

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);
Expand All @@ -65,30 +64,30 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex

return c;
}
});
}

private static J.ParameterizedType getWebFactoryCustomizerIdentifier(ExecutionContext ctx) {
// Really no need to use a JavaTemplate in this recipe, we just compile a stubbed out class and extract
// the J.ParameterizedType from the class's stub's implements.
if (webFactoryCustomizerIdentifier == null) {
JavaParser parser = JavaParser
.fromJavaVersion()
.classpathFromResources(ctx, "spring-boot-2.*")
.build();
J.CompilationUnit cu = parser.parse(
"import org.springframework.boot.web.server.WebServerFactoryCustomizer;\n" +
"import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;\n" +
"public abstract class Template implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {}"
)
.map(J.CompilationUnit.class::cast)
.findFirst()
.get();
private J.ParameterizedType getWebFactoryCustomizerIdentifier(ExecutionContext ctx) {
// Really no need to use a JavaTemplate in this recipe, we just compile a stubbed out class and extract
// the J.ParameterizedType from the class's stub's implements.
if (webFactoryCustomizerIdentifier == null) {
JavaParser parser = JavaParser
.fromJavaVersion()
.classpathFromResources(ctx, "spring-boot-2.*")
.build();
J.CompilationUnit cu = parser.parse(
"import org.springframework.boot.web.server.WebServerFactoryCustomizer;\n" +
"import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;\n" +
"public abstract class Template implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {}"
)
.map(J.CompilationUnit.class::cast)
.findFirst()
.get();

webFactoryCustomizerIdentifier = (J.ParameterizedType) requireNonNull(cu.getClasses()
.get(0).getImplements()).get(0);
}
webFactoryCustomizerIdentifier = (J.ParameterizedType) requireNonNull(cu.getClasses()
.get(0).getImplements()).get(0);
}

return webFactoryCustomizerIdentifier.withId(Tree.randomId());
return webFactoryCustomizerIdentifier.withId(Tree.randomId());
}
});
}
}

0 comments on commit e742517

Please sign in to comment.