alhudz commented on code in PR #1807:
URL: https://github.com/apache/commons-lang/pull/1807#discussion_r4178587887
##########
src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java:
##########
@@ -269,11 +271,8 @@ public Type[] getUpperBounds() {
*/
@Override
public int hashCode() {
- int result = 73 << 8;
- result |= Arrays.hashCode(upperBounds);
- result <<= 8;
- result |= Arrays.hashCode(lowerBounds);
- return result;
+ // Same algorithm as the JDK's WildcardType, so that equal
wildcards share a hash code.
+ return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds);
Review Comment:
@garydgregory Reviewed, it's valid, both halves.
`repro:` `TypeUtils.wildcardType().withLowerBounds((Type)
null).withUpperBounds().build().hashCode() == TypeUtils.WILDCARD_ALL.hashCode()`
`expected:` `true`, the two are equal
`actual (2a9ef8e):` `false`, the added assertion fails with `expected:
<640070710> but was: <640070696>`
`fix (ce68c67):` `hashCode()` now hashes the bounds `equals()` compares: a
lone `null` lower bound hashes as the JDK's empty array, and the upper bounds
go through `getImplicitUpperBounds` first
The upper-bound half reproduces as well: `withUpperBounds(Collection.class,
List.class)` equals `withUpperBounds(List.class)` but hashed differently.
`testUnboundedWildcardType` now asserts the hash code and the new
`testWildcardTypeRedundantUpperBound` covers the redundant bound; both fail on
2a9ef8e and pass on ce68c67.
This wasn't introduced by 2a9ef8e, the old shift/OR formula hashed the raw
arrays too, but it belongs with the `hashCode()` change so it's fixed here.
`getLowerBounds()` and `getUpperBounds()` still return what the builder was
given, and the hash code still matches the JDK's for `?`, `? extends String`
and `? super String`.
Local build on Java 21 is green: 89365 tests, 0 failures,
checkstyle/spotbugs/pmd/japicmp clean.
--
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]