Skip to content

Commit f7c8f32

Browse files
committed
Avoid possible duplicate key exceptions in toMap() calls
1 parent 810c08c commit f7c8f32

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public List<ResolvedDependency> getDirectResolved() {
102102
if (resolutionContext.isResolveRequired()) {
103103
resolutionContext.resolve();
104104
}
105+
//noinspection ConstantValue
105106
return directResolved == null ? emptyList() : directResolved;
106107
}
107108

@@ -221,7 +222,7 @@ public void resolve() {
221222
// Since we do not support all possible repositories, fall back on leaving the original resolved dependency in place
222223
if (gaToOriginalDirectResolved == null) {
223224
gaToOriginalDirectResolved = directResolved.stream()
224-
.collect(toMap(it -> it.getGav().asGroupArtifact(), it -> it));
225+
.collect(toMap(it -> it.getGav().asGroupArtifact(), it -> it, GradleDependencyConfiguration::newer));
225226
}
226227
// If a new dependency was added but could not be resolved there may be no pre-existing resolved dependency available
227228
// Add a synthetic resolved dependency so that if a
@@ -239,6 +240,30 @@ public void resolve() {
239240
}
240241
}
241242

243+
private static ResolvedDependency newer(ResolvedDependency a, ResolvedDependency b) {
244+
if (!Semver.isVersion(a.getVersion()) || !Semver.isVersion(b.getVersion())) {
245+
// If we can make no meaningful comparison of version numbers then give up and return _something_
246+
return a;
247+
}
248+
String newer = Semver.max(a.getVersion(), b.getVersion());
249+
if (Objects.equals(newer, a.getVersion())) {
250+
return a;
251+
}
252+
return b;
253+
}
254+
255+
private static GradleDependencyConstraint newer(GradleDependencyConstraint a, GradleDependencyConstraint b) {
256+
if (!Semver.isVersion(a.approximateEffectiveVersion()) || !Semver.isVersion(b.approximateEffectiveVersion())) {
257+
// If we can make no meaningful comparison of version numbers then give up and return _something_
258+
return a;
259+
}
260+
String newer = Semver.max(a.approximateEffectiveVersion(), b.approximateEffectiveVersion());
261+
if (Objects.equals(newer, a.approximateEffectiveVersion())) {
262+
return a;
263+
}
264+
return b;
265+
}
266+
242267
/**
243268
* Lists all the constraints in effect for the current configuration, including those constraints inherited from
244269
* parent configurations.
@@ -391,7 +416,7 @@ public static List<GradleDependencyConstraint> merge(@Nullable Collection<Gradle
391416
if (others == null || others.isEmpty()) {
392417
return new ArrayList<>(preferred);
393418
}
394-
Map<GroupArtifact, GradleDependencyConstraint> results = preferred.stream().collect(toMap(it -> new GroupArtifact(it.getGroupId(), it.getArtifactId()), it -> it));
419+
Map<GroupArtifact, GradleDependencyConstraint> results = preferred.stream().collect(toMap(it -> new GroupArtifact(it.getGroupId(), it.getArtifactId()), it -> it, GradleDependencyConfiguration::newer));
395420
for (GradleDependencyConstraint lowerPrecedenceConstraint : others) {
396421
results.putIfAbsent(new GroupArtifact(lowerPrecedenceConstraint.getGroupId(), lowerPrecedenceConstraint.getArtifactId()), lowerPrecedenceConstraint);
397422
}

0 commit comments

Comments
 (0)