Skip to content

Commit

Permalink
Only allow Gradle AddDependency to add dependencies to the top-leve…
Browse files Browse the repository at this point in the history
…l `dependencies` block (#4181)

Fixes gh-4094
  • Loading branch information
shanman190 authored May 9, 2024
1 parent 264e8e8 commit 6db9201
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,15 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
G.CompilationUnit g = (G.CompilationUnit) s;
for (String resolvedConfiguration : resolvedConfigurations) {
g = (G.CompilationUnit) new AddDependencyVisitor(groupId, artifactId, version, versionPattern, resolvedConfiguration,
classifier, extension, metadataFailures).visitNonNull(g, ctx);
classifier, extension, metadataFailures, this::isTopLevel).visitNonNull(g, ctx);
}

return g;
}

private boolean isTopLevel(Cursor cursor) {
return cursor.getParent().firstEnclosing(J.MethodInvocation.class) == null;
}
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openrewrite.tree.ParseError;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -76,6 +77,9 @@ public class AddDependencyVisitor extends GroovyIsoVisitor<ExecutionContext> {
@Nullable
private String resolvedVersion;

@Nullable
private final Predicate<Cursor> insertPredicate;

@Override
public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
Optional<GradleProject> maybeGp = cu.getMarkers().findFirst(GradleProject.class);
Expand Down Expand Up @@ -220,6 +224,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return m;
}

if (!insertPredicate.test(getCursor())) {
return m;
}

J.Lambda dependenciesBlock = (J.Lambda) m.getArguments().get(0);
if (!(dependenciesBlock.getBody() instanceof J.Block)) {
return m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,57 @@ void defaultConfigurationEscaped() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4094")
void doNotAddToIncorrectBlocks() {
rewriteRun(
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", "com.google.common.math.IntMath", null)),
mavenProject("project",
srcMainJava(
java(usingGuavaIntMath)
),
buildGradle(
"""
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.1.5'
}
repositories {
mavenCentral()
}
dependencyManagement {
dependencies {
dependency "org.openrewrite:rewrite-core:8.0.0"
}
}
""",
"""
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.1.5'
}
repositories {
mavenCentral()
}
dependencyManagement {
dependencies {
dependency "org.openrewrite:rewrite-core:8.0.0"
}
}
dependencies {
implementation "com.google.guava:guava:29.0-jre"
}
"""
)
)
);
}

private AddDependency addDependency(String gav, String onlyIfUsing) {
return addDependency(gav, onlyIfUsing, null);
}
Expand Down

0 comments on commit 6db9201

Please sign in to comment.