From 6cd06736345b3892294a8c1b97f10f3871ff9224 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 9 Nov 2023 19:13:23 +0100 Subject: [PATCH] Another approach --- .../org/eclipse/lsp4j/jsonrpc/Launcher.java | 11 ++-- .../eclipse/lsp4j/jsonrpc/MessageTracer.java | 9 ++-- .../lsp4j/jsonrpc/TracingMessageConsumer.java | 50 +++++++++++++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/Launcher.java b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/Launcher.java index 7fe551ac..c6a42590 100644 --- a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/Launcher.java +++ b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/Launcher.java @@ -235,7 +235,12 @@ public static class Builder { protected boolean validateMessages; protected Consumer configureGson; protected ClassLoader classLoader; + /** + * @deprecated use {@link #tracer} instead + */ + @Deprecated protected MessageTracer messageTracer; + protected PrintWriter tracer; public Builder setLocalService(Object localService) { this.localServices = Collections.singletonList(localService); @@ -294,7 +299,7 @@ public Builder validateMessages(boolean validate) { public Builder traceMessages(PrintWriter tracer) { if (tracer != null) { - this.messageTracer = new MessageTracer(tracer); + this.tracer = tracer; } return this; } @@ -318,8 +323,8 @@ public Launcher create() { // Create the JSON handler, remote endpoint and remote proxy MessageJsonHandler jsonHandler = createJsonHandler(); - if (messageTracer != null) { - messageTracer.setJsonHandler(jsonHandler); + if (tracer != null) { + messageTracer = new MessageTracer(tracer, jsonHandler); } RemoteEndpoint remoteEndpoint = createRemoteEndpoint(jsonHandler); diff --git a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/MessageTracer.java b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/MessageTracer.java index 4c5c8783..66180500 100644 --- a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/MessageTracer.java +++ b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/MessageTracer.java @@ -27,16 +27,13 @@ */ public class MessageTracer implements Function { private final PrintWriter printWriter; - private MessageJsonHandler jsonHandler; + private final MessageJsonHandler jsonHandler; private final Map sentRequests = new HashMap<>(); private final Map receivedRequests = new HashMap<>(); - MessageTracer(PrintWriter printWriter) { + MessageTracer(PrintWriter printWriter, MessageJsonHandler jsonHandler) { this.printWriter = Objects.requireNonNull(printWriter); - } - - public void setJsonHandler(MessageJsonHandler jsonHandler) { - this.jsonHandler = jsonHandler; + this.jsonHandler = Objects.requireNonNull(jsonHandler); } @Override diff --git a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/TracingMessageConsumer.java b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/TracingMessageConsumer.java index c2a6bc33..977b8a16 100644 --- a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/TracingMessageConsumer.java +++ b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/TracingMessageConsumer.java @@ -15,6 +15,7 @@ import java.time.Clock; import java.time.Instant; import java.time.format.DateTimeFormatter; +import java.util.Collections; import java.util.Locale; import java.util.Map; import java.util.Objects; @@ -49,6 +50,7 @@ public class TracingMessageConsumer implements MessageConsumer { * @param sentRequests A map that keeps track of pending sent request data. * @param receivedRequests A map that keeps track of pending received request data. * @param printWriter Where to write the log to. + * @param jsonHandler The handler to be used to serialize messages. * @param clock The clock that is used to calculate timestamps and durations. */ public TracingMessageConsumer( @@ -67,6 +69,54 @@ public TracingMessageConsumer( * @param receivedRequests A map that keeps track of pending received request data. * @param printWriter Where to write the log to. * @param clock The clock that is used to calculate timestamps and durations. + * @deprecated use {@link TracingMessageConsumer#TracingMessageConsumer(MessageConsumer, Map, Map, PrintWriter, MessageJsonHandler, Clock)} + */ + @Deprecated + public TracingMessageConsumer( + MessageConsumer messageConsumer, + Map sentRequests, + Map receivedRequests, + PrintWriter printWriter, + Clock clock) { + this(messageConsumer, sentRequests, receivedRequests, printWriter, defaultHandlerWithoutUserAdapters(), clock, null); + } + + /** + * For backward compatibility, we are using a default handler that does not contain any user adapters. + */ + private static MessageJsonHandler defaultHandlerWithoutUserAdapters() { + return new MessageJsonHandler(Collections.emptyMap(), gsonBuilder -> { + gsonBuilder.setPrettyPrinting(); + }); + } + + /** + * @param messageConsumer The {@link MessageConsumer} to wrap. + * @param sentRequests A map that keeps track of pending sent request data. + * @param receivedRequests A map that keeps track of pending received request data. + * @param printWriter Where to write the log to. + * @param clock The clock that is used to calculate timestamps and durations. + * @param locale THe Locale to format the timestamps and durations, or null to use default locale. + * @deprecated use {@link TracingMessageConsumer#TracingMessageConsumer(MessageConsumer, Map, Map, PrintWriter, MessageJsonHandler, Clock, Locale)} + */ + @Deprecated + public TracingMessageConsumer( + MessageConsumer messageConsumer, + Map sentRequests, + Map receivedRequests, + PrintWriter printWriter, + Clock clock, + Locale locale) { + this(messageConsumer, sentRequests, receivedRequests, printWriter, defaultHandlerWithoutUserAdapters(), clock, locale); + } + + /** + * @param messageConsumer The {@link MessageConsumer} to wrap. + * @param sentRequests A map that keeps track of pending sent request data. + * @param receivedRequests A map that keeps track of pending received request data. + * @param printWriter Where to write the log to. + * @param jsonHandler The handler to be used to serialize messages. + * @param clock The clock that is used to calculate timestamps and durations. * @param locale THe Locale to format the timestamps and durations, or null to use default locale. */ public TracingMessageConsumer(