-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DoesNotUseType recipe for use in preconditions to reject source f…
…iles (#4471) * Initial setup with test cases * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * PR feedback * Remove unused should be true so it shows the recipe did not run * Apply formatter to NotUsesTypeTest * PR feedback * Add missing options and actually run recipe in test --------- Co-authored-by: Laurens Westerlaken <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
153700c
commit ac74ea6
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
rewrite-java/src/main/java/org/openrewrite/java/search/DoesNotUseType.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,53 @@ | ||
/* | ||
* 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.search; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.*; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class DoesNotUseType extends Recipe { | ||
|
||
@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"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Useful as a precondition to skip over compilation units using the argument type."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.not(new UsesType<>(fullyQualifiedTypeName, includeImplicit)); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
rewrite-java/src/test/java/org/openrewrite/java/search/DoesNotUseTypeTest.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,80 @@ | ||
/* | ||
* 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.search; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class DoesNotUseTypeTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipeFromYaml( | ||
""" | ||
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 importRemovedWhenTypeNotFound() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.lang.StringBuilder; | ||
class Foo { | ||
int bla = 123; | ||
} | ||
""", | ||
""" | ||
class Foo { | ||
int bla = 123; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void importRetainedWhenTypeInUse() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.lang.StringBuilder; | ||
class Foo { | ||
String bla = "bla"; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |