From c87e8d32365985a9f18eea2b4cf7c6ba5449a69f Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Fri, 6 Sep 2024 16:31:21 -0700 Subject: [PATCH] Fix incomplete boolean simplification of ternary operators. --- .../gradle/UpgradeDependencyVersion.java | 45 +++++++++++-------- .../gradle/UpgradeDependencyVersionTest.java | 42 +++++++++++++++++ 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java index 3f0b19d8f10..2bc3e2216e6 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java @@ -15,6 +15,7 @@ */ package org.openrewrite.gradle; +import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; import lombok.Value; @@ -176,14 +177,15 @@ private static boolean isLikelyDependencyConfiguration(Cursor cursor) { public TreeVisitor getScanner(DependencyVersionState acc) { return new GroovyVisitor() { + @Nullable GradleProject gradleProject; @Override public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) { - gradleProject = cu.getMarkers().findFirst(GradleProject.class).orElse(null); - if (gradleProject == null) { + if (!cu.getSourcePath().toString().endsWith(".gradle")) { return cu; } + gradleProject = cu.getMarkers().findFirst(GradleProject.class).orElse(null); return super.visitCompilationUnit(cu, ctx); } @@ -252,8 +254,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) try { String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null) .select(new GroupArtifact(groupId, artifactId), m.getSimpleName(), newVersion, versionPattern, ctx); - acc.versionPropNameToGA.put(versionVariableName, ga); - acc.gaToNewVersion.put(ga, resolvedVersion); + acc.versionPropNameToGA.put(requireNonNull(versionVariableName), ga); + acc.gaToNewVersion.put(ga, requireNonNull(resolvedVersion)); } catch (MavenDownloadingException e) { acc.gaToNewVersion.put(ga, e); return m; @@ -283,8 +285,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) try { String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null) .select(new GroupArtifact(dep.getGroupId(), dep.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx); - acc.versionPropNameToGA.put(versionVariableName, ga); - acc.gaToNewVersion.put(ga, resolvedVersion); + if (resolvedVersion != null) { + acc.versionPropNameToGA.put(versionVariableName, ga); + acc.gaToNewVersion.put(ga, resolvedVersion); + } } catch (MavenDownloadingException e) { acc.gaToNewVersion.put(ga, e); } @@ -300,8 +304,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) @Override public TreeVisitor getVisitor(DependencyVersionState acc) { return new TreeVisitor() { - private UpdateGroovy updateGroovy = new UpdateGroovy(acc); - private UpdateProperties updateProperties = new UpdateProperties(acc); + private final UpdateGroovy updateGroovy = new UpdateGroovy(acc); + private final UpdateProperties updateProperties = new UpdateProperties(acc); @Override public boolean isAcceptable(SourceFile sf, ExecutionContext ctx) { @@ -310,11 +314,13 @@ public boolean isAcceptable(SourceFile sf, ExecutionContext ctx) { @Override public @Nullable Tree visit(@Nullable Tree t, ExecutionContext ctx) { - SourceFile sf = (SourceFile) t; - if (updateProperties.isAcceptable(sf, ctx)) { - t = updateProperties.visitNonNull(t, ctx); - } else if (updateGroovy.isAcceptable(sf, ctx)) { - t = updateGroovy.visitNonNull(t, ctx); + if (t instanceof SourceFile) { + SourceFile sf = (SourceFile) t; + if (updateProperties.isAcceptable(sf, ctx)) { + t = updateProperties.visitNonNull(t, ctx); + } else if (updateGroovy.isAcceptable(sf, ctx)) { + t = updateGroovy.visitNonNull(t, ctx); + } } return t; } @@ -359,6 +365,7 @@ public org.openrewrite.properties.tree.Properties visitEntry(Properties.Entry en @RequiredArgsConstructor private class UpdateGroovy extends GroovyVisitor { final DependencyVersionState acc; + @Nullable GradleProject gradleProject; final DependencyMatcher dependencyMatcher = new DependencyMatcher(groupId, artifactId, null); @@ -366,9 +373,6 @@ private class UpdateGroovy extends GroovyVisitor { public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) { gradleProject = cu.getMarkers().findFirst(GradleProject.class) .orElse(null); - if (gradleProject == null) { - return cu; - } return super.visitCompilationUnit(cu, ctx); } @@ -377,7 +381,7 @@ public J postVisit(J tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) tree; Map>> variableNames = getCursor().getMessage(VERSION_VARIABLE_KEY); - if (variableNames != null && gradleProject != null) { + if (variableNames != null) { cu = (JavaSourceFile) new UpdateVariable(variableNames, gradleProject).visitNonNull(cu, ctx); } Map> versionUpdates = getCursor().getMessage(NEW_VERSION_KEY); @@ -505,7 +509,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution if (!dependencyMatcher.matches((String) groupLiteral.getValue(), (String) artifactLiteral.getValue())) { return m; } - Object scanResult = acc.gaToNewVersion.get(new GroupArtifact((String) groupLiteral.getValue(), (String) artifactLiteral.getValue())); + Object scanResult = acc.gaToNewVersion.get(new GroupArtifact((String) requireNonNull(groupLiteral.getValue()), (String) artifactLiteral.getValue())); if (scanResult instanceof Exception) { return Markup.warn(m, (Exception) scanResult); } @@ -552,9 +556,11 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution } } - @RequiredArgsConstructor + @AllArgsConstructor private class UpdateVariable extends GroovyIsoVisitor { private final Map>> versionVariableNames; + + @Nullable private final GradleProject gradleProject; @Override @@ -666,6 +672,7 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct public static GradleProject replaceVersion(GradleProject gp, ExecutionContext ctx, GroupArtifactVersion gav, Set configurations) { try { + //noinspection ConstantValue if (gav.getGroupId() == null || gav.getArtifactId() == null) { return gp; } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java index c7aaf02f417..84357fa5952 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java @@ -994,4 +994,46 @@ void exactVersionWithRegexPattern() { ) ); } + + @Test + void dependenciesBlockInFreestandingScript() { + rewriteRun( + spec -> spec.recipe(new UpgradeDependencyVersion("com.fasterxml.jackson.core", "jackson-databind", "2.17.2", null)), + buildGradle( + """ + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + dependencies { + implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2") + } + """, + """ + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + dependencies { + implementation("com.fasterxml.jackson.core:jackson-databind:2.17.2") + } + """, + spec -> spec.path("dependencies.gradle") + ), + buildGradle( + """ + plugins { + id("java") + } + apply from: 'dependencies.gradle' + """ + ) + ); + } }