Skip to content

Commit 5fbcf3a

Browse files
authored
Merge branch 'openrewrite:main' into main
2 parents c6a7bd7 + 6dbad84 commit 5fbcf3a

13 files changed

Lines changed: 4291 additions & 244 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,17 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
200200
} else if (arg.getValue() instanceof J.Identifier) {
201201
J.Identifier value = (J.Identifier) arg.getValue();
202202
valueValue = value.getSimpleName();
203+
} else if (arg.getValue() instanceof J.FieldAccess) {
204+
valueValue = arg.getValue().printTrimmed(getCursor());
203205
} else if (arg.getValue() instanceof G.GString) {
204206
G.GString value = (G.GString) arg.getValue();
205207
List<J> strings = value.getStrings();
206208
if (!strings.isEmpty() && strings.get(0) instanceof G.GString.Value) {
207209
G.GString.Value versionGStringValue = (G.GString.Value) strings.get(0);
208210
if (versionGStringValue.getTree() instanceof J.Identifier) {
209211
valueValue = ((J.Identifier) versionGStringValue.getTree()).getSimpleName();
212+
} else if (versionGStringValue.getTree() instanceof J.FieldAccess) {
213+
valueValue = versionGStringValue.getTree().printTrimmed(getCursor());
210214
}
211215
}
212216
}
@@ -454,11 +458,13 @@ private void gatherVariables(J.MethodInvocation method) {
454458
artifactValue = ((J.Assignment) depArgs.get(1)).getAssignment();
455459
versionExp = ((J.Assignment) depArgs.get(2)).getAssignment();
456460
}
457-
if (groupValue instanceof J.Literal && artifactValue instanceof J.Literal && versionExp instanceof J.Identifier) {
461+
if (groupValue instanceof J.Literal && artifactValue instanceof J.Literal && (versionExp instanceof J.Identifier || versionExp instanceof J.FieldAccess)) {
458462
J.Literal groupLiteral = (J.Literal) groupValue;
459463
J.Literal artifactLiteral = (J.Literal) artifactValue;
460464
if (groupLiteral.getValue() instanceof String && artifactLiteral.getValue() instanceof String && shouldResolveVersion((String) groupLiteral.getValue(), (String) artifactLiteral.getValue())) {
461-
String versionVariableName = ((J.Identifier) versionExp).getSimpleName();
465+
String versionVariableName = versionExp instanceof J.Identifier ?
466+
((J.Identifier) versionExp).getSimpleName() :
467+
(versionExp).printTrimmed(getCursor());
462468
acc.variableNames.computeIfAbsent(versionVariableName, it -> new HashMap<>())
463469
.computeIfAbsent(new GroupArtifact((String) groupLiteral.getValue(), (String) artifactLiteral.getValue()), it -> new HashSet<>())
464470
.add(method.getSimpleName());
@@ -551,7 +557,7 @@ public org.openrewrite.properties.tree.Properties visitEntry(Properties.Entry en
551557
if (result == null || result instanceof Exception) {
552558
return entry;
553559
}
554-
VersionComparator versionComparator = Semver.validate(StringUtils.isBlank(newVersion) ? "latest.release" : newVersion, versionPattern).getValue();
560+
VersionComparator versionComparator = getVersionComparator();
555561
if (versionComparator == null) {
556562
return entry;
557563
}
@@ -562,6 +568,10 @@ public org.openrewrite.properties.tree.Properties visitEntry(Properties.Entry en
562568
}
563569
}
564570

571+
private @Nullable VersionComparator getVersionComparator() {
572+
return Semver.validate(StringUtils.isBlank(newVersion) ? "latest.release" : newVersion, versionPattern).getValue();
573+
}
574+
565575
@RequiredArgsConstructor
566576
private class UpdateGradle extends JavaVisitor<ExecutionContext> {
567577
final DependencyVersionState acc;
@@ -624,6 +634,10 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ex
624634
return a;
625635
}
626636
Map<GroupArtifact, Set<String>> groupArtifactSetMap = acc.versionPropNameToGA.get("gradle." + a.getVariable());
637+
// Guard to ensure that an unsupported notation doesn't throw an exception
638+
if (groupArtifactSetMap == null) {
639+
return a;
640+
}
627641
GroupArtifact ga = groupArtifactSetMap.entrySet().stream().findFirst().map(Map.Entry::getKey).orElse(null);
628642
if (ga == null) {
629643
return a;
@@ -639,6 +653,17 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ex
639653
if (J.Literal.isLiteralValue(l, newVersion)) {
640654
return a;
641655
}
656+
657+
VersionComparator versionComparator = getVersionComparator();
658+
if (versionComparator != null) {
659+
String currentVersion = (String) l.getValue();
660+
Optional<String> finalVersion = versionComparator.upgrade(currentVersion, singletonList(newVersion));
661+
if (!finalVersion.isPresent()) {
662+
// Would be a downgrade, don't change
663+
return a;
664+
}
665+
}
666+
642667
String quote = l.getValueSource() == null ? "\"" : l.getValueSource().substring(0, 1);
643668
return a.withAssignment(l.withValue(newVersion).withValueSource(quote + newVersion + quote));
644669
}

rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleDependencyConfiguration.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
import org.jspecify.annotations.Nullable;
2424
import org.openrewrite.internal.StringUtils;
2525
import org.openrewrite.maven.attributes.Attributed;
26-
import org.openrewrite.maven.tree.*;
26+
import org.openrewrite.maven.tree.Dependency;
27+
import org.openrewrite.maven.tree.GroupArtifact;
28+
import org.openrewrite.maven.tree.ResolvedDependency;
29+
import org.openrewrite.maven.tree.Version;
2730

2831
import java.io.Serializable;
2932
import java.util.*;
3033
import java.util.stream.Collectors;
3134

3235
import static java.util.Collections.emptyList;
3336
import static java.util.Collections.emptyMap;
37+
import static java.util.stream.Collectors.toMap;
3438

3539
@SuppressWarnings("unused")
3640
@Value
@@ -54,8 +58,8 @@ public class GradleDependencyConfiguration implements Serializable, Attributed {
5458
boolean isCanBeResolved;
5559

5660
/**
57-
* Indicates that this configuration is intended for exposing artifacts outside this project. A consumable configuration should not be declarable or resolvable.
58-
* See <a href="https://docs.gradle.org/current/userguide/declaring_configurations.html#sec:configuration-flags-roles">Configuration flag roles</a>
61+
* Indicates that this configuration is intended for exposing artifacts outside this project. A consumable configuration should not be declarable or resolvable.
62+
* See <a href="https://docs.gradle.org/current/userguide/declaring_configurations.html#sec:configuration-flags-roles">Configuration flag roles</a>
5963
*/
6064
boolean isCanBeConsumed;
6165

@@ -205,7 +209,7 @@ public List<GradleDependencyConfiguration> allExtendsFrom() {
205209
public @Nullable Dependency findRequestedDependency(String groupId, String artifactId) {
206210
for (Dependency d : requested) {
207211
if (StringUtils.matchesGlob(d.getGav().getGroupId(), groupId) &&
208-
StringUtils.matchesGlob(d.getGav().getArtifactId(), artifactId)) {
212+
StringUtils.matchesGlob(d.getGav().getArtifactId(), artifactId)) {
209213
return d;
210214
}
211215
}
@@ -265,7 +269,7 @@ public static List<GradleDependencyConstraint> merge(@Nullable Collection<Gradle
265269
if (others == null || others.isEmpty()) {
266270
return new ArrayList<>(preferred);
267271
}
268-
Map<GroupArtifact, GradleDependencyConstraint> results = preferred.stream().collect(Collectors.toMap(it -> new GroupArtifact(it.getGroupId(), it.getArtifactId()), it -> it));
272+
Map<GroupArtifact, GradleDependencyConstraint> results = preferred.stream().collect(toMap(it -> new GroupArtifact(it.getGroupId(), it.getArtifactId()), it -> it));
269273
for (GradleDependencyConstraint lowerPrecedenceConstraint : others) {
270274
results.putIfAbsent(new GroupArtifact(lowerPrecedenceConstraint.getGroupId(), lowerPrecedenceConstraint.getArtifactId()), lowerPrecedenceConstraint);
271275
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,8 +1953,14 @@ void ignoreVersionDefinedInExtraPropertiesWithConcatenatedReferenceInDependencie
19531953
);
19541954
}
19551955

1956-
@Test
1957-
void upgradeVersionInSettingsGradleExt() {
1956+
@ParameterizedTest
1957+
@ValueSource(strings = {
1958+
"\"com.fasterxml.jackson.core:jackson-databind:${gradle.jackson}\"",
1959+
"group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: gradle.jackson",
1960+
"group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: \"$gradle.jackson\"",
1961+
"group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: \"${gradle.jackson}\""
1962+
})
1963+
void upgradeVersionInSettingsGradleExt(String dependencyNotation) {
19581964
rewriteRun(
19591965
spec -> spec.recipe(new UpgradeDependencyVersion("com.fasterxml.jackson.core", "jackson-databind", "2.15.0", null)),
19601966
settingsGradle(
@@ -1980,9 +1986,9 @@ void upgradeVersionInSettingsGradleExt() {
19801986
}
19811987
19821988
dependencies {
1983-
implementation "com.fasterxml.jackson.core:jackson-databind:${gradle.jackson}"
1989+
implementation %s
19841990
}
1985-
"""
1991+
""".formatted(dependencyNotation)
19861992
)
19871993
);
19881994
}
@@ -2028,7 +2034,7 @@ void doesNotDowngradeBuildscriptDependencyVersion() {
20282034
}
20292035

20302036
@Test
2031-
void doesNotDowngradeVersionInSettingsGradleExt() {
2037+
void doesNotDowngradeRegularDependencyVersionInSettingsGradleExt() {
20322038
rewriteRun(
20332039
spec -> spec.recipe(new UpgradeDependencyVersion("com.fasterxml.jackson.core", "jackson-databind", "2.13.2", null)),
20342040
settingsGradle(
@@ -2055,4 +2061,30 @@ void doesNotDowngradeVersionInSettingsGradleExt() {
20552061
)
20562062
);
20572063
}
2064+
2065+
@Test
2066+
void doesNotDowngradeBuildscriptDependencyVersionInSettingsGradleExt() {
2067+
rewriteRun(
2068+
spec -> spec.recipe(new UpgradeDependencyVersion("com.fasterxml.jackson.core", "jackson-databind", "2.13.2", null)),
2069+
settingsGradle(
2070+
"""
2071+
gradle.ext {
2072+
jackson = '2.13.3'
2073+
}
2074+
"""
2075+
),
2076+
buildGradle(
2077+
"""
2078+
buildscript {
2079+
repositories {
2080+
mavenCentral()
2081+
}
2082+
dependencies {
2083+
classpath "com.fasterxml.jackson.core:jackson-databind:${gradle.jackson}"
2084+
}
2085+
}
2086+
"""
2087+
)
2088+
);
2089+
}
20582090
}

rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private static boolean valueMatches(@Nullable Expression expression, @Nullable S
340340
return oldAttributeValue.equals(((J.Literal) expression).getValue());
341341
} else if (expression instanceof J.FieldAccess) {
342342
J.FieldAccess fa = (J.FieldAccess) expression;
343-
if (TypeUtils.isAssignableTo("java.lang.String", fa.getType())) {
343+
if (!(fa.getTarget() instanceof J.Identifier)) {
344344
return oldAttributeValue.equals(fa.toString());
345345
}
346346
String currentValue = ((J.Identifier) fa.getTarget()).getSimpleName() + "." + fa.getSimpleName();

0 commit comments

Comments
 (0)