Improve generic type safety of CollectionUtils.sort#16236
Open
codingkiddo wants to merge 4 commits intoapache:3.3from
Open
Improve generic type safety of CollectionUtils.sort#16236codingkiddo wants to merge 4 commits intoapache:3.3from
codingkiddo wants to merge 4 commits intoapache:3.3from
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16236 +/- ##
============================================
+ Coverage 60.78% 60.79% +0.01%
- Complexity 11763 11768 +5
============================================
Files 1953 1953
Lines 89186 89186
Branches 13454 13454
============================================
+ Hits 54209 54225 +16
+ Misses 29400 29392 -8
+ Partials 5577 5569 -8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
zrlw
approved these changes
Apr 29, 2026
Contributor
|
LGTM |
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.
What is changed
This PR updates
CollectionUtils.sortto use a bounded generic type:To
The raw cast is also removed:
to
Why
CollectionUtils.sort currently accepts any List and relies on a raw cast with suppressed warnings.
This means callers can pass a list whose elements do not implement Comparable, and the code still compiles. However, sorting such a list can fail at runtime.
By using:
the method now follows the same generic constraint style as java.util.Collections.sort.
This ensures that only naturally comparable elements can be passed to the method.
Benefit
Compatibility
The erased method signature remains sort(List), so this change should be binary-compatible.
However, this may be source-incompatible for callers that currently pass non-comparable element types to CollectionUtils.sort. Those usages would already be unsafe because they may fail at runtime.
Tests
Added/updated tests to verify:
Sorting a normal comparable list
Sorting a subtype whose parent implements Comparable
Fixes #16235