Skip to content

C#: Fix SearchResult.Found using dynamic dispatch instead of reflection#7319

Draft
steve-aom-elliott wants to merge 2 commits intomainfrom
fix/csharp-tree-withmarkers
Draft

C#: Fix SearchResult.Found using dynamic dispatch instead of reflection#7319
steve-aom-elliott wants to merge 2 commits intomainfrom
fix/csharp-tree-withmarkers

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

@steve-aom-elliott steve-aom-elliott commented Apr 8, 2026

Summary

  • Replace fragile GetMethod reflection in SearchResult.Found<T>() with dynamic dispatch

The original code used tree.GetType().GetMethod("WithMarkers", ...) which silently returned null, causing Found to return the same tree instance unchanged. Dynamic dispatch reliably resolves WithMarkers on any concrete type at runtime.

Context

Two PreconditionsCheckTest tests have been failing on main since 71809c7 invalidated the Gradle build cache, causing csharpTest to actually execute for the first time:

  1. CheckDelegatesToVisitorWhenPreconditionMatchesAssert.NotSame() failure (same object returned)
  2. PreconditionMatchesAndVisitorRuns — "Recipe was expected to make changes" (no marker added)

These tests were never caught earlier because :rewrite-csharp:csharpTest was FROM-CACHE in CI.

Test plan

  • All 4 PreconditionsCheckTest tests pass locally (including both previously failing)
  • CI passes — specifically :rewrite-csharp:csharpTest

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Apr 8, 2026
@steve-aom-elliott steve-aom-elliott force-pushed the fix/csharp-tree-withmarkers branch from e9d9c4e to c2e3ff4 Compare April 8, 2026 18:17
@steve-aom-elliott steve-aom-elliott changed the title C#: Add WithMarkers to Tree interface, fix SearchResult.Found C#: Fix SearchResult.Found using dynamic dispatch instead of reflection Apr 8, 2026
@steve-aom-elliott steve-aom-elliott requested a review from macsux April 8, 2026 19:00
SearchResult.Found used GetMethod reflection to call WithMarkers on
concrete tree types. This silently returned the same instance when
GetMethod failed, breaking PreconditionsCheckTest assertions that
were previously masked by Gradle build cache.

Replace the fragile reflection with dynamic dispatch, which reliably
resolves WithMarkers on any concrete type at runtime.
ExpressionStatement delegates Markers to its inner expression but
had no WithMarkers method, which would cause a RuntimeBinderException
if SearchResult.Found or any other dynamic WithMarkers call targeted
this type.
@steve-aom-elliott steve-aom-elliott force-pushed the fix/csharp-tree-withmarkers branch from 27f81c1 to be46a4f Compare April 9, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant