diff --git a/src/main/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessing.java b/src/main/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessing.java index 2f7d95c92..d28660d27 100644 --- a/src/main/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessing.java +++ b/src/main/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessing.java @@ -16,11 +16,13 @@ package org.openrewrite.java.spring.boot3; import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.search.FindAnnotations; +import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -36,16 +38,18 @@ public String getDescription() { return "Add or remove the `@EnableBatchProcessing` annotation from a Spring Boot application."; } + private static final String ENABLE_BATCH_PROCESSING = "org.springframework.batch.core.configuration.annotation.EnableBatchProcessing"; + @Override public TreeVisitor getVisitor() { - return new JavaIsoVisitor() { - + return Preconditions.check(new UsesType<>(ENABLE_BATCH_PROCESSING, true), new JavaIsoVisitor() { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { if (!FindAnnotations.find(classDecl, "@org.springframework.boot.autoconfigure.SpringBootApplication").isEmpty() && - !FindAnnotations.find(classDecl, "@org.springframework.batch.core.configuration.annotation.EnableBatchProcessing").isEmpty()) { + !FindAnnotations.find(classDecl, "@" + ENABLE_BATCH_PROCESSING).isEmpty()) { return classDecl.withLeadingAnnotations(ListUtils.map(classDecl.getLeadingAnnotations(), a -> { - if (TypeUtils.isOfClassType(a.getType(), "org.springframework.batch.core.configuration.annotation.EnableBatchProcessing")) { + if (TypeUtils.isOfClassType(a.getType(), ENABLE_BATCH_PROCESSING)) { + maybeRemoveImport(ENABLE_BATCH_PROCESSING); return null; } return a; @@ -53,6 +57,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex } return super.visitClassDeclaration(classDecl, ctx); } - }; + }); } } diff --git a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessingTest.java b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessingTest.java index 9fb0ed897..1283bd0ad 100644 --- a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessingTest.java +++ b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/RemoveEnableBatchProcessingTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; -public class RemoveEnableBatchProcessingTest implements RewriteTest { +class RemoveEnableBatchProcessingTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { @@ -47,7 +47,6 @@ public class Application { } """, """ - import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication