Skip to content

Commit 7f5b317

Browse files
committed
fix ClassCastException in CapReqComparator
- reason was that the namespace 'osgi.ee' can contain List<Version> - so we now handle cases of single Version vs. List<Version> - for lists we take the highest version for comparison
1 parent 26523bf commit 7f5b317

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

ui/org.eclipse.pde.bnd.ui/src/org/eclipse/pde/bnd/ui/model/resolution/CapReqComparator.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*******************************************************************************/
1616
package org.eclipse.pde.bnd.ui.model.resolution;
1717

18+
import java.util.Collection;
1819
import java.util.Comparator;
1920

2021
import org.eclipse.pde.bnd.ui.model.resource.R5LabelFormatter;
@@ -85,16 +86,29 @@ private int compareCapToCap(Capability c1, Capability c2) {
8586
String versionAttribName = R5LabelFormatter.getVersionAttributeName(ns1);
8687
if (versionAttribName == null)
8788
return 0;
88-
Version v1 = (Version) c1.getAttributes()
89-
.get(versionAttribName);
90-
if (v1 == null)
91-
v1 = Version.emptyVersion;
92-
Version v2 = (Version) c2.getAttributes()
93-
.get(versionAttribName);
94-
if (v2 == null)
95-
v2 = Version.emptyVersion;
89+
90+
Version v1 = highestVersion(c1.getAttributes().get(versionAttribName));
91+
Version v2 = highestVersion(c2.getAttributes().get(versionAttribName));
92+
9693
return v1.compareTo(v2);
9794
}
95+
96+
private static Version highestVersion(Object attr) {
97+
if (attr instanceof Version v) {
98+
return v;
99+
}
100+
if (attr instanceof Collection<?> col) {
101+
// e.g. namespace 'osgi.ee' can contain List<Version>
102+
// see https://osgi.github.io/osgi/core/framework.namespaces.html#framework.namespaces-ee.namespace
103+
// so we compare the highest versions
104+
return col.stream()
105+
.filter(Version.class::isInstance)
106+
.map(Version.class::cast)
107+
.max(Version::compareTo)
108+
.orElse(Version.emptyVersion);
109+
}
110+
return Version.emptyVersion; // null or wrong type
111+
}
98112

99113
private int compareReqToReq(Requirement r1, Requirement r2) {
100114
return ResourceUtils.REQUIREMENT_COMPARATOR.compare(r1, r2);

0 commit comments

Comments
 (0)