diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7a7ad4c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +insert_final_newline = true +trim_trailing_whitespace = true + +[src/test*/java/**.java] +indent_size = 4 +ij_continuation_indent_size = 2 diff --git a/src/main/java/org/openrewrite/java/dependencies/RemoveDependency.java b/src/main/java/org/openrewrite/java/dependencies/RemoveDependency.java index 9869747..dc36297 100644 --- a/src/main/java/org/openrewrite/java/dependencies/RemoveDependency.java +++ b/src/main/java/org/openrewrite/java/dependencies/RemoveDependency.java @@ -28,7 +28,7 @@ @Value @EqualsAndHashCode(callSuper = false) -public class RemoveDependency extends Recipe { +public class RemoveDependency extends Recipe { // TODO Extend ScanningRecipe instead @Option(displayName = "Group ID", description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.", example = "com.fasterxml.jackson*") @@ -39,9 +39,9 @@ public class RemoveDependency extends Recipe { example = "jackson-module*") String artifactId; - @Option(displayName = "Unless using", description = "If a dependency is used in the code, do not remove it.") + @Option(displayName = "Unless using", description = "If a dependency is used in the code, do not remove it.", example = "org.aspectj.lang.*") @Nullable - Boolean unlessUsing; + String unlessUsing; // Gradle only parameter @Option(displayName = "The dependency configuration", description = "The dependency configuration to remove from.", example = "api", required = false) @@ -76,7 +76,7 @@ public String getDescription() { public RemoveDependency( String groupId, String artifactId, - @Nullable Boolean unlessUsing, + @Nullable String unlessUsing, @Nullable String configuration, @Nullable String scope) { this.groupId = groupId; @@ -88,12 +88,15 @@ public RemoveDependency( removeMavenDependency = new org.openrewrite.maven.RemoveDependency(groupId, artifactId, scope); } + // TODO Provide a scanner that looks across java sources to see if `unlessUsing` is used + // Optionally look for use in the correct scope and submodule, but for do-no-harm v1 can skip that for now + + + // TODO remove this method, and instead override getVisitor, as the scanning condition is not evaluated for getRecipeList @Override public List getRecipeList() { - if (Boolean.TRUE.equals(unlessUsing)) { - // logic to check if code uses the lib, if so -> - return emptyList(); - } return Arrays.asList(removeGradleDependency, removeMavenDependency); } + + // TODO override getVisitor that only calls out to removeGradleDependency or removeMavenDependency if the scanning condition is met } diff --git a/src/test/java/org/openrewrite/.editorconfig b/src/test/java/org/openrewrite/.editorconfig deleted file mode 100644 index a482493..0000000 --- a/src/test/java/org/openrewrite/.editorconfig +++ /dev/null @@ -1,5 +0,0 @@ -root = true - -[*.java] -indent_size = 4 -ij_continuation_indent_size = 2 diff --git a/src/test/java/org/openrewrite/java/dependencies/RemoveDependencyTest.java b/src/test/java/org/openrewrite/java/dependencies/RemoveDependencyTest.java index d668cab..e84f84d 100644 --- a/src/test/java/org/openrewrite/java/dependencies/RemoveDependencyTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/RemoveDependencyTest.java @@ -17,10 +17,12 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; import org.openrewrite.test.RewriteTest; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; +import static org.openrewrite.java.Assertions.*; import static org.openrewrite.maven.Assertions.pomXml; class RemoveDependencyTest implements RewriteTest { @@ -30,7 +32,7 @@ class RemoveDependencyTest implements RewriteTest { void removeGradleDependencyUsingStringNotationWithExclusion() { rewriteRun( spec -> spec.beforeRecipe(withToolingApi()) - .recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", false, null, null)), + .recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", null, null, null)), //language=groovy buildGradle( """ @@ -70,7 +72,7 @@ void removeGradleDependencyUsingStringNotationWithExclusion() { @Test void removeMavenDependency() { rewriteRun( - spec -> spec.recipe(new RemoveDependency("junit", "junit", false, null, null)), + spec -> spec.recipe(new RemoveDependency("junit", "junit", null, null, null)), //language=xml pomXml( """ @@ -118,28 +120,58 @@ void removeMavenDependency() { } @Test - void doNotRemoveDependencyIfItIsUsedAndUnlessUsingIsTrue() { + void doNotRemoveIfInUse() { rewriteRun( - spec -> spec.beforeRecipe(withToolingApi()) - .recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", true, null, null)), - //language=groovy - buildGradle( - """ - plugins { - id 'java-library' - } - - repositories { - mavenCentral() - } + spec -> spec + .parser(JavaParser.fromJavaVersion().dependsOn( + //language=java + """ + package org.aspectj.lang.annotation; - dependencies { - implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") { - exclude group: "junit" + import java.lang.annotation.Target; + import java.lang.annotation.ElementType; + import java.lang.annotation.Retention; + import java.lang.annotation.RetentionPolicy; + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface Aspect { + } + """ + )) + .recipe(new RemoveDependency("org.aspectj", "aspectjrt", "org.aspectj.lang.annotation.*", null, null)), + mavenProject("example", + //language=java + srcMainJava( + java( + """ + import org.aspectj.lang.annotation.Aspect; + @Aspect + class MyLoggingInterceptor { } - testImplementation "org.junit.vintage:junit-vintage-engine:5.6.2" - } + """ + ) + ), + //language=xml + pomXml( """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + + + org.aspectj + aspectjrt + 1.9.22.1 + + + + """ + ) ) ); }