Skip to content

Commit

Permalink
Fix incomplete boolean simplification of ternary operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 6, 2024
1 parent c339007 commit c87e8d3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.gradle;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
Expand Down Expand Up @@ -176,14 +177,15 @@ private static boolean isLikelyDependencyConfiguration(Cursor cursor) {
public TreeVisitor<?, ExecutionContext> getScanner(DependencyVersionState acc) {

return new GroovyVisitor<ExecutionContext>() {
@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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -300,8 +304,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(DependencyVersionState acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -359,16 +365,14 @@ public org.openrewrite.properties.tree.Properties visitEntry(Properties.Entry en
@RequiredArgsConstructor
private class UpdateGroovy extends GroovyVisitor<ExecutionContext> {
final DependencyVersionState acc;
@Nullable
GradleProject gradleProject;
final DependencyMatcher dependencyMatcher = new DependencyMatcher(groupId, artifactId, null);

@Override
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);
}

Expand All @@ -377,7 +381,7 @@ public J postVisit(J tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) tree;
Map<String, Map<GroupArtifact, Set<String>>> variableNames = getCursor().getMessage(VERSION_VARIABLE_KEY);
if (variableNames != null && gradleProject != null) {
if (variableNames != null) {
cu = (JavaSourceFile) new UpdateVariable(variableNames, gradleProject).visitNonNull(cu, ctx);
}
Map<GroupArtifactVersion, Set<String>> versionUpdates = getCursor().getMessage(NEW_VERSION_KEY);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -552,9 +556,11 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution
}
}

@RequiredArgsConstructor
@AllArgsConstructor
private class UpdateVariable extends GroovyIsoVisitor<ExecutionContext> {
private final Map<String, Map<GroupArtifact, Set<String>>> versionVariableNames;

@Nullable
private final GradleProject gradleProject;

@Override
Expand Down Expand Up @@ -666,6 +672,7 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct

public static GradleProject replaceVersion(GradleProject gp, ExecutionContext ctx, GroupArtifactVersion gav, Set<String> configurations) {
try {
//noinspection ConstantValue
if (gav.getGroupId() == null || gav.getArtifactId() == null) {
return gp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
"""
)
);
}
}

0 comments on commit c87e8d3

Please sign in to comment.