From 0aacba852543cf3daf7ccbcdb52d9cec0ffb6d61 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 20 Feb 2024 23:53:17 -0500 Subject: [PATCH] Fix transitive dependency method matchers --- .../UpgradeTransitiveDependencyVersion.java | 77 +++++++++++-------- .../java/format/BlankLinesVisitor.java | 2 +- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java index 7d05a66ac21..38ec1ec22d5 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java @@ -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); @@ -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 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 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); } @@ -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) @@ -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() @@ -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) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java index b79937ce3fb..68a4904c9e6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java @@ -262,7 +262,7 @@ private String keepMaximumLines(String whitespace, int max) { return whitespace; } - public JRightPadded minimumLines(JRightPadded tree, int min) { + private JRightPadded minimumLines(JRightPadded tree, int min) { return tree.withElement(minimumLines(tree.getElement(), min)); }