Skip to content

Commit

Permalink
Add missing options and actually run recipe in test
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 4, 2024
1 parent b3a7220 commit d917c59
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<?, ExecutionContext> getVisitor() {
return Preconditions.not(new UsesType<>(fullyQualifiedType, includeImplicit));
return Preconditions.not(new UsesType<>(fullyQualifiedTypeName, includeImplicit));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
"""
Expand All @@ -60,14 +64,13 @@ class Foo{
}

@Test
void doesUseType() {
void importRetainedWhenTypeInUse() {
rewriteRun(
java(
"""
import java.lang.StringBuilder;
class Foo{
class Foo {
String bla = "bla";
}
"""
Expand Down

0 comments on commit d917c59

Please sign in to comment.