Skip to content

Commit e9d9c4e

Browse files
C#: Add WithMarkers to Tree interface, fix SearchResult.Found
The Tree interface was missing WithMarkers, unlike its Java counterpart. SearchResult.Found used reflection to call WithMarkers on concrete types, which silently returned the same instance when GetMethod failed. This broke PreconditionsCheckTest assertions that were previously masked by Gradle build cache. Add Tree.WithMarkers(Markers) to the interface and explicit implementations on all 181 concrete tree types (J, Cs, Linq, Xml). Replace the fragile reflection in SearchResult.Found with a direct interface call.
1 parent 71809c7 commit e9d9c4e

File tree

6 files changed

+186
-3
lines changed

6 files changed

+186
-3
lines changed

rewrite-csharp/csharp/OpenRewrite/CSharp/Cs.cs

Lines changed: 93 additions & 0 deletions
Large diffs are not rendered by default.

rewrite-csharp/csharp/OpenRewrite/CSharp/Linq.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public QueryExpression WithBody(QueryBody body) =>
6464
public JavaType? Type => FromClause.Type;
6565

6666
Tree Tree.WithId(Guid id) => WithId(id);
67+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
6768

6869
public bool Equals(QueryExpression? other) => other is not null && Id == other.Id;
6970
public override bool Equals(object? obj) => Equals(obj as QueryExpression);
@@ -103,6 +104,7 @@ public QueryBody WithContinuation(QueryContinuation? continuation) =>
103104
ReferenceEquals(continuation, Continuation) ? this : new(Id, Prefix, Markers, Clauses, SelectOrGroup, continuation);
104105

105106
Tree Tree.WithId(Guid id) => WithId(id);
107+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
106108

107109
public bool Equals(QueryBody? other) => other is not null && Id == other.Id;
108110
public override bool Equals(object? obj) => Equals(obj as QueryBody);
@@ -146,6 +148,7 @@ public FromClause WithExpression(Expression expression) =>
146148
public JavaType? Type => Expression.Type;
147149

148150
Tree Tree.WithId(Guid id) => WithId(id);
151+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
149152

150153
public bool Equals(FromClause? other) => other is not null && Id == other.Id;
151154
public override bool Equals(object? obj) => Equals(obj as FromClause);
@@ -183,6 +186,7 @@ public LetClause WithExpression(Expression expression) =>
183186
ReferenceEquals(expression, Expression) ? this : new(Id, Prefix, Markers, IdentifierPadded, expression);
184187

185188
Tree Tree.WithId(Guid id) => WithId(id);
189+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
186190

187191
public bool Equals(LetClause? other) => other is not null && Id == other.Id;
188192
public override bool Equals(object? obj) => Equals(obj as LetClause);
@@ -232,6 +236,7 @@ public JoinClause WithInto(JLeftPadded<JoinIntoClause>? into) =>
232236
ReferenceEquals(into, Into) ? this : new(Id, Prefix, Markers, IdentifierPadded, InExpression, LeftExpression, RightExpression, into);
233237

234238
Tree Tree.WithId(Guid id) => WithId(id);
239+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
235240

236241
public bool Equals(JoinClause? other) => other is not null && Id == other.Id;
237242
public override bool Equals(object? obj) => Equals(obj as JoinClause);
@@ -263,6 +268,7 @@ public JoinIntoClause WithIdentifier(Identifier identifier) =>
263268
ReferenceEquals(identifier, Identifier) ? this : new(Id, Prefix, Markers, identifier);
264269

265270
Tree Tree.WithId(Guid id) => WithId(id);
271+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
266272

267273
public bool Equals(JoinIntoClause? other) => other is not null && Id == other.Id;
268274
public override bool Equals(object? obj) => Equals(obj as JoinIntoClause);
@@ -294,6 +300,7 @@ public WhereClause WithCondition(Expression condition) =>
294300
ReferenceEquals(condition, Condition) ? this : new(Id, Prefix, Markers, condition);
295301

296302
Tree Tree.WithId(Guid id) => WithId(id);
303+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
297304

298305
public bool Equals(WhereClause? other) => other is not null && Id == other.Id;
299306
public override bool Equals(object? obj) => Equals(obj as WhereClause);
@@ -325,6 +332,7 @@ public OrderByClause WithOrderings(IList<JRightPadded<Ordering>> orderings) =>
325332
ReferenceEquals(orderings, Orderings) ? this : new(Id, Prefix, Markers, orderings);
326333

327334
Tree Tree.WithId(Guid id) => WithId(id);
335+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
328336

329337
public bool Equals(OrderByClause? other) => other is not null && Id == other.Id;
330338
public override bool Equals(object? obj) => Equals(obj as OrderByClause);
@@ -371,6 +379,7 @@ public Ordering WithDirection(DirectionKind? direction) =>
371379
direction == Direction ? this : new(Id, Prefix, Markers, ExpressionPadded, direction);
372380

373381
Tree Tree.WithId(Guid id) => WithId(id);
382+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
374383

375384
public bool Equals(Ordering? other) => other is not null && Id == other.Id;
376385
public override bool Equals(object? obj) => Equals(obj as Ordering);
@@ -402,6 +411,7 @@ public SelectClause WithExpression(Expression expression) =>
402411
ReferenceEquals(expression, Expression) ? this : new(Id, Prefix, Markers, expression);
403412

404413
Tree Tree.WithId(Guid id) => WithId(id);
414+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
405415

406416
public bool Equals(SelectClause? other) => other is not null && Id == other.Id;
407417
public override bool Equals(object? obj) => Equals(obj as SelectClause);
@@ -437,6 +447,7 @@ public GroupClause WithKey(Expression key) =>
437447
ReferenceEquals(key, Key) ? this : new(Id, Prefix, Markers, GroupExpression, key);
438448

439449
Tree Tree.WithId(Guid id) => WithId(id);
450+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
440451

441452
public bool Equals(GroupClause? other) => other is not null && Id == other.Id;
442453
public override bool Equals(object? obj) => Equals(obj as GroupClause);
@@ -472,6 +483,7 @@ public QueryContinuation WithBody(QueryBody body) =>
472483
ReferenceEquals(body, Body) ? this : new(Id, Prefix, Markers, Identifier, body);
473484

474485
Tree Tree.WithId(Guid id) => WithId(id);
486+
Tree Tree.WithMarkers(Markers markers) => WithMarkers(markers);
475487

476488
public bool Equals(QueryContinuation? other) => other is not null && Id == other.Id;
477489
public override bool Equals(object? obj) => Equals(obj as QueryContinuation);

rewrite-csharp/csharp/OpenRewrite/Core/SearchResult.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public sealed class SearchResult(Guid id, string? description) : Marker, IRpcCod
2828

2929
/// <summary>
3030
/// Adds a SearchResult marker to the given tree node.
31-
/// Uses reflection to call WithMarkers on the concrete type.
3231
/// </summary>
3332
public static T Found<T>(T tree, string? description = null) where T : J
3433
{
3534
var newMarkers = tree.Markers.Add(new SearchResult(Guid.NewGuid(), description));
36-
var withMarkers = tree.GetType().GetMethod("WithMarkers", [typeof(Markers)]);
37-
return withMarkers != null ? (T)withMarkers.Invoke(tree, [newMarkers])! : tree;
35+
return (T)((Tree)tree).WithMarkers(newMarkers);
3836
}
3937

4038
public void RpcSend(SearchResult after, RpcSendQueue q)

rewrite-csharp/csharp/OpenRewrite/Core/Tree.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public interface Tree
2525
Markers Markers { get; }
2626

2727
Tree WithId(Guid id);
28+
29+
Tree WithMarkers(Markers markers);
2830
}
2931

3032
/// <summary>

0 commit comments

Comments
 (0)