Fix version comparator: use numeric comparison with non-numeric segment support (#166) - #179
Open
elharo wants to merge 2 commits into
Open
Fix version comparator: use numeric comparison with non-numeric segment support (#166)#179elharo wants to merge 2 commits into
elharo wants to merge 2 commits into
Conversation
…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
force-pushed
the
fix-version-comparator-v2
branch
from
July 22, 2026 13:42
a2cd8d3 to
1d32c26
Compare
Contributor
There was a problem hiding this comment.
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 useInteger.comparefor segment-count tie-breaks. - Added JUnit tests validating ordering across simple, multi-part, multi-digit, underscore-suffix, and
-eapre-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; | ||
| } |
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; | ||
| } |
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")); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #166
The
version()comparator inToolchainDiscovererusedString.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
String.compareTo()with numeric comparison of the leading digit prefix in each dot-separated version segment. A non-throwing character scan extracts digits (avoidsNumberFormatExceptionoverhead). 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).Testing
All 5 version comparator tests pass. The pre-existing
testDiscoverytest is environment-dependent and unrelated.