Skip to content

Commit

Permalink
Fix transitive dependency method matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Feb 21, 2024
1 parent 49b9df5 commit 0aacba8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
@EqualsAndHashCode(callSuper = false)
public class UpgradeTransitiveDependencyVersion extends Recipe {
private static final MethodMatcher DEPENDENCIES_DSL_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");
private static final MethodMatcher CONSTRAINTS_MATCHER = new MethodMatcher("DependencyHandlerSpec constraints(..)");
private static final String CONSTRAINT_MATCHER = "DependencyHandlerSpec *(..)";
private static final MethodMatcher CONSTRAINTS_MATCHER = new MethodMatcher("org.gradle.api.artifacts.dsl.DependencyHandler constraints(..)", true);
private static final String CONSTRAINT_MATCHER = "org.gradle.api.artifacts.dsl.DependencyHandler *(..)";

@EqualsAndHashCode.Exclude
transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this);
Expand Down Expand Up @@ -141,38 +141,49 @@ public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
try {
String selected = versionSelector.select(resolved.getGav(), configuration.getName(),
version, versionPattern, ctx);
if (!resolved.getVersion().equals(selected)) {
toUpdate.merge(
new GroupArtifact(resolved.getGroupId(), resolved.getArtifactId()),
singletonMap(constraintConfiguration(configuration), selected),
(existing, update) -> {
Map<GradleDependencyConfiguration, String> all = new HashMap<>(existing);
all.putAll(update);
all.keySet().removeIf(c -> {
for (GradleDependencyConfiguration config : all.keySet()) {
if (c.allExtendsFrom().contains(config)) {
return true;
}
if (resolved.getVersion().equals(selected)) {
continue;
}

// TODO there has to be a better way!
if (c.getName().equals("runtimeOnly")) {
if (config.getName().equals("implementation")) {
return true;
}
GradleDependencyConfiguration constraintConfig = constraintConfiguration(configuration);
if (constraintConfig == null) {
continue;
}

toUpdate.merge(
new GroupArtifact(resolved.getGroupId(), resolved.getArtifactId()),
singletonMap(constraintConfig, selected),
(existing, update) -> {
Map<GradleDependencyConfiguration, String> all = new HashMap<>(existing);
all.putAll(update);
all.keySet().removeIf(c -> {
if (c == null) {
return true; // TODO ?? how does this happen
}

for (GradleDependencyConfiguration config : all.keySet()) {
if (c.allExtendsFrom().contains(config)) {
return true;
}

// TODO there has to be a better way!
if (c.getName().equals("runtimeOnly")) {
if (config.getName().equals("implementation")) {
return true;
}
if (c.getName().equals("testRuntimeOnly")) {
if (config.getName().equals("testImplementation") ||
config.getName().equals("implementation")) {
return true;
}
}
if (c.getName().equals("testRuntimeOnly")) {
if (config.getName().equals("testImplementation") ||
config.getName().equals("implementation")) {
return true;
}
}
return false;
});
return all;
}
);
}
}
return false;
});
return all;
}
);
} catch (MavenDownloadingException e) {
return Markup.warn(cu, e);
}
Expand Down Expand Up @@ -236,7 +247,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
"}\n"
).findFirst().orElseThrow(() -> new IllegalStateException("Unable to parse constraints block"));

Statement constraints = FindMethods.find(withConstraints, "DependencyHandlerSpec constraints(..)")
Statement constraints = FindMethods.find(withConstraints, "org.gradle.api.artifacts.dsl.DependencyHandler constraints(..)", true)
.stream()
.filter(J.MethodInvocation.class::isInstance)
.map(J.MethodInvocation.class::cast)
Expand Down Expand Up @@ -276,7 +287,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (CONSTRAINTS_MATCHER.matches(method)) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

boolean constraintExists = FindMethods.find(m, CONSTRAINT_MATCHER).stream()
boolean constraintExists = FindMethods.find(m, CONSTRAINT_MATCHER, true).stream()
.filter(J.MethodInvocation.class::isInstance)
.map(J.MethodInvocation.class::cast)
.anyMatch(c -> c.getSimpleName().equals(config) && c.getArguments().stream()
Expand All @@ -300,7 +311,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
because == null ? "" : String.format(" {\n because '%s'\n}", because)
)).findFirst().orElseThrow(() -> new IllegalStateException("Unable to parse constraint"));

Statement constraint = FindMethods.find(withConstraint, CONSTRAINT_MATCHER)
Statement constraint = FindMethods.find(withConstraint, CONSTRAINT_MATCHER, true)
.stream()
.filter(J.MethodInvocation.class::isInstance)
.map(J.MethodInvocation.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private String keepMaximumLines(String whitespace, int max) {
return whitespace;
}

public <J2 extends J> JRightPadded<J2> minimumLines(JRightPadded<J2> tree, int min) {
private <J2 extends J> JRightPadded<J2> minimumLines(JRightPadded<J2> tree, int min) {
return tree.withElement(minimumLines(tree.getElement(), min));
}

Expand Down

0 comments on commit 0aacba8

Please sign in to comment.