Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import org.openrewrite.*;
import org.openrewrite.java.marker.JavaProject;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.maven.tree.Dependency;
import org.openrewrite.maven.tree.MavenResolutionResult;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static org.openrewrite.internal.StringUtils.matchesGlob;

@EqualsAndHashCode(callSuper = false)
@Value
public class RemoveDependency extends ScanningRecipe<Map<JavaProject, Boolean>> {
public class RemoveDependency extends ScanningRecipe<RemoveDependency.Accumulator> {
@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*")
Expand Down Expand Up @@ -68,13 +73,18 @@ public class RemoveDependency extends ScanningRecipe<Map<JavaProject, Boolean>>
String description = "For Gradle project, removes a single dependency from the dependencies section of the `build.gradle`.\n" +
"For Maven project, removes a single dependency from the `<dependencies>` section of the pom.xml.";

public static class Accumulator {
final Map<JavaProject, Boolean> projectToInUse = new HashMap<>();
final Map<UUID, JavaProject> mavenIdToProject = new HashMap<>();
}

@Override
public Map<JavaProject, Boolean> getInitialValue(ExecutionContext ctx) {
return new HashMap<>();
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Map<JavaProject, Boolean> projectToInUse) {
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
if (unlessUsing == null) {
return TreeVisitor.noop();
}
Expand All @@ -83,15 +93,19 @@ public TreeVisitor<?, ExecutionContext> getScanner(Map<JavaProject, Boolean> pro
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
tree.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject ->
projectToInUse.compute(javaProject, (jp, foundSoFar) -> Boolean.TRUE.equals(foundSoFar) || tree != usesType.visit(tree, ctx)));
tree.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject -> {
acc.projectToInUse.compute(javaProject, (jp, foundSoFar) ->
Boolean.TRUE.equals(foundSoFar) || tree != usesType.visit(tree, ctx));
tree.getMarkers().findFirst(MavenResolutionResult.class).ifPresent(mrr ->
acc.mavenIdToProject.put(mrr.getId(), javaProject));
});
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Map<JavaProject, Boolean> projectToInUse) {
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
final TreeVisitor<?, ExecutionContext> gradleRemoveDep = new org.openrewrite.gradle.RemoveDependency(groupId, artifactId, configuration).getVisitor();
final TreeVisitor<?, ExecutionContext> mavenRemoveDep = new org.openrewrite.maven.RemoveDependency(groupId, artifactId, scope).getVisitor();
Expand All @@ -103,7 +117,11 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Map<JavaProject, Boolean> pro
}
if (unlessUsing != null) {
JavaProject jp = tree.getMarkers().findFirst(JavaProject.class).orElse(null);
if (jp == null || Boolean.TRUE.equals(projectToInUse.get(jp))) {
if (jp == null || Boolean.TRUE.equals(acc.projectToInUse.get(jp))) {
return tree;
}
MavenResolutionResult mrr = tree.getMarkers().findFirst(MavenResolutionResult.class).orElse(null);
if (mrr != null && anyDescendantPreservesDependency(mrr, acc)) {
return tree;
}
}
Expand All @@ -118,4 +136,35 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Map<JavaProject, Boolean> pro
}
};
}

/**
* Keep this pom's `<dependency>` declaration if any descendant module in the reactor uses the
* `unlessUsing` type AND does not also declare the dependency itself. A descendant that
* self-declares the dependency in its own pom is unaffected by removing it from an ancestor,
* so it should not block removal.
*/
private boolean anyDescendantPreservesDependency(MavenResolutionResult mrr, Accumulator acc) {
for (MavenResolutionResult child : mrr.getModules()) {
JavaProject childJp = acc.mavenIdToProject.get(child.getId());
if (childJp != null && Boolean.TRUE.equals(acc.projectToInUse.get(childJp)) && !declaresDependency(child)) {
return true;
}
if (anyDescendantPreservesDependency(child, acc)) {
return true;
}
}
return false;
}

private boolean declaresDependency(MavenResolutionResult mrr) {
// Look at the raw `requested` pom — the as-written `<dependencies>` of this module only,
// excluding inheritance from a parent pom. A module that inherits the dep from the parent
// we may be modifying should still block removal.
for (Dependency d : mrr.getPom().getRequested().getDependencies()) {
if (matchesGlob(d.getGroupId(), groupId) && matchesGlob(d.getArtifactId(), artifactId)) {
return true;
}
}
return false;
}
}
Loading
Loading