diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/DoesNotUseType.java b/rewrite-java/src/main/java/org/openrewrite/java/search/DoesNotUseType.java index 50daa8648e5..5de18770385 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/DoesNotUseType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/DoesNotUseType.java @@ -16,34 +16,38 @@ package org.openrewrite.java.search; import lombok.EqualsAndHashCode; -import lombok.Getter; import lombok.Value; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import org.jspecify.annotations.Nullable; +import org.openrewrite.*; @Value @EqualsAndHashCode(callSuper = false) public class DoesNotUseType extends Recipe { - @Getter - String fullyQualifiedType; + @Option(displayName = "Fully-qualified type name", + description = "A fully-qualified type name, that is used to find matching type references. " + + "Supports glob expressions. `java..*` finds every type from every subpackage of the `java` package.", + example = "java.util.List") + String fullyQualifiedTypeName; + @Option(displayName = "Include implicit type references", + description = "Whether to include implicit type references, such as those in method signatures.", + required = false) + @Nullable Boolean includeImplicit; @Override public String getDisplayName() { - return "Check whether a type is not in use"; + return "Check whether a type is **not** in use"; } @Override public String getDescription() { - return "To be used as a precondition to invalidate classes using the provided type. So recipe X doesn't run on a class using type Y"; + return "Useful as a precondition to skip over compilation units using the argument type."; } @Override public TreeVisitor getVisitor() { - return Preconditions.not(new UsesType<>(fullyQualifiedType, includeImplicit)); + return Preconditions.not(new UsesType<>(fullyQualifiedTypeName, includeImplicit)); } } diff --git a/rewrite-java/src/test/java/org/openrewrite/java/search/DoesNotUseTypeTest.java b/rewrite-java/src/test/java/org/openrewrite/java/search/DoesNotUseTypeTest.java index 9f054232228..4cdc9977b73 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/search/DoesNotUseTypeTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/search/DoesNotUseTypeTest.java @@ -28,30 +28,34 @@ class DoesNotUseTypeTest implements RewriteTest { public void defaults(RecipeSpec spec) { spec.recipeFromYaml( """ - --- - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.NotUsesTypeTest - description: Test. - preconditions: - - org.openrewrite.java.search.DoesNotUseType: - fullyQualifiedType: java.lang.String - includeImplicit: true - recipeList: - - org.openrewrite.java.OrderImports: - removeUnused: true - """ + type: specs.openrewrite.org/v1beta/recipe + name: org.acme.RemoveUnusedImportsIfNotUsingString + description: Test. + preconditions: + - org.openrewrite.java.search.DoesNotUseType: + fullyQualifiedTypeName: java.lang.String + includeImplicit: true + recipeList: + - org.openrewrite.java.RemoveUnusedImports + """, + "org.acme.RemoveUnusedImportsIfNotUsingString" ); } @DocumentExample @Test - void doesNotUseType() { + void importRemovedWhenTypeNotFound() { rewriteRun( java( """ import java.lang.StringBuilder; - - class Foo{ + + class Foo { + int bla = 123; + } + """, + """ + class Foo { int bla = 123; } """ @@ -60,14 +64,13 @@ class Foo{ } @Test - void doesUseType() { + void importRetainedWhenTypeInUse() { rewriteRun( java( """ import java.lang.StringBuilder; - - - class Foo{ + + class Foo { String bla = "bla"; } """