Skip to content

Commit 186c617

Browse files
authored
JavaScript Type reading of cyclic types from Java -> TypeScript process (#6126)
1 parent fb73848 commit 186c617

53 files changed

Lines changed: 2943 additions & 2604 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.idea/icon.png

-5.17 KB
Binary file not shown.

rewrite-core/src/main/java/org/openrewrite/Result.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,14 @@ public String toString() {
268268
}
269269

270270
public static boolean isLocalAndHasNoChanges(@Nullable SourceFile before, @Nullable SourceFile after) {
271-
try {
272-
return (before == after) ||
273-
(before != null && after != null &&
274-
// Remote source files are fetched on `printAll`, let's avoid that cost.
275-
!(before instanceof Remote) && !(after instanceof Remote) &&
276-
before.printAll().equals(after.printAll()));
277-
} catch (Exception e) {
271+
// try {
272+
// return (before == after) ||
273+
// (before != null && after != null &&
274+
// // Remote source files are fetched on `printAll`, let's avoid that cost.
275+
// !(before instanceof Remote) && !(after instanceof Remote) &&
276+
// before.printAll().equals(after.printAll()));
277+
// } catch (Exception e) {
278278
return false;
279-
}
279+
// }
280280
}
281281
}

rewrite-core/src/main/java/org/openrewrite/internal/ListUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ public static <T> List<T> insertAll(@Nullable List<T> ls, int index, @Nullable L
502502
* @param <T> The type of elements in the list.
503503
* @return The array representation of the list, or null if the list is empty.
504504
*/
505-
public static <T> T @Nullable [] arrayOrNullIfEmpty(@Nullable List<T> list, T[] array) {
506-
if (list == null || list.isEmpty()) {
505+
public static <T> T @Nullable [] arrayOrNullIfEmpty(@Nullable List<T> list, T @Nullable [] array) {
506+
if (list == null || array == null || list.isEmpty()) {
507507
return null;
508508
}
509509
return list.toArray(array);

rewrite-core/src/main/java/org/openrewrite/rpc/RewriteRpc.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openrewrite.tree.ParsingEventListener;
3434
import org.openrewrite.tree.ParsingExecutionContextView;
3535

36+
import java.io.PrintStream;
3637
import java.nio.file.Files;
3738
import java.nio.file.Path;
3839
import java.time.Duration;
@@ -42,6 +43,7 @@
4243
import java.util.concurrent.TimeUnit;
4344
import java.util.concurrent.TimeoutException;
4445
import java.util.concurrent.atomic.AtomicInteger;
46+
import java.util.concurrent.atomic.AtomicReference;
4547
import java.util.function.Consumer;
4648
import java.util.function.Supplier;
4749
import java.util.stream.Stream;
@@ -62,6 +64,9 @@ public class RewriteRpc {
6264
private final AtomicInteger batchSize = new AtomicInteger(200);
6365
private Duration timeout = Duration.ofSeconds(30);
6466
private Supplier<? extends @Nullable RuntimeException> livenessCheck = () -> null;
67+
private final AtomicReference<PrintStream> log = new AtomicReference<>();
68+
private final AtomicReference<TraceGetObject> traceGetObject = new AtomicReference<>(
69+
new TraceGetObject(false, false));
6570

6671
final PreparedRecipeCache preparedRecipes = new PreparedRecipeCache();
6772

@@ -102,13 +107,21 @@ public RewriteRpc(JsonRpc jsonRpc, Environment marketplace) {
102107
this::getObject, this::getCursor));
103108
jsonRpc.rpc("Generate", new Generate.Handler(localObjects, preparedRecipes,
104109
this::getObject));
105-
jsonRpc.rpc("GetObject", new GetObject.Handler(batchSize, remoteObjects, localObjects, localRefs));
110+
jsonRpc.rpc("GetObject", new GetObject.Handler(batchSize, remoteObjects, localObjects,
111+
localRefs, log, () -> traceGetObject.get().isSend()));
106112
jsonRpc.rpc("GetRecipes", new JsonRpcMethod<Void>() {
107113
@Override
108114
protected Object handle(Void noParams) {
109115
return marketplace.listRecipeDescriptors();
110116
}
111117
});
118+
jsonRpc.rpc("TraceGetObject", new JsonRpcMethod<TraceGetObject>() {
119+
@Override
120+
protected Boolean handle(TraceGetObject request) {
121+
traceGetObject.set(request);
122+
return true;
123+
}
124+
});
112125
jsonRpc.rpc("GetLanguages", new JsonRpcMethod<Void>() {
113126
@Override
114127
protected Object handle(Void noParams) {
@@ -150,7 +163,17 @@ public RewriteRpc batchSize(int batchSize) {
150163
return this;
151164
}
152165

166+
public RewriteRpc log(@Nullable PrintStream logFile) {
167+
//noinspection DataFlowIssue
168+
this.log.set(logFile);
169+
return this;
170+
}
171+
153172
public void shutdown() {
173+
//noinspection ConstantValue
174+
if (log.get() != null) {
175+
log.get().close();
176+
}
154177
jsonRpc.shutdown();
155178
}
156179

@@ -312,7 +335,10 @@ public boolean tryAdvance(Consumer<? super SourceFile> action) {
312335
SourceFile sourceFile = null;
313336
parsingListener.startedParsing(input);
314337
try {
315-
sourceFile = parser.requirePrintEqualsInput(getObject(id, sourceFileType), input, relativeTo, ctx);
338+
// input.getRelativePath(relativeTo)
339+
sourceFile = getObject(id, sourceFileType);
340+
// TODO this should be handled on the remote side.
341+
// sourceFile = parser.requirePrintEqualsInput(sourceFile, input, relativeTo, ctx);
316342
} catch (Exception e) {
317343
sourceFile = ParseError.build(parser, input, relativeTo, ctx, e);
318344
} finally {
@@ -355,18 +381,25 @@ public String print(Tree tree, Cursor parent) {
355381

356382
public String print(Tree tree, Cursor parent, Print.@Nullable MarkerPrinter markerPrinter) {
357383
localObjects.put(tree.getId().toString(), tree);
384+
SourceFile sourceFile = tree instanceof SourceFile ? (SourceFile) tree : parent.firstEnclosingOrThrow(SourceFile.class);
358385
return send(
359386
"Print",
360387
new Print(
361388
tree.getId().toString(),
362-
(tree instanceof SourceFile ? tree : parent.firstEnclosingOrThrow(SourceFile.class))
363-
.getClass().getName(),
389+
sourceFile.getSourcePath(),
390+
sourceFile.getClass().getName(),
364391
markerPrinter
365392
),
366393
String.class
367394
);
368395
}
369396

397+
public RewriteRpc traceGetObject(boolean receive, boolean send) {
398+
this.traceGetObject.set(new TraceGetObject(receive, send));
399+
send("TraceGetObject", new TraceGetObject(receive, send), Boolean.class);
400+
return this;
401+
}
402+
370403
@VisibleForTesting
371404
@Nullable
372405
List<String> getCursorIds(@Nullable Cursor cursor) {
@@ -391,20 +424,22 @@ public <T> T getObject(String id, @Nullable String sourceFileType) {
391424
RpcReceiveQueue q = new RpcReceiveQueue(
392425
remoteRefs,
393426
() -> send("GetObject", new GetObject(id, sourceFileType), GetObjectResponse.class),
394-
sourceFileType
427+
sourceFileType,
428+
log.get()
395429
);
396430
Object remoteObject = q.receive(localObject, null);
397431
if (q.take().getState() != END_OF_OBJECT) {
398432
throw new IllegalStateException("Expected END_OF_OBJECT");
399433
}
400434

435+
//noinspection ConstantValue
401436
if (remoteObject != null) {
402437
// We are now in sync with the remote state of the object.
403438
remoteObjects.put(id, requireNonNull(remoteObject));
404439
localObjects.put(id, remoteObject);
405440
}
406441

407-
//noinspection unchecked,DataFlowIssue
442+
//noinspection unchecked
408443
return (T) remoteObject;
409444
}
410445

rewrite-core/src/main/java/org/openrewrite/rpc/RpcObjectData.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
2727
import lombok.AccessLevel;
2828
import lombok.NoArgsConstructor;
29-
import lombok.RequiredArgsConstructor;
3029
import lombok.Value;
31-
import lombok.experimental.NonFinal;
30+
import lombok.With;
3231
import org.jspecify.annotations.Nullable;
3332
import org.openrewrite.Tree;
3433

@@ -39,7 +38,6 @@
3938
*/
4039
@Value
4140
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE, onConstructor_ = @JsonCreator)
42-
@RequiredArgsConstructor
4341
public class RpcObjectData {
4442
private static final ObjectMapper mapper = JsonMapper.builder()
4543
// to be able to construct classes that have @Data and a single field
@@ -86,13 +84,18 @@ public class RpcObjectData {
8684
* useful in debugging asymmetries between senders/receivers.
8785
*/
8886
@Nullable
89-
@NonFinal
90-
String trace = Trace.traceSender();
87+
String trace;
9188

92-
public RpcObjectData withTrace() {
93-
RpcObjectData d = new RpcObjectData(state, valueType, value, ref);
94-
d.trace = null;
95-
return d;
89+
public RpcObjectData(State state, @Nullable String valueType, @Nullable Object value, @Nullable Integer ref, boolean trace) {
90+
this.state = state;
91+
this.valueType = valueType;
92+
this.value = value;
93+
this.ref = ref;
94+
this.trace = trace ? Trace.traceSender() : null;
95+
}
96+
97+
public RpcObjectData withoutTrace() {
98+
return new RpcObjectData(state, valueType, value, ref, false);
9699
}
97100

98101
public <V> @Nullable V getValue() {

rewrite-core/src/main/java/org/openrewrite/rpc/RpcReceiveQueue.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jspecify.annotations.Nullable;
1919
import org.objenesis.ObjenesisStd;
2020

21+
import java.io.PrintStream;
2122
import java.util.*;
2223
import java.util.function.Function;
2324
import java.util.function.Supplier;
@@ -32,11 +33,13 @@ public class RpcReceiveQueue {
3233
private final Map<Integer, Object> refs;
3334
private final Supplier<List<RpcObjectData>> pull;
3435
private final @Nullable String sourceFileType;
36+
private final @Nullable PrintStream log;
3537

3638
public RpcReceiveQueue(Map<Integer, Object> refs, Supplier<List<RpcObjectData>> pull,
37-
@Nullable String sourceFileType) {
39+
@Nullable String sourceFileType, @Nullable PrintStream log) {
3840
this.refs = refs;
3941
this.sourceFileType = sourceFileType;
42+
this.log = log;
4043
this.batch = new ArrayDeque<>();
4144
this.pull = pull;
4245
}
@@ -93,7 +96,7 @@ public <T> T receive(@Nullable T before) {
9396
@SuppressWarnings("DataFlowIssue")
9497
public <T> T receive(@Nullable T before, @Nullable UnaryOperator<T> onChange) {
9598
RpcObjectData message = take();
96-
Trace.traceReceiver(message);
99+
Trace.traceReceiver(message, log);
97100
Integer ref = null;
98101
switch (message.getState()) {
99102
case NO_CHANGE:
@@ -150,6 +153,7 @@ public <T> T receive(@Nullable T before, @Nullable UnaryOperator<T> onChange) {
150153
@SuppressWarnings("DataFlowIssue")
151154
public <T> List<T> receiveList(@Nullable List<T> before, @Nullable UnaryOperator<T> onChange) {
152155
RpcObjectData msg = take();
156+
Trace.traceReceiver(msg, log);
153157
switch (msg.getState()) {
154158
case NO_CHANGE:
155159
return before;

rewrite-core/src/main/java/org/openrewrite/rpc/RpcSendQueue.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ public class RpcSendQueue {
3232
private final Consumer<List<RpcObjectData>> drain;
3333
private final IdentityHashMap<Object, Integer> refs;
3434
private final @Nullable String sourceFileType;
35+
private final boolean trace;
3536

3637
private @Nullable Object before;
3738

3839
public RpcSendQueue(int batchSize, ThrowingConsumer<List<RpcObjectData>> drain, IdentityHashMap<Object, Integer> refs,
39-
@Nullable String sourceFileType) {
40+
@Nullable String sourceFileType, boolean trace) {
4041
this.batchSize = batchSize;
4142
this.batch = new ArrayList<>(batchSize);
4243
this.drain = drain;
4344
this.refs = refs;
4445
this.sourceFileType = sourceFileType;
46+
this.trace = trace;
4547
}
4648

4749
public void put(RpcObjectData rpcObjectData) {
@@ -103,14 +105,14 @@ public <T> void send(@Nullable T after, @Nullable T before, @Nullable Runnable o
103105
Object beforeVal = Reference.getValue(before);
104106

105107
if (beforeVal == afterVal) {
106-
put(new RpcObjectData(NO_CHANGE, null, null, null));
108+
put(new RpcObjectData(NO_CHANGE, null, null, null, trace));
107109
} else if (beforeVal == null) {
108110
add(after, onChange);
109111
} else if (afterVal == null) {
110-
put(new RpcObjectData(DELETE, null, null, null));
112+
put(new RpcObjectData(DELETE, null, null, null, trace));
111113
} else {
112114
RpcCodec<Object> afterCodec = RpcCodec.forInstance(afterVal, sourceFileType);
113-
put(new RpcObjectData(CHANGE, getValueType(afterVal), onChange == null && afterCodec == null ? afterVal : null, null));
115+
put(new RpcObjectData(CHANGE, getValueType(afterVal), onChange == null && afterCodec == null ? afterVal : null, null, trace));
114116
doChange(after, before, onChange, afterCodec);
115117
}
116118
}
@@ -133,9 +135,9 @@ <T> void sendList(@Nullable List<T> after,
133135
} else {
134136
T aBefore = before == null ? null : before.get(beforePos);
135137
if (aBefore == anAfter) {
136-
put(new RpcObjectData(NO_CHANGE, null, null, null));
138+
put(new RpcObjectData(NO_CHANGE, null, null, null, trace));
137139
} else {
138-
put(new RpcObjectData(CHANGE, getValueType(anAfter), null, null));
140+
put(new RpcObjectData(CHANGE, getValueType(anAfter), null, null, trace));
139141
doChange(anAfter, aBefore, onChangeRun, RpcCodec.forInstance(anAfter, sourceFileType));
140142
}
141143
}
@@ -155,7 +157,7 @@ private <T> Map<Object, Integer> putListPositions(List<T> after, @Nullable List<
155157
Integer beforePos = beforeIdx.get(id.apply(t));
156158
positions.add(beforePos == null ? ADDED_LIST_ITEM : beforePos);
157159
}
158-
put(new RpcObjectData(CHANGE, null, positions, null));
160+
put(new RpcObjectData(CHANGE, null, positions, null, trace));
159161
return beforeIdx;
160162
}
161163

@@ -164,7 +166,7 @@ private void add(Object after, @Nullable Runnable onChange) {
164166
Integer ref = null;
165167
if (after instanceof Reference) {
166168
if (refs.containsKey(afterVal)) {
167-
put(new RpcObjectData(ADD, null, null, refs.get(afterVal)));
169+
put(new RpcObjectData(ADD, null, null, refs.get(afterVal), trace));
168170
// No onChange call because the remote will be using an instance from its ref cache
169171
return;
170172
}
@@ -173,7 +175,7 @@ private void add(Object after, @Nullable Runnable onChange) {
173175
}
174176
RpcCodec<Object> afterCodec = RpcCodec.forInstance(afterVal, sourceFileType);
175177
put(new RpcObjectData(ADD, getValueType(afterVal),
176-
onChange == null && afterCodec == null ? afterVal : null, ref));
178+
onChange == null && afterCodec == null ? afterVal : null, ref, trace));
177179
doChange(afterVal, null, onChange, afterCodec);
178180
}
179181

rewrite-core/src/main/java/org/openrewrite/rpc/Trace.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.rpc;
1717

1818
import org.jspecify.annotations.Nullable;
19+
import org.openrewrite.PathUtils;
1920

2021
import java.io.File;
2122
import java.io.IOException;
@@ -32,21 +33,17 @@
3233
public class Trace {
3334
private static final Trace.StackElementSourceLookup sourceLookup = new Trace.StackElementSourceLookup();
3435

35-
public static boolean TRACE_SENDER = false;
36-
public static @Nullable PrintStream TRACE_RECEIVER = null;
37-
38-
public static void traceReceiver(RpcObjectData message) {
39-
PrintStream logFile = TRACE_RECEIVER;
36+
public static void traceReceiver(RpcObjectData message, @Nullable PrintStream logFile) {
4037
if (logFile != null && message.getTrace() != null) {
41-
logFile.println(message.withTrace());
38+
logFile.println(message.withoutTrace());
4239
logFile.println(" " + message.getTrace());
4340
logFile.println(" " + trace("Receiver"));
4441
logFile.flush();
4542
}
4643
}
4744

48-
public static @Nullable String traceSender() {
49-
return TRACE_SENDER ? trace("Sender") : null;
45+
public static String traceSender() {
46+
return trace("Sender");
5047
}
5148

5249
private static String trace(String type) {

0 commit comments

Comments
 (0)