Repository navigation
[LANG-1836] Make TypeUtils.containsTypeVariables and unrollVariables honor the owner type of a ParameterizedType - #1812
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Owner traversal introduces stack-overflow cases involving wildcard assignments and cyclic custom owner types.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Addresses LANG-1836 by making TypeUtils account for parameterized owner types.
Changes:
- Checks and unrolls owner types while preserving raw owners.
- Resolves repeated type variables consistently.
- Adds regression tests and a release note.
| File | Description |
|---|---|
| src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java | Adds owner-type and repeated-variable regression tests. |
| src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java | Updates owner traversal and variable resolution. |
| src/changes/changes.xml | Records the LANG-1836 fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Hello @sharanggupta |
|
Hi @garydgregory, Thanks for catching those issues! I've pushed a follow-up commit that addresses both Copilot findings: Guard cyclic owner traversal — Added cycle tracking via a visited set in containsTypeVariables() to prevent StackOverflowError when a ParameterizedType has self-referential or mutually cyclic owners. All 435 TypeUtilsTest tests pass with no regressions. The full suite also completed successfully (exit code 0). The fixes allow: Outer.Inner owner traversal without stack overflow Ready for re-review. |
|
@sharanggupta |
|
Sorry about the failing build, @garydgregory. My follow-up commit left an unused private This time I ran the exact CI command ( The workflows need approval to run on the new commit, whenever you have a moment. |
5f97869 to
f9c9d6a
Compare
|
@sharanggupta |
|
Done: |
|
@sharanggupta |
|
Thanks @garydgregory. I went through Copilot's remaining findings and pushed 2f443bf:
Because I cannot request Copilot reviews myself, I had the diff reviewed adversarially against a sweep of about 29,000 JDK generic types: no new exceptions, and every changed result is more resolved than before. The default |
You cannot request a review in your forked repo? |
|
@garydgregory I tried: I opened a pull request inside my fork and requested Copilot as the reviewer, but no review came back, so my account apparently has no Copilot code review access. I will keep trying to enable it. Until then I run the equivalent myself before pushing: an independent adversarial review of the diff, plus the default |
There was a problem hiding this comment.
Warning
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.
Copilot review overview
7 open findings
Track owner object cycles during type variable unrolling Use identity-backed cycle detection to avoid recursive hashCode calls Guard cyclic owner traversal in type-variable checks Cycle detection incontainsTypeVariablesonly guards traversal viagetOwnerType(). A custom… · New Cycle detection incontainsTypeVariablesonly guards traversal viagetOwnerType(). A custom… · New The updatedunrollBounds(...)signature adds theunrollingparameter, but the Javadoc does not… · NewunrollVariables(...)calls the publiccontainsTypeVariables(type), which now allocates a fresh… · New
🧠 Review effort: Lite
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
|
@sharanggupta |
|
@garydgregory Pulled your Javadoc commit and addressed the remaining Copilot comments in c9b48b4:
Four new tests fail with |
There was a problem hiding this comment.
Warning
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.
Copilot review overview
8 open findings
Track owner object cycles during type variable unrolling Use identity-backed cycle detection to avoid recursive hashCode calls Guard cyclic owner traversal in type-variable checksvisited.remove(var)should be in afinallyblock to ensure the cycle-detection state is… · NewcontainsTypeVariables(type)performs a full recursive walk and is invoked inside… · NewunrollVariables(...)calls the publiccontainsTypeVariables(type), which now allocates a fresh… Cycle detection incontainsTypeVariablesonly guards traversal viagetOwnerType(). A custom… Cycle detection incontainsTypeVariablesonly guards traversal viagetOwnerType(). A custom…
1 resolved since last review
🧠 Review effort: Lite
|
@sharanggupta |
|
@garydgregory Pulled your two commits (aedf448, fc3d1c1) and worked through all of Copilot's findings. Everything is now addressed, the last item in 22693a2:
One behaviour difference to know about: for an invalid custom type where a The default |
There was a problem hiding this comment.
🟡 Changes recommended
The promised LANG-1836 release-note entry is absent from changes.xml.
1 open finding
🧠 Review effort: Balanced
| * Tests, recursively, whether any of the type parameters associated with {@code type} are bound to variables. | ||
| * <p> | ||
| * The owner type of a parameterized type, for example {@code Outer<T>} of {@code Outer<T>.Inner}, is checked as well. |
|
Hello @sharanggupta |
…honour the owner type of a ParameterizedType containsTypeVariables(Type) only inspected getActualTypeArguments() of a ParameterizedType, so Outer<T>.Inner was reported as free of type variables although its Javadoc promises a recursive check of the type's parameterization. Now the owner type is checked as well, unless it is a raw Class (for example Map.class owning Map.Entry<String, Integer>), because the Class branch reports true for any class that merely declares type parameters. unrollVariables(Map, Type) had three related problems in its ParameterizedType branch: - The owner type was never unrolled, so Outer<T>.Inner<Integer> kept the unresolved owner even when T was mapped. The owner is now unrolled like the type arguments (again leaving a raw Class owner untouched) and the result is built with parameterizeWithOwner(unrolledOwner, raw, args). - When an owner was present, getTypeArguments(p) was merged over the caller's map with putAll. For Outer<T>.Inner<T> that injects the identity entry T -> T, which silently discards the caller's T -> String. The merge is dropped: the actual type arguments of p are written in terms of the enclosing scope's variables, which the caller's map is responsible for, not in terms of the raw type's own parameters. - The "visited" guard introduced for LANG-1700 never released a variable after it had been resolved, so a variable occurring more than once was only unrolled the first time (Map<T, T> became Map<String, T>). The variable is now removed from the set after its substitution has been unrolled, which still breaks genuine cycles such as T -> U -> T. Add regression tests for Outer<T>.Inner, Outer<String>.Inner, Map.Entry<String, Integer>, Outer<T>.Inner<Integer>, Outer<T>.Inner<T>, Map.Entry<T, Integer> and Map<T, T>, and a changes.xml entry.
Add cycle tracking to containsTypeVariables() to prevent StackOverflowError
when a ParameterizedType has self-referential or mutually cyclic owners.
Pass the visited set through unrollBounds() to prevent wildcard owner
unrolling from losing cycle state, which caused StackOverflowError with
assignments like {T -> Outer<? extends T>.Inner}.
- containsTypeVariables(): Track visited types during owner traversal
- unrollBounds(): Add overload accepting visited set for cycle preservation
- unrollVariables(): Pass visited set to unrollBounds() for wildcard bounds
… types The previous commit added unrollBounds(Map, Type[], Set) and left the two argument variant without callers, which fails the SpotBugs check (UPM_UNCALLED_PRIVATE_METHOD). Remove the unused private method. Add regression tests for the cycle guards of that commit: a ParameterizedType that is its own owner, and an assignment of T to Outer<? extends T>.Inner. Both fail with a StackOverflowError without the guards.
A custom ParameterizedType can have a cyclic owner chain, or be reached again through its own type arguments, wildcard bounds or owners. Cycle detection must not rely on hashCode() and equals() of such types, and unrolling must not follow a cycle forever. - containsTypeVariables(Type) detects cycles with an identity based set, so it no longer calls the hashCode() of a custom owner type. - unrollVariables(Map, Type) leaves a parameterized type with a cyclic owner chain unchanged and tracks the parameterized types and wildcard types it is currently unrolling by identity. - Add tests for self owned types, owner chains that lead back to the start from either type, and a cycle through a type argument.
Add parameter 'unrolling' to unrollBounds method for recursion prevention. Co-authored-by: Copilot Autofix powered by AI <[email protected]>
…nt cycles - Track every visited composite type by identity, so a custom ParameterizedType, WildcardType or GenericArrayType that reaches itself through its type arguments no longer overflows the stack. - Skip the identity set in unrollVariables for plain type variables and classes, which need no containment check.
Refactor unrollVariables method to use try-finally for visited removal. Co-authored-by: Copilot Autofix powered by AI <[email protected]>
- Drop the containsTypeVariables pre-checks from unrollVariables, which walked the same nested types again at every level. Parameterized and wildcard types are now unrolled once and returned as they are when nothing changed. - A cycle through a type argument and an owner is now left as it is instead of being rejected by the rebuild with an IllegalArgumentException, like the other cyclic cases.
22693a2 to
c66e2ab
Compare
honor the owner type of a ParameterizedType (#1812) Add TypeUtils regression tests for variable unrolling Cover wildcard bound substitution and input preservation, unchanged wildcard identity, self-referential and mutually cyclic variable assignments, and variable detection and substitution across multiple owner levels.
|
@sharanggupta |



Thanks for your contribution to Apache Commons! Your help is appreciated!
Before you push a pull request, review this list:
masterand pass with the change, and the full defaultmvnbuild was run successfully before opening this PR.mvn; that'smvnon the command line by itself.Fixes https://issues.apache.org/jira/browse/LANG-1836
What
TypeUtils.containsTypeVariables(Type)ignoredParameterizedType#getOwnerType(), soOuter<T>.Innerwas reported as containing no type variables, contradicting its Javadoc.TypeUtils.unrollVariables(Map, Type)never unrolled the owner, and when an owner was present it mergedgetTypeArguments(p)over the caller's map withputAll, which forOuter<T>.Inner<T>injects the identity entryT -> Tand discards the caller'sT -> String.How
containsTypeVariables: after the type arguments, also recurse into the owner type, unless it is a rawClass(e.g.Map.classowningMap.Entry<String, Integer>), because theClassbranch returns true for any class that merely declares type parameters.unrollVariables: unroll the type arguments directly against the caller's map (thegetTypeArguments(p)merge is dropped: the actual type arguments are expressed in the enclosing scope's variables, which the caller's map is responsible for, not the raw type's own parameters), unroll a non-Classowner the same way, and rebuild withparameterizeWithOwner(unrolledOwner, raw, args).visitedguard from LANG-1700 is now released once a variable has been resolved, so a variable that occurs more than once in a type is unrolled everywhere (Map<T, T>previously becameMap<String, T>); genuine cycles such asT -> U -> Tare still cut. This was required forOuter<T>.Inner<T>→Outer<String>.Inner<String>. Happy to split it into its own ticket if preferred.Note: on current
master,Outer<T>.Inner<T>no longer overflows the stack as the ticket describes (the LANG-1700 guard already stops that); it returns the type unchanged instead, which the new tests capture.Tests
New
TypeUtilsTestfixtureOuter<T>with inner classes and regression tests that fail onmaster:containsTypeVariables:Outer<T>.Inner→ true,Outer<T>.InnerU<Integer>→ true,Outer<String>.Inner→ false,Map.Entry<String, Integer>→ false.unrollVariableswith{T -> String}:Outer<T>.Inner→Outer<String>.Inner,Outer<T>.InnerU<Integer>→Outer<String>.InnerU<Integer>,Outer<T>.InnerU<T>→Outer<String>.InnerU<String>,Map.Entry<T, Integer>→Map.Entry<String, Integer>(raw owner unchanged),Map<T, T>→Map<String, String>.mvn(default goal) passes: 85,530 tests, 0 Checkstyle violations, PMD and SpotBugs clean, japicmp reports no incompatible changes.