Skip to content

Commit c8e8bd2

Browse files
Fix RPC receiving of method invocation identifier type (#7139)
1 parent e614127 commit c8e8bd2

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

rewrite-java/src/main/java/org/openrewrite/java/internal/rpc/JavaReceiver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,14 @@ public J visitMethodDeclaration(J.MethodDeclaration method, RpcReceiveQueue q) {
378378

379379
@Override
380380
public J visitMethodInvocation(J.MethodInvocation method, RpcReceiveQueue q) {
381-
return method
381+
method = method
382382
.getPadding().withSelect(q.receive(method.getPadding().getSelect(), s -> visitRightPadded(s, q)))
383-
.getPadding().withTypeParameters(q.receive(method.getPadding().getTypeParameters(), tp -> visitContainer(tp, q)))
384-
.withName(q.receive(method.getName(), n -> (J.Identifier) visitNonNull(n, q)))
383+
.getPadding().withTypeParameters(q.receive(method.getPadding().getTypeParameters(), tp -> visitContainer(tp, q)));
384+
J.Identifier name = q.receive(method.getName(), n -> (J.Identifier) visitNonNull(n, q));
385+
return method
385386
.getPadding().withArguments(q.receive(method.getPadding().getArguments(), a -> visitContainer(a, q)))
386-
.withMethodType(q.receive(method.getMethodType(), t -> (JavaType.Method) visitType(t, q)));
387+
.withMethodType(q.receive(method.getMethodType(), t -> (JavaType.Method) visitType(t, q)))
388+
.withName(name);
387389
}
388390

389391
@Override
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.internal.rpc;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.Tree;
21+
import org.openrewrite.java.tree.J;
22+
import org.openrewrite.java.tree.JContainer;
23+
import org.openrewrite.java.tree.JavaType;
24+
import org.openrewrite.java.tree.Space;
25+
import org.openrewrite.marker.Markers;
26+
import org.openrewrite.rpc.RpcObjectData;
27+
import org.openrewrite.rpc.RpcReceiveQueue;
28+
import org.openrewrite.rpc.RpcSendQueue;
29+
30+
import java.nio.file.Path;
31+
import java.util.*;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
class JavaReceiverTest {
36+
37+
private Deque<List<RpcObjectData>> batches;
38+
private RpcSendQueue sq;
39+
private RpcReceiveQueue rq;
40+
41+
@BeforeEach
42+
void setUp() {
43+
batches = new ArrayDeque<>();
44+
String sourceFileType = J.CompilationUnit.class.getName();
45+
IdentityHashMap<Object, Integer> localRefs = new IdentityHashMap<>();
46+
sq = new RpcSendQueue(1, e -> batches.addLast(encode(e)), localRefs, sourceFileType, false);
47+
rq = new RpcReceiveQueue(new HashMap<>(), batches::removeFirst, sourceFileType, null);
48+
}
49+
50+
@Test
51+
void methodInvocationRoundTripPreservesNameType() {
52+
// given
53+
JavaType.Method methodType = new JavaType.Method(
54+
null, 0, null, "foo", null,
55+
Collections.emptyList(), Collections.emptyList(),
56+
Collections.emptyList(), Collections.emptyList(),
57+
null, null
58+
);
59+
J.Identifier name = new J.Identifier(
60+
Tree.randomId(), Space.EMPTY, Markers.EMPTY,
61+
Collections.emptyList(), "foo", methodType, null
62+
);
63+
J.MethodInvocation original = new J.MethodInvocation(
64+
Tree.randomId(), Space.EMPTY, Markers.EMPTY,
65+
null, null, name,
66+
JContainer.build(Space.EMPTY, Collections.emptyList(), Markers.EMPTY),
67+
methodType
68+
);
69+
70+
// when: send as ADD (from null) and receive — exercises the actual JavaSender/JavaReceiver
71+
sq.send(original, null, null);
72+
sq.flush();
73+
J.MethodInvocation received = rq.receive(null);
74+
75+
// then
76+
assertThat(received.getMethodType()).isNotNull();
77+
assertThat(received.getName().getType())
78+
.as("Name identifier type must survive the sender/receiver round trip")
79+
.isNotNull();
80+
}
81+
82+
private List<RpcObjectData> encode(List<RpcObjectData> batch) {
83+
List<RpcObjectData> encoded = new ArrayList<>();
84+
for (RpcObjectData data : batch) {
85+
if (data.getValue() instanceof UUID || data.getValue() instanceof Path) {
86+
encoded.add(new RpcObjectData(data.getState(), data.getValueType(), data.getValue().toString(), data.getRef(), false));
87+
} else {
88+
encoded.add(data);
89+
}
90+
}
91+
return encoded;
92+
}
93+
}

0 commit comments

Comments
 (0)