Alwaysgaurav1 commented on code in PR #1789:
URL: https://github.com/apache/commons-lang/pull/1789#discussion_r3996975424
##########
src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java:
##########
@@ -1605,6 +1613,8 @@ public static boolean typesSatisfyVariables(final
Map<TypeVariable<?>, Type> typ
return true;
}
+ private static final ThreadLocal<Set<TypeVariable<?>>> VISITING =
ThreadLocal.withInitial(HashSet::new);
Review Comment:
Thanks @garydgregory and Copilot for the review!
Copilot is spot-on. I've addressed both concerns in the latest commit:
1. **Defensive copying in `WildcardTypeImpl`**:
- Cloned `upperBounds` and `lowerBounds` in the `WildcardTypeImpl`
constructor (consistent with `ParameterizedTypeImpl.typeArguments`), preventing
external post-build array mutations from introducing cycles.
2. **Identity-based `Type` recursion guard**:
- Generalized `VISITING` to `ThreadLocal<Set<Type>>` using
`Collections.newSetFromMap(new IdentityHashMap<>())`.
- Placed the guard directly in `TypeUtils.toString(Type)` so all
supported `Type` implementations (`Class`, `ParameterizedType`, `WildcardType`,
`TypeVariable`, `GenericArrayType`) are protected against arbitrary cyclic type
graphs.
- Using reference equality (`==`) avoids evaluating `.hashCode()` /
`.equals()` on potentially recursive or synthetic type structures.
- Any cyclic reference encountered during formatting is rendered with
Commons Lang's standard `(cycle)` marker (e.g. `? extends ? (cycle)`,
`java.util.List<List(cycle)>`, `(cycle)[]`).
3. **Unit Tests**:
- Added tests covering defensive bounds copying on `WildcardTypeBuilder`
as well as safe string formatting for cyclic `WildcardType`,
`ParameterizedType`, and `GenericArrayType`.
--
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]