Skip to content

Commit

Permalink
Add DoesNotUseType recipe for use in preconditions to reject source f…
Browse files Browse the repository at this point in the history
…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
4 people authored Sep 4, 2024
1 parent 153700c commit ac74ea6
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
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));
}
}
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";
}
"""
)
);
}
}

0 comments on commit ac74ea6

Please sign in to comment.