|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + *******************************************************************************/ |
| 8 | +package org.eclipse.lsp4j.jsonrpc.json.adapters; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.lang.reflect.Constructor; |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.lang.reflect.TypeVariable; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.LinkedHashSet; |
| 17 | +import java.util.LinkedList; |
| 18 | +import java.util.Queue; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.SortedSet; |
| 21 | +import java.util.TreeSet; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import com.google.gson.Gson; |
| 25 | +import com.google.gson.TypeAdapter; |
| 26 | +import com.google.gson.TypeAdapterFactory; |
| 27 | +import com.google.gson.reflect.TypeToken; |
| 28 | +import com.google.gson.stream.JsonReader; |
| 29 | +import com.google.gson.stream.JsonToken; |
| 30 | +import com.google.gson.stream.JsonWriter; |
| 31 | + |
| 32 | +/** |
| 33 | + * A specialized type adapter for collections that can handle single values. |
| 34 | + */ |
| 35 | +public class CollectionTypeAdapter<E> extends TypeAdapter<Collection<E>> { |
| 36 | + |
| 37 | + public static class Factory implements TypeAdapterFactory { |
| 38 | + |
| 39 | + @Override |
| 40 | + @SuppressWarnings({ "unchecked" }) |
| 41 | + public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { |
| 42 | + if (!Collection.class.isAssignableFrom(typeToken.getRawType())) |
| 43 | + return null; |
| 44 | + |
| 45 | + Type[] elementTypes = TypeUtils.getElementTypes(typeToken, Collection.class); |
| 46 | + if (elementTypes.length != 1) |
| 47 | + return null; |
| 48 | + TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementTypes[0])); |
| 49 | + Supplier<Collection<Object>> constructor = getConstructor((Class<Collection<Object>>) typeToken.getRawType()); |
| 50 | + return (TypeAdapter<T>) create(gson, elementTypes[0], elementTypeAdapter, constructor); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 54 | + protected TypeAdapter<?> create(Gson gson, Type elementType, TypeAdapter<?> elementTypeAdapter, Supplier<Collection<Object>> constructor) { |
| 55 | + return new CollectionTypeAdapter(gson, elementType, elementTypeAdapter, constructor); |
| 56 | + } |
| 57 | + |
| 58 | + protected <E> Supplier<Collection<E>> getConstructor(Class<Collection<E>> rawType) { |
| 59 | + try { |
| 60 | + Constructor<Collection<E>> constructor = rawType.getDeclaredConstructor(); |
| 61 | + if (!constructor.isAccessible()) |
| 62 | + constructor.setAccessible(true); |
| 63 | + return () -> { |
| 64 | + try { |
| 65 | + return constructor.newInstance(); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new RuntimeException(e); |
| 68 | + } |
| 69 | + }; |
| 70 | + } catch (NoSuchMethodException e) { |
| 71 | + if (SortedSet.class.isAssignableFrom(rawType)) { |
| 72 | + return () -> { |
| 73 | + return new TreeSet<E>(); |
| 74 | + }; |
| 75 | + } else if (Set.class.isAssignableFrom(rawType)) { |
| 76 | + return () -> { |
| 77 | + return new LinkedHashSet<E>(); |
| 78 | + }; |
| 79 | + } else if (Queue.class.isAssignableFrom(rawType)) { |
| 80 | + return () -> { |
| 81 | + return new LinkedList<E>(); |
| 82 | + }; |
| 83 | + } else { |
| 84 | + return () -> { |
| 85 | + return new ArrayList<E>(); |
| 86 | + }; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + private final Gson gson; |
| 94 | + private final Type elementType; |
| 95 | + private final TypeAdapter<E> elementTypeAdapter; |
| 96 | + private final Supplier<Collection<E>> constructor; |
| 97 | + |
| 98 | + public CollectionTypeAdapter(Gson gson, Type elementType, TypeAdapter<E> elementTypeAdapter, Supplier<Collection<E>> constructor) { |
| 99 | + this.gson = gson; |
| 100 | + this.elementType = elementType; |
| 101 | + this.elementTypeAdapter = elementTypeAdapter; |
| 102 | + this.constructor = constructor; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Collection<E> read(JsonReader in) throws IOException { |
| 107 | + JsonToken peek = in.peek(); |
| 108 | + if (peek == JsonToken.NULL) { |
| 109 | + in.nextNull(); |
| 110 | + return null; |
| 111 | + } else if (peek == JsonToken.BEGIN_ARRAY) { |
| 112 | + Collection<E> collection = constructor.get(); |
| 113 | + in.beginArray(); |
| 114 | + while (in.hasNext()) { |
| 115 | + E instance = elementTypeAdapter.read(in); |
| 116 | + collection.add(instance); |
| 117 | + } |
| 118 | + in.endArray(); |
| 119 | + return collection; |
| 120 | + } else { |
| 121 | + Collection<E> collection = constructor.get(); |
| 122 | + E instance = elementTypeAdapter.read(in); |
| 123 | + collection.add(instance); |
| 124 | + return collection; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void write(JsonWriter out, Collection<E> collection) throws IOException { |
| 130 | + if (collection == null) { |
| 131 | + out.nullValue(); |
| 132 | + return; |
| 133 | + } |
| 134 | + out.beginArray(); |
| 135 | + for (E element : collection) { |
| 136 | + if (element != null && elementType != element.getClass() |
| 137 | + && (elementType instanceof TypeVariable<?> || elementType instanceof Class<?>)) { |
| 138 | + @SuppressWarnings("unchecked") |
| 139 | + TypeAdapter<E> runtimeTypeAdapter = (TypeAdapter<E>) gson.getAdapter(TypeToken.get(element.getClass())); |
| 140 | + runtimeTypeAdapter.write(out, element); |
| 141 | + } else { |
| 142 | + elementTypeAdapter.write(out, element); |
| 143 | + } |
| 144 | + } |
| 145 | + out.endArray(); |
| 146 | + } |
| 147 | +} |
0 commit comments