From 58f15f72924124f961c900ca41589b02b0c5b818 Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Thu, 14 Nov 2024 19:05:34 -0800 Subject: [PATCH] Fix Gradle AddDependency not doing anything when no onlyIfUsing parameter is specified --- .../org/openrewrite/gradle/AddDependency.java | 20 +- .../openrewrite/gradle/AddDependencyTest.java | 262 ++++++++++-------- 2 files changed, 162 insertions(+), 120 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java index fa7565211e5..990a0606d65 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java @@ -115,8 +115,6 @@ public class AddDependency extends ScanningRecipe { @Nullable Boolean acceptTransitive; - static final String DEPENDENCY_PRESENT = "org.openrewrite.gradle.AddDependency.DEPENDENCY_PRESENT"; - @Override public String getDisplayName() { return "Add Gradle dependency"; @@ -155,7 +153,8 @@ public Scanned getInitialValue(ExecutionContext ctx) { public TreeVisitor getScanner(Scanned acc) { return new TreeVisitor() { - UsesType usesType; + @Nullable + UsesType usesType = null; private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) { if(onlyIfUsing == null) { return true; @@ -168,14 +167,17 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) { @Override public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { - SourceFile sourceFile = (SourceFile) requireNonNull(tree); + if (!(tree instanceof SourceFile)) { + return tree; + } + SourceFile sourceFile = (SourceFile) tree; sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject -> sourceFile.getMarkers().findFirst(JavaSourceSet.class).ifPresent(sourceSet -> { if (usesType(sourceFile, ctx)) { acc.usingType = true; - Set configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>()); - configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation"); } + Set configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>()); + configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation"); })); return tree; } @@ -184,7 +186,7 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) { @Override public TreeVisitor getVisitor(Scanned acc) { - return Preconditions.check(acc.usingType && !acc.configurationsByProject.isEmpty(), + return Preconditions.check((onlyIfUsing == null || acc.usingType) && !acc.configurationsByProject.isEmpty(), Preconditions.check(new IsBuildGradle<>(), new GroovyIsoVisitor() { @Override @@ -225,7 +227,7 @@ public TreeVisitor getVisitor(Scanned acc) { tmpConfigurations = new HashSet<>(resolvedConfigurations); for (String tmpConfiguration : tmpConfigurations) { - GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration); + GradleDependencyConfiguration gdc = requireNonNull((gp.getConfiguration(tmpConfiguration))); for (GradleDependencyConfiguration transitive : gp.configurationsExtendingFrom(gdc, true)) { if (resolvedConfigurations.contains(transitive.getName()) || (Boolean.TRUE.equals(acceptTransitive) && transitive.findResolvedDependency(groupId, artifactId) != null)) { @@ -248,7 +250,7 @@ public TreeVisitor getVisitor(Scanned acc) { } private boolean isTopLevel(Cursor cursor) { - return cursor.getParent().firstEnclosing(J.MethodInvocation.class) == null; + return cursor.getParentOrThrow().firstEnclosing(J.MethodInvocation.class) == null; } }) ); diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java index 42596fb4002..6330f931018 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java @@ -76,7 +76,7 @@ void onlyIfUsingTestScope(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -85,11 +85,11 @@ void onlyIfUsingTestScope(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { testImplementation "com.google.guava:guava:29.0-jre" } @@ -115,11 +115,11 @@ void onlyIfUsingSmokeTestScope(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -131,17 +131,17 @@ void onlyIfUsingSmokeTestScope(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { smokeTestImplementation "com.google.guava:guava:29.0-jre" } @@ -165,7 +165,7 @@ void onlyIfUsingCompileScope(String onlyIfUsing) { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -174,11 +174,11 @@ void onlyIfUsingCompileScope(String onlyIfUsing) { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -211,7 +211,7 @@ void onlyIfUsingMultipleScopes(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -220,11 +220,11 @@ void onlyIfUsingMultipleScopes(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -257,11 +257,11 @@ void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -273,17 +273,17 @@ void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -312,11 +312,11 @@ void usedInTransitiveSourceSet() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -328,17 +328,17 @@ void usedInTransitiveSourceSet() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { testImplementation "com.google.guava:guava:29.0-jre" } @@ -367,11 +367,11 @@ void addDependencyIfNotUsedInATransitive() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "main" @@ -383,20 +383,20 @@ void addDependencyIfNotUsedInATransitive() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "main" } } - + dependencies { smokeTestImplementation "com.google.guava:guava:29.0-jre" - + testImplementation "com.google.guava:guava:29.0-jre" } """ @@ -419,7 +419,7 @@ void addDependencyWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -428,11 +428,11 @@ void addDependencyWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static:2.0.54.Final:linux-x86_64" } @@ -456,7 +456,7 @@ void addDependencyWithoutVersion() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -465,11 +465,11 @@ void addDependencyWithoutVersion() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static" } @@ -494,7 +494,7 @@ void addDependencyWithoutVersionWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -503,11 +503,11 @@ void addDependencyWithoutVersionWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static" } @@ -546,11 +546,11 @@ void addInOrder() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" } @@ -559,11 +559,11 @@ void addInOrder() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" implementation "commons-lang:commons-lang:1.0" @@ -587,11 +587,11 @@ void addTestDependenciesAfterCompile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" } @@ -600,14 +600,14 @@ void addTestDependenciesAfterCompile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" - + testImplementation "com.google.guava:guava:29.0-jre" } """ @@ -629,11 +629,11 @@ void addDependenciesKeepFormatting() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.40.8" testImplementation "junit:junit:4.12" @@ -643,11 +643,11 @@ void addDependenciesKeepFormatting() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.40.8" implementation "org.slf4j:slf4j-api:2.0.7" @@ -667,7 +667,7 @@ void addDependencyToNewGrouping() { srcMainJava( java(""" import lombok.Value; - + @Value class A { String b; @@ -680,11 +680,11 @@ class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:2.6" @@ -695,16 +695,16 @@ class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { annotationProcessor "org.projectlombok:lombok:1.18.26" - + implementation "commons-lang:commons-lang:2.6" - + testImplementation "junit:junit:4.13" } """ @@ -726,11 +726,11 @@ void addDependenciesToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" @@ -742,14 +742,14 @@ void addDependenciesToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava", version: "29.0-jre" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -773,14 +773,14 @@ void addDependenciesWithoutVersionToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -789,14 +789,14 @@ void addDependenciesWithoutVersionToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -820,14 +820,14 @@ void addDependenciesWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -836,14 +836,14 @@ void addDependenciesWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava", version: "29.0-jre", classifier: "test" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -868,14 +868,14 @@ void addDependenciesWithoutVersionWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -884,14 +884,14 @@ void addDependenciesWithoutVersionWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "io.netty", name: "netty-tcnative-boringssl-static", classifier: "linux-x86_64" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -915,11 +915,11 @@ void matchesDependencyDeclarationStyle() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" } @@ -928,11 +928,11 @@ void matchesDependencyDeclarationStyle() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" @@ -957,11 +957,11 @@ void addDependencyDoesntAddWhenExistingDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:28.0-jre" } @@ -991,7 +991,7 @@ public class A { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1000,11 +1000,11 @@ public class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { %s "com.fasterxml.jackson.core:jackson-core:2.12.0" } @@ -1038,7 +1038,7 @@ void addDependencyToProjectWithOtherSourceTypes() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1047,11 +1047,11 @@ void addDependencyToProjectWithOtherSourceTypes() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -1091,7 +1091,7 @@ void addDependencyToProjectsThatNeedIt() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1100,11 +1100,11 @@ void addDependencyToProjectsThatNeedIt() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -1131,7 +1131,7 @@ void addDynamicVersionDependency() { groovy( """ import java.util.* - + class MyClass { static void main(String[] args) { Date date = new Date() @@ -1146,7 +1146,7 @@ static void main(String[] args) { plugins { id 'java' } - + repositories { mavenCentral() } @@ -1155,11 +1155,11 @@ static void main(String[] args) { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.39.1" } @@ -1196,24 +1196,24 @@ void addDependencyWithVariable() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + def guavaVersion = "29.0-jre" """, """ plugins { id 'java-library' } - + repositories { mavenCentral() } - + def guavaVersion = "29.0-jre" - + dependencies { implementation "com.google.guava:guava:${guavaVersion}" } @@ -1238,7 +1238,7 @@ void defaultConfigurationEscaped() { plugins { id 'java' } - + repositories { mavenCentral() } @@ -1247,11 +1247,11 @@ void defaultConfigurationEscaped() { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { 'default' "com.google.guava:guava:29.0-jre" } @@ -1312,11 +1312,51 @@ void doNotAddToIncorrectBlocks() { ); } - private AddDependency addDependency(String gav, String onlyIfUsing) { + @Test + void addUnconditionally() { + rewriteRun( + spec -> spec.recipe(addDependency("org.apache.logging.log4j:log4j-core:2.22.1")), + mavenProject("project", + srcMainJava( + java(usingGuavaIntMath) + ), + buildGradle(""" + plugins { + id "java-library" + } + repositories { + mavenCentral() + } + dependencies { + implementation 'org.openrewrite:rewrite-core:8.35.0' + } + """, + """ + plugins { + id "java-library" + } + repositories { + mavenCentral() + } + dependencies { + implementation "org.apache.logging.log4j:log4j-core:2.22.1" + implementation 'org.openrewrite:rewrite-core:8.35.0' + } + """, + spec -> spec.path("build.gradle") + )) + ); + } + + private AddDependency addDependency(@SuppressWarnings("SameParameterValue") String gav) { + return addDependency(gav, null, null); + } + + private AddDependency addDependency(String gav, @Nullable String onlyIfUsing) { return addDependency(gav, onlyIfUsing, null); } - private AddDependency addDependency(String gav, String onlyIfUsing, @Nullable String configuration) { + private AddDependency addDependency(String gav, @Nullable String onlyIfUsing, @Nullable String configuration) { String[] gavParts = gav.split(":"); return new AddDependency( gavParts[0], gavParts[1], (gavParts.length < 3) ? null : gavParts[2], null, configuration, onlyIfUsing,