diff --git a/src/main/java/org/openrewrite/java/dependencies/AddDependency.java b/src/main/java/org/openrewrite/java/dependencies/AddDependency.java index 1788f28..defae0c 100644 --- a/src/main/java/org/openrewrite/java/dependencies/AddDependency.java +++ b/src/main/java/org/openrewrite/java/dependencies/AddDependency.java @@ -17,17 +17,12 @@ import lombok.EqualsAndHashCode; import lombok.Value; -import org.openrewrite.Option; -import org.openrewrite.Recipe; +import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; -import java.util.Arrays; -import java.util.List; - - @Value @EqualsAndHashCode(callSuper = true) -public class AddDependency extends Recipe { +public class AddDependency extends ScanningRecipe { // Gradle and Maven shared parameters @Option(displayName = "Group", description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`.", @@ -54,7 +49,6 @@ public class AddDependency extends Recipe { @Nullable String versionPattern; - @SuppressWarnings("NullableProblems") @Option(displayName = "Only if using", description = "Used to determine if the dependency will be added and in which scope it should be placed.", example = "org.junit.jupiter.api.*", @@ -143,54 +137,66 @@ public String getDescription() { "correct scope based on where it is used."; } + @Override + public Accumulator getInitialValue(ExecutionContext ctx) { + return new Accumulator(gradleAddDep().getInitialValue(ctx), mavenAddDep().getInitialValue(ctx)); + } + + @Override + public TreeVisitor getScanner(Accumulator acc) { + return new TreeVisitor() { + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { + gradleAddDep().getScanner(acc.gradleAccumulator).visit(tree, ctx); + mavenAddDep().getScanner(acc.mavenAccumulator).visit(tree, ctx); + return tree; + } + }; + } + + @Override + public TreeVisitor getVisitor(Accumulator acc) { + return new TreeVisitor() { + final TreeVisitor gradleAddDep = gradleAddDep().getVisitor(acc.gradleAccumulator); + final TreeVisitor mavenAddDep = mavenAddDep().getVisitor(acc.mavenAccumulator); + @Override + public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { + if(!(tree instanceof SourceFile)) { + return tree; + } + Tree t = tree; + if(gradleAddDep.isAcceptable((SourceFile) t, ctx)) { + t = gradleAddDep.visitNonNull(tree, ctx); + } + if(mavenAddDep.isAcceptable((SourceFile) t, ctx)) { + t = mavenAddDep.visitNonNull(tree, ctx); + } + return t; + } + }; + } + + @Value + public static class Accumulator { + org.openrewrite.gradle.AddDependency.Scanned gradleAccumulator; + org.openrewrite.maven.AddDependency.Scanned mavenAccumulator; + } + @Nullable org.openrewrite.gradle.AddDependency addGradleDependency; @Nullable org.openrewrite.maven.AddDependency addMavenDependency; - public AddDependency( - String groupId, - String artifactId, - @Nullable String version, - @Nullable String versionPattern, - String onlyIfUsing, - @Nullable String classifier, - @Nullable String familyPattern, - @Nullable String extension, - @Nullable String configuration, - @Nullable String scope, - @Nullable Boolean releasesOnly, - @Nullable String type, - @Nullable Boolean optional, - @Nullable Boolean acceptTransitive) { - this.groupId = groupId; - this.artifactId = artifactId; - this.version = version; - this.versionPattern = versionPattern; - this.onlyIfUsing = onlyIfUsing; - this.classifier = classifier; - this.familyPattern = familyPattern; - this.extension = extension; - this.configuration = configuration; - this.scope = scope; - this.releasesOnly = releasesOnly; - this.type = type; - this.optional = optional; - this.acceptTransitive = acceptTransitive; - addGradleDependency = new org.openrewrite.gradle.AddDependency(groupId, artifactId, version, versionPattern, - configuration, onlyIfUsing, classifier, extension, familyPattern, acceptTransitive); - String versionForMaven = version != null ? version : "latest.release"; - addMavenDependency = new org.openrewrite.maven.AddDependency(groupId, artifactId, versionForMaven, - versionPattern, scope, releasesOnly, onlyIfUsing, type, classifier, optional, familyPattern, - acceptTransitive); + + private org.openrewrite.gradle.AddDependency gradleAddDep() { + return new org.openrewrite.gradle.AddDependency(groupId, artifactId, version, versionPattern, + configuration, onlyIfUsing, classifier, extension, familyPattern, acceptTransitive); } - @Override - public List getRecipeList() { - return Arrays.asList( - addGradleDependency, - addMavenDependency - ); + private org.openrewrite.maven.AddDependency mavenAddDep() { + return new org.openrewrite.maven.AddDependency(groupId, artifactId, version != null ? version : "latest.release", + versionPattern, scope, releasesOnly, onlyIfUsing, type, classifier, optional, familyPattern, + acceptTransitive); } } diff --git a/src/test/java/org/openrewrite/java/dependencies/AddDependencyTest.java b/src/test/java/org/openrewrite/java/dependencies/AddDependencyTest.java index 033fcde..56c5268 100644 --- a/src/test/java/org/openrewrite/java/dependencies/AddDependencyTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/AddDependencyTest.java @@ -53,7 +53,7 @@ boolean getMap() { @ValueSource(strings = {"com.google.common.math.*", "com.google.common.math.IntMath"}) void addGradleDependencyWithOnlyIfUsingTestScope(String onlyIfUsing) { rewriteRun( - spec -> spec.beforeRecipe(withToolingApi()).recipe(addGradleDependency("com.google.guava:guava:29.0-jre", onlyIfUsing)), + spec -> spec.beforeRecipe(withToolingApi()).recipe(addDependency("com.google.guava:guava:29.0-jre", onlyIfUsing, null)), mavenProject("project", srcTestJava( java(usingGuavaIntMath) @@ -91,7 +91,7 @@ void addGradleDependencyWithOnlyIfUsingTestScope(String onlyIfUsing) { void addMavenDependencyWithSystemScope() { rewriteRun( spec -> spec - .recipe(addMavenDependency("doesnotexist:doesnotexist:1", "com.google.common.math.IntMath", "system")), + .recipe(addDependency("doesnotexist:doesnotexist:1", "com.google.common.math.IntMath", "system")), mavenProject("project", srcMainJava( java(usingGuavaIntMath) @@ -124,8 +124,8 @@ void addMavenDependencyWithSystemScope() { ); } - @SuppressWarnings("all") - private AddDependency addGradleDependency(String gav, String onlyIfUsing) { + + private AddDependency addDependency(String gav, String onlyIfUsing, @Nullable String scope) { String[] gavParts = gav.split(":"); return new AddDependency( gavParts[0], @@ -137,31 +137,12 @@ private AddDependency addGradleDependency(String gav, String onlyIfUsing) { null, null, null, + scope, null, null, null, null, - null - ); - } - - @SuppressWarnings("all") - private AddDependency addMavenDependency(String gav, String onlyIfUsing, @Nullable String scope) { - String[] gavParts = gav.split(":"); - return new AddDependency( - gavParts[0], - gavParts[1], - gavParts[2], - null, - onlyIfUsing, - null, - null, - onlyIfUsing, - null, - scope, - true, null, - false, null ); }