You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by sd...@apache.org on 2023/12/20 11:39:32 UTC

(netbeans) 11/13: Compensate Lsp4j bad EnumSet deserialization.

This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch sdedic/feature/project-dependency-add_base2
in repository https://gitbox.apache.org/repos/asf/netbeans.git

commit d0e993bbac047ad6944916a454f765c6c3b7a76b
Author: Svata Dedic <sv...@oracle.com>
AuthorDate: Mon Dec 18 19:15:36 2023 +0100

    Compensate Lsp4j bad EnumSet deserialization.
---
 .../java/lsp/server/protocol/NbGsonAdapter.java    | 40 ++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/NbGsonAdapter.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/NbGsonAdapter.java
index 4054e60ae0..0dc958db2f 100644
--- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/NbGsonAdapter.java
+++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/NbGsonAdapter.java
@@ -22,6 +22,11 @@ import com.google.gson.ExclusionStrategy;
 import com.google.gson.FieldAttributes;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParseException;
 import com.google.gson.TypeAdapter;
 import com.google.gson.TypeAdapterFactory;
 import com.google.gson.reflect.TypeToken;
@@ -29,10 +34,13 @@ import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonToken;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.net.MalformedURLException;
 import java.nio.file.FileSystemNotFoundException;
 import java.nio.file.Paths;
 import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.logging.Level;
@@ -185,5 +193,37 @@ public class NbGsonAdapter implements LspGsonSetup {
                 }
             }
         });
+        b.registerTypeAdapter(EnumSet.class, new EnumSetDeserializer());
     }
+
+
+    /**
+     * LSP4j obscures EnumSet handling by its Collection type adapter factory. We need to register a type adapter
+     * that overrides the deserialization for enumsets. This adapter also accepts a primitive as a (singleton) EnumSet.
+     */
+    private static final class EnumSetDeserializer implements JsonDeserializer<EnumSet> {
+        @Override
+        public EnumSet deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
+            ParameterizedType pt = (ParameterizedType)type;
+            Type itemType = pt.getActualTypeArguments()[0];
+            if (je.isJsonPrimitive()) {
+                Enum e = (Enum)jdc.deserialize(je, itemType);
+                if (e != null) {
+                    return EnumSet.of(e);
+                }
+            } else if (!je.isJsonArray()) {
+                throw new JsonParseException("Primitive or array expected");
+            }
+            JsonArray arr = je.getAsJsonArray();
+            EnumSet raw = EnumSet.noneOf((Class)itemType);
+            for (JsonElement el : arr) {
+                if (!el.isJsonPrimitive()) {
+                    throw new JsonParseException("Primitive item expected");
+                }
+                raw.add((Enum)jdc.deserialize(el, itemType));
+            }
+            return raw;
+        }
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists