codingkiddo opened a new pull request, #16236:
URL: https://github.com/apache/dubbo/pull/16236

   ### What is changed
   
   
   This PR updates `CollectionUtils.sort` to use a bounded generic type:
   
   ```java
   <T extends Comparable<? super T>>
   ```
   To 
   
   ```java
   public static <T extends Comparable<? super T>> List<T> sort(List<T> list)
   ```
   
   The raw cast is also removed:
   
   ```java
   Collections.sort((List) list);
   ```
   
   to 
   
   ```java
   Collections.sort(list);
   ```
   
   
   ## Why
   
   CollectionUtils.sort currently accepts any List<T> 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:
   
   
   ```java
   <T extends Comparable<? super T>>
   ```
   
   
   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
   
   - Improves compile-time type safety
   - Removes raw type usage
   - Removes unchecked/raw warning suppression
   - Preserves the original return type as List<T>
   - Aligns the method with Java collection sorting conventions
   - Supports inherited comparison cases, for example when a subtype inherits 
Comparable<SuperType>
   
   ## 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<Parent>
   
   Fixes #16235
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to