Skip to content

Forward visit methods declared on a delegate superclass under adapt()#8172

Merged
jkschneider merged 1 commit into
mainfrom
fix/adapter-superclass-visit-methods
Jul 3, 2026
Merged

Forward visit methods declared on a delegate superclass under adapt()#8172
jkschneider merged 1 commit into
mainfrom
fix/adapter-superclass-visit-methods

Conversation

@jkschneider

Copy link
Copy Markdown
Member

What's changed

TreeVisitorAdapter collected the visit-like methods to proxy-forward from delegate.getClass().getDeclaredMethods() — the leaf class only. So preVisit/postVisit/visitX declared on a superclass of the delegate were never forwarded, and silently did not fire when the visitor was adapted to another language (C#/Python/Kotlin/Groovy). Java is unaffected because it runs natively, without adapt() — which is why this stayed hidden.

The fix walks the delegate's class hierarchy from the leaf up to — but not into — the language iso-visitor (delegateType), collecting the user's visit-like methods at every level (leaf-first, so the most-derived override wins). Stopping at the iso-visitor matters: crossing into it would forward the framework's own visitX to the foreign-language delegate and break native traversal.

Why it matters

Any visitor that factors shared behavior into a base class (e.g. a common scanning base that overrides preVisit/postVisit) misbehaves under cross-language adapt() today, with no error — the overrides just don't run.

Tests

Two regression tests in TreeVisitorAdapterTest:

  • preVisit declared on a TreeVisitor superclass is forwarded.
  • visitIdentifier declared on a JavaIsoVisitor superclass is forwarded (exercises the iso-visitor stop-boundary).

Both fail on main and pass with this change; existing adapter tests, rewrite-groovy, and rewrite-core stay green.

Also

Incidental cleanup of the touched file: inline fully-qualified class names replaced with imports, and a few verbose comments trimmed.

TreeVisitorAdapter collected proxy-forwarded methods from delegate.getClass().getDeclaredMethods() (the leaf only), so preVisit/postVisit/visitX declared on a superclass never fired under cross-language adapt(). Walk the delegate hierarchy up to (not into) the iso-visitor instead, leaf-first so the most-derived override wins. Also de-qualifies inline class names and trims comments in the file.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 3, 2026
@jkschneider jkschneider merged commit 64c92c7 into main Jul 3, 2026
1 check passed
@jkschneider jkschneider deleted the fix/adapter-superclass-visit-methods branch July 3, 2026 19:44
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant