Skip to content

[LANG-1836] Make TypeUtils.containsTypeVariables and unrollVariables honor the owner type of a ParameterizedType - #1812

Merged
garydgregory merged 8 commits into
apache:masterfrom
sharanggupta:LANG-1836-owner-type
Oct 10, 2026
Merged

garydgregory merged 8 commits into
apache:masterfrom
sharanggupta:LANG-1836-owner-type

Conversation

@sharanggupta

@sharanggupta sharanggupta commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute? — Claude Code (Anthropic) assisted in drafting the code change, the regression tests and this description. The result was reviewed, the new tests were confirmed to fail on master and pass with the change, and the full default mvn build was run successfully before opening this PR.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

Fixes https://issues.apache.org/jira/browse/LANG-1836

What

TypeUtils.containsTypeVariables(Type) ignored ParameterizedType#getOwnerType(), so Outer<T>.Inner was reported as containing no type variables, contradicting its Javadoc. TypeUtils.unrollVariables(Map, Type) never unrolled the owner, and when an owner was present it merged getTypeArguments(p) over the caller's map with putAll, which for Outer<T>.Inner<T> injects the identity entry T -> T and discards the caller's T -> String.

How

  • containsTypeVariables: after the type arguments, also recurse into the owner type, unless it is a raw Class (e.g. Map.class owning Map.Entry<String, Integer>), because the Class branch returns true for any class that merely declares type parameters.
  • unrollVariables: unroll the type arguments directly against the caller's map (the getTypeArguments(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-Class owner the same way, and rebuild with parameterizeWithOwner(unrolledOwner, raw, args).
  • The visited guard 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 became Map<String, T>); genuine cycles such as T -> U -> T are still cut. This was required for Outer<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 TypeUtilsTest fixture Outer<T> with inner classes and regression tests that fail on master:

  • containsTypeVariables: Outer<T>.Inner → true, Outer<T>.InnerU<Integer> → true, Outer<String>.Inner → false, Map.Entry<String, Integer> → false.
  • unrollVariables with {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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Owner traversal introduces stack-overflow cases involving wildcard assignments and cyclic custom owner types.

Review effort: Balanced
Findings: 2 High severity

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.

@garydgregory

Copy link
Copy Markdown
Member

Hello @sharanggupta
Please review the Copilot comment.

@sharanggupta

Copy link
Copy Markdown
Contributor Author

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.
Prevent wildcard owner unrolling from losing cycle state — Created an overload of unrollBounds() that accepts and preserves the visited set throughout wildcard bound processing, maintaining cycle detection state.

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
Wildcard cyclic assignments like {T -> Outer<? extends T>.Inner} to unroll safely

Ready for re-review.

@garydgregory

garydgregory commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

@sharanggupta
The build fails. Please follow the instructions in the PR template. Setting to draft.

@garydgregory
garydgregory marked this pull request as draft October 7, 2026 20:21
@sharanggupta

Copy link
Copy Markdown
Contributor Author

Sorry about the failing build, @garydgregory. My follow-up commit left an unused private unrollBounds overload, which SpotBugs flagged (UPM_UNCALLED_PRIVATE_METHOD). I removed it in 5f97869 and added regression tests for the two cycle cases from the Copilot review; both fail with a StackOverflowError without the guards.

This time I ran the exact CI command (mvn --errors --show-version --batch-mode --no-transfer-progress -Ddoclint=all) locally on JDK 8, 17, 21 and 25. All four finish with BUILD SUCCESS, 0 failing tests, 0 Checkstyle violations and 0 SpotBugs bugs, and the RAT, PMD, japicmp and Javadoc steps pass.

The workflows need approval to run on the new commit, whenever you have a moment.

@sharanggupta
sharanggupta marked this pull request as ready for review October 7, 2026 21:07
@garydgregory

Copy link
Copy Markdown
Member

@sharanggupta
Please revert edits to changes.xml.

@sharanggupta

Copy link
Copy Markdown
Contributor Author

Done: changes.xml is back to the upstream version (72a65b3), so the PR now only touches TypeUtils and TypeUtilsTest. The branch is rebased on current master, and the Java CI run passes on all 18 required jobs; only the experimental 28-ea job fails, in the FastDateParser tests, as it does on master.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cyclic owner types can still cause stack overflows during detection and unrolling.

3 open findings
1 resolved since last review

🧠 Review effort: Balanced

@garydgregory

Copy link
Copy Markdown
Member

@sharanggupta
Please review Copilot's comments. Better yet, run Copilot yourself on your PRs so we don't have to go round and round.

@sharanggupta

Copy link
Copy Markdown
Contributor Author

Thanks @garydgregory. I went through Copilot's remaining findings and pushed 2f443bf:

  • cycle detection in containsTypeVariables now uses an identity-based set, so it never calls hashCode() of a custom owner type;
  • unrollVariables leaves a parameterized type with a cyclic owner chain unchanged and tracks the parameterized and wildcard types it is unrolling by identity, so a cycle through owners, type arguments or wildcard bounds cannot overflow the stack;
  • five new tests cover self-owned types (including one with a structural hashCode()), an owner chain that leads back to the start from either type, and a cycle through a type argument; they fail with a StackOverflowError before this change.

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 mvn build with -Ddoclint=all passes locally on JDK 8, 17, 21 and 25. Could you approve the workflow run for the new commit?

@garydgregory

Copy link
Copy Markdown
Member

Because I cannot request Copilot reviews myself,

You cannot request a review in your forked repo?

@sharanggupta

Copy link
Copy Markdown
Contributor Author

@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 mvn build with -Ddoclint=all on JDK 8, 17, 21 and 25. The Java CI run on the current head (2f443bf) passes; only the optional 28-ea job is red, in the FastDateParser tests, as on master.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

🧠 Review effort: Lite


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

@garydgregory

Copy link
Copy Markdown
Member

@sharanggupta
I accepted a Copilot Javadoc fix to this PR. Pull to your local branch. Please review Copilot comments.

@garydgregory
garydgregory marked this pull request as draft October 8, 2026 12:41
@sharanggupta

Copy link
Copy Markdown
Contributor Author

@garydgregory Pulled your Javadoc commit and addressed the remaining Copilot comments in c9b48b4:

  • containsTypeVariables now tracks every visited composite type by identity, so cycles through type arguments (not only owners) are cut without calling equals()/hashCode().
  • unrollVariables no longer allocates a set for plain type variables and classes.
  • The @param unrolling Javadoc is covered by your commit.

Four new tests fail with StackOverflowError before the change. The default mvn build with -Ddoclint=all passes locally on JDK 8, 17, 21 and 25 (tests, Checkstyle, SpotBugs). The workflows need your approval to run on the new head.

@garydgregory
garydgregory requested a balanced review from Copilot October 9, 2026 11:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garydgregory

Copy link
Copy Markdown
Member

@sharanggupta
I committed a Copilot suggestion, please pull to your local branch and review Copilot's other comments.

@sharanggupta

sharanggupta commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@garydgregory Pulled your two commits (aedf448, fc3d1c1) and worked through all of Copilot's findings. Everything is now addressed, the last item in 22693a2:

  • Owner cycles, identity-based cycle detection and type-argument cycles: 2f443bf and c9b48b4, each with tests that fail without them.
  • The @param unrolling Javadoc and visited.remove in a finally block: your aedf448 and fc3d1c1.
  • The re-traversal finding: 22693a2 removes the containsTypeVariables pre-checks from unrollVariables. Types are now unrolled in a single pass and returned as they are when nothing changed.

One behaviour difference to know about: for an invalid custom type where a Map's argument is owned by that same Map, unrolling used to end in an IllegalArgumentException, but only because every type was rebuilt. The cycle is now left as it is, like the other cyclic cases, and I updated that test.

The default mvn -Ddoclint=all build passes locally on JDK 8, 17, 21 and 25, including tests, Checkstyle and SpotBugs. I resolved the Copilot threads that are addressed. The workflows for 22693a2 need your approval, and from my side this is ready whenever you want to take it out of draft.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The promised LANG-1836 release-note entry is absent from changes.xml.

1 open finding

🧠 Review effort: Balanced

Comment on lines 373 to +375
* 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.
@garydgregory

Copy link
Copy Markdown
Member

Hello @sharanggupta
Thank you for the update.
Please rebase on git master (or merge git master into this branch).

sharanggupta and others added 8 commits October 10, 2026 18:42
…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation matches the stated behavior and includes comprehensive regression and cycle-safety coverage.

1 open finding

🧠 Review effort: Balanced

@garydgregory
garydgregory marked this pull request as ready for review October 10, 2026 19:22
@garydgregory
garydgregory merged commit 48bf95f into apache:master Oct 10, 2026
22 of 23 checks passed
@garydgregory garydgregory changed the title [LANG-1836] Make TypeUtils.containsTypeVariables and unrollVariables honour the owner type of a ParameterizedType [LANG-1836] Make TypeUtils.containsTypeVariables and unrollVariables honor the owner type of a ParameterizedType Oct 10, 2026
garydgregory added a commit that referenced this pull request Oct 10, 2026
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.
@garydgregory

Copy link
Copy Markdown
Member

@sharanggupta
Merged 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants