From e914604563395b4d459241888a6df37a4d343c91 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Fri, 22 Sep 2023 17:19:52 -0700 Subject: [PATCH] JsonRpcRequest: Cleanup 1. Make params member immutable w/defensive copy 2. Constructor that takes long id calls canonical constructor 3. Use List for params list in several places 4. Improve JavaDoc --- .../consensusj/jsonrpc/JsonRpcRequest.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/consensusj-jsonrpc/src/main/java/org/consensusj/jsonrpc/JsonRpcRequest.java b/consensusj-jsonrpc/src/main/java/org/consensusj/jsonrpc/JsonRpcRequest.java index 7e49d86b0..7ed626858 100644 --- a/consensusj-jsonrpc/src/main/java/org/consensusj/jsonrpc/JsonRpcRequest.java +++ b/consensusj-jsonrpc/src/main/java/org/consensusj/jsonrpc/JsonRpcRequest.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import org.consensusj.jsonrpc.internal.NumberStringSerializer; +import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; @@ -43,7 +45,7 @@ public JsonRpcRequest(@JsonProperty("jsonrpc") String jsonrpc, this.jsonrpc = jsonrpc; this.method = method; this.id = id; - this.params = params; + this.params = Collections.unmodifiableList(new ArrayList<>(params)); } /** @@ -60,10 +62,7 @@ public JsonRpcRequest(JsonRpcMessage.Version jsonRpcVersion, long id, String method, List params) { - this.jsonrpc = jsonRpcVersion.jsonrpc(); - this.method = method; - this.id = Long.toString(id); - this.params = params; + this(jsonRpcVersion.jsonrpc(), Long.toString(id), method, params); } /** @@ -75,7 +74,7 @@ public JsonRpcRequest(JsonRpcMessage.Version jsonRpcVersion, * @param method Method of remote procedure to call * @param params Parameters to serialize */ - public JsonRpcRequest(JsonRpcMessage.Version jsonRpcVersion, String method, List params) { + public JsonRpcRequest(JsonRpcMessage.Version jsonRpcVersion, String method, List params) { this(jsonRpcVersion, JsonRpcRequest.nextRequestId.incrementAndGet(), method, removeTrailingNulls(params)); } @@ -88,7 +87,7 @@ public JsonRpcRequest(JsonRpcMessage.Version jsonRpcVersion, String method, List * @see JsonRpcClient#buildJsonRequest(String, List) * @see JsonRpcClient#buildJsonRequest(String, Object...) */ - public JsonRpcRequest(String method, List params) { + public JsonRpcRequest(String method, List params) { this(DEFAULT_JSON_RPC_VERSION, method, params); } @@ -118,16 +117,16 @@ public List getParams() { } /** - * Remove trailing nulls (all nulls *following* the last non-null object) - * - * This allows convenience methods to use `null` parameters to indicate the - * server-determined default should be used. If `null` were actually passed as - * JSON, then the server default would be overridden. `null` can be used before - * the last non-null element, but those `null`s will be sent to the server. + * Remove trailing {@code null}s (all {@code null}s following the last non-{@code null} object) + *

+ * This allows convenience methods to use {@code null} parameters to indicate the + * server-determined default should be used. If {@code null} were actually sent as + * JSON, then the server default would be overridden. {@code null} can be used before + * the last non-null element, but those {@code null} s will be sent to the server. */ - private static List removeTrailingNulls(List params) { + private static List removeTrailingNulls(List params) { LinkedList cleaned = new LinkedList<>(params); - while ((cleaned.size() > 0) && (cleaned.getLast() == null)) { + while (!cleaned.isEmpty() && cleaned.getLast() == null) { cleaned.removeLast(); } return cleaned;