Skip to content

Fix version comparator: use numeric comparison with non-numeric segment support (#166) - #179

Open
elharo wants to merge 2 commits into
masterfrom
fix-version-comparator-v2
Open

Fix version comparator: use numeric comparison with non-numeric segment support (#166)#179
elharo wants to merge 2 commits into
masterfrom
fix-version-comparator-v2

Conversation

@elharo

@elharo elharo commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #166

The version() comparator in ToolchainDiscoverer used String.compareTo() for version segment comparison, which produces incorrect ordering for versions with different digit counts (e.g. "8" was sorted before "11" because '8' > '1' lexicographically).

Changes

  • ToolchainDiscoverer.java: Replace lexicographic String.compareTo() with numeric comparison of the leading digit prefix in each dot-separated version segment. A non-throwing character scan extracts digits (avoids NumberFormatException overhead). Segments with non-numeric suffixes (e.g. 11-ea, 1.8.0_202, 17.0.2+8) are compared by leading digit prefix first; if equal, the segment without a suffix is treated as greater (release > pre-release).
  • ToolchainDiscovererTest.java: 5 tests covering:
    • Simple single-number versions (8, 11, 17)
    • Multi-part versions (11.0.1 vs 11.0.31 vs 17.0.1 vs 1.8)
    • Multi-digit segments (17.0.2 vs 17.0.10)
    • Non-numeric underscore suffixes (1.8.0_202 vs 1.8.0_121)
    • EA/pre-release suffixes (11-ea vs 11)

Testing

All 5 version comparator tests pass. The pre-existing testDiscovery test is environment-dependent and unrelated.

…nt support (#166)

- Replace String.compareTo() with numeric comparison of leading digit
  prefix in each version segment
- Use non-throwing character scan instead of parseInt for performance
- Handle non-numeric suffixes (e.g. 1.8.0_202, 17.0.2+8, 11-ea) by
  comparing leading digit prefix first, then treating segments with
  suffixes as lower than clean numeric segments
- Add tests: simple versions, multi-part, multi-digit segments,
  underscore suffixes, and EA/pre-release variants
@elharo
elharo force-pushed the fix-version-comparator-v2 branch from a2cd8d3 to 1d32c26 Compare July 22, 2026 13:42
@elharo
elharo requested a review from Copilot July 22, 2026 13:49
@elharo elharo added bug Something isn't working priority:major Major loss of function labels Jul 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes incorrect JDK toolchain version ordering by replacing lexicographic segment comparison with numeric segment comparison that also treats “release” segments as greater than “pre-release” suffix variants.

Changes:

  • Updated ToolchainDiscoverer#version() to compare dot-separated segments numerically (with suffix detection) and use Integer.compare for segment-count tie-breaks.
  • Added JUnit tests validating ordering across simple, multi-part, multi-digit, underscore-suffix, and -ea pre-release versions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java Implements numeric version segment comparison with suffix handling to fix ordering (e.g., 11 > 8).
src/test/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscovererTest.java Adds focused unit tests covering key version ordering scenarios for the updated comparator.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +383 to +395
private static int compareVersionSegments(String sa, String sb) {
int na = parseVersionSegment(sa);
int nb = parseVersionSegment(sb);
if (na != nb) {
return Integer.compare(na, nb);
}
boolean suffixA = hasSuffix(sa);
boolean suffixB = hasSuffix(sb);
if (suffixA != suffixB) {
return suffixA ? -1 : 1;
}
return 0;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot done

Comment on lines +407 to +417
private static int parseVersionSegment(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') {
break;
}
n = n * 10 + (c - '0');
}
return n;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot done

Comment on lines +94 to +106
@Test
void testVersionComparatorMultiDigitSegments() {
ToolchainDiscoverer discoverer = new ToolchainDiscoverer();

List<ToolchainModel> list = new ArrayList<>();
list.add(toolchain("17.0.2"));
list.add(toolchain("17.0.10"));

list.sort(discoverer.version());

assertEquals("17.0.10", list.get(0).getProvides().getProperty("version"));
assertEquals("17.0.2", list.get(1).getProvides().getProperty("version"));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority:major Major loss of function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Version comparator in ToolchainDiscoverer uses lexicographic String.compareTo instead of numeric version comparison

2 participants