diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependencyClassifier.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependencyClassifier.java index fcc162cea02..007f0c45ef4 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependencyClassifier.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependencyClassifier.java @@ -214,11 +214,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu G.MapEntry mapEntry = null; String classifierStringDelimiter = null; int index = 0; - for (Expression e : depArgs) { - if (!(e instanceof G.MapEntry)) { - continue; - } - G.MapEntry arg = (G.MapEntry) e; + for (G.MapEntry arg : map.getElements()) { if (!(arg.getKey() instanceof J.Literal) || !(arg.getValue() instanceof J.Literal)) { continue; } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java index b527b7362b5..4f6804c6e07 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java @@ -28,6 +28,7 @@ import org.openrewrite.groovy.GroovyIsoVisitor; import org.openrewrite.groovy.tree.G; import org.openrewrite.internal.ListUtils; +import org.openrewrite.internal.StringUtils; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; @@ -37,7 +38,10 @@ import org.openrewrite.maven.tree.GroupArtifactVersion; import org.openrewrite.maven.tree.ResolvedDependency; import org.openrewrite.maven.tree.ResolvedPom; -import org.openrewrite.semver.*; +import org.openrewrite.semver.ExactVersion; +import org.openrewrite.semver.LatestIntegration; +import org.openrewrite.semver.Semver; +import org.openrewrite.semver.VersionComparator; import java.util.*; @@ -98,7 +102,6 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - DependencyMatcher dependencyMatcher = new DependencyMatcher(groupPattern == null ? "*" : groupPattern, artifactPattern == null ? "*" : artifactPattern, null); return Preconditions.check( new IsBuildGradle<>(), new GroovyIsoVisitor() { @@ -197,14 +200,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = super.visitMethodInvocation(method, ctx); - Optional maybeGradleDependency = new GradleDependency.Matcher().get(getCursor()); + Optional maybeGradleDependency = new GradleDependency.Matcher() + .groupId(groupPattern) + .artifactId(artifactPattern) + .get(getCursor()); if (!maybeGradleDependency.isPresent()) { return m; } GradleDependency gradleDependency = maybeGradleDependency.get(); ResolvedDependency d = gradleDependency.getResolvedDependency(); - if (!dependencyMatcher.matches(d.getGroupId(), d.getArtifactId())) { + if (StringUtils.isBlank(d.getVersion())) { return m; } @@ -216,12 +222,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } } } - for (GradleDependencyConfiguration configuration : gp.getConfiguration(m.getSimpleName()).allExtendsFrom()) { - if (platforms.containsKey(configuration.getName())) { - for (ResolvedPom platform : platforms.get(configuration.getName())) { - String managedVersion = platform.getManagedVersion(d.getGroupId(), d.getArtifactId(), null, d.getRequested().getClassifier()); - if (matchesComparator(managedVersion, d.getVersion())) { - return maybeRemoveVersion(m); + GradleDependencyConfiguration gdc = gp.getConfiguration(m.getSimpleName()); + if (gdc != null) { + for (GradleDependencyConfiguration configuration : gdc.allExtendsFrom()) { + if (platforms.containsKey(configuration.getName())) { + for (ResolvedPom platform : platforms.get(configuration.getName())) { + String managedVersion = platform.getManagedVersion(d.getGroupId(), d.getArtifactId(), null, d.getRequested().getClassifier()); + if (matchesComparator(managedVersion, d.getVersion())) { + return maybeRemoveVersion(m); + } } } }