Skip to content

Commit b277fab

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 b277fab

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

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

Lines changed: 23 additions & 6 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,32 @@ 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);
89+
90+
Version v1 = highestVersion(c1.getAttributes().get(versionAttribName));
91+
Version v2 = highestVersion(c2.getAttributes().get(versionAttribName));
92+
9493
if (v2 == null)
9594
v2 = Version.emptyVersion;
95+
9696
return v1.compareTo(v2);
9797
}
98+
99+
private static Version highestVersion(Object attr) {
100+
if (attr instanceof Version v) {
101+
return v;
102+
}
103+
if (attr instanceof Collection<?> col) {
104+
// e.g. namespace 'osgi.ee' can contain List<Version>
105+
// see https://osgi.github.io/osgi/core/framework.namespaces.html#framework.namespaces-ee.namespace
106+
// so we compare the highest versions
107+
return col.stream()
108+
.filter(Version.class::isInstance)
109+
.map(Version.class::cast)
110+
.max(Version::compareTo)
111+
.orElse(Version.emptyVersion);
112+
}
113+
return Version.emptyVersion; // null or wrong type
114+
}
98115

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

0 commit comments

Comments
 (0)