Skip to content

Commit 5e9803a

Browse files
authored
Support dependency Map notation for version reference in Settings (#5912)
1 parent 02153e8 commit 5e9803a

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

Lines changed: 12 additions & 2 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());
@@ -624,6 +630,10 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ex
624630
return a;
625631
}
626632
Map<GroupArtifact, Set<String>> groupArtifactSetMap = acc.versionPropNameToGA.get("gradle." + a.getVariable());
633+
// Guard to ensure that an unsupported notation doesn't throw an exception
634+
if (groupArtifactSetMap == null) {
635+
return a;
636+
}
627637
GroupArtifact ga = groupArtifactSetMap.entrySet().stream().findFirst().map(Map.Entry::getKey).orElse(null);
628638
if (ga == null) {
629639
return a;

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

Lines changed: 10 additions & 4 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
}

0 commit comments

Comments
 (0)