Skip to content

Commit 6160932

Browse files
Fix a class loading deadlock involving TypeAdapters. (google#2740)
* Fix a class loading deadlock involving `TypeAdapters`. Fixes google#2739. * Remove deprecation of internal fields. The idea was to discourage people from using these, but they have been there for a long time and the deprecation isn't going to have much effect. Meanwhile the deprecation required `@SuppressWarnings`, which was annoying. Also move `JsonElementTypeFactory.ADAPTER` back into `TypeAdapters` so there is no further circular dependency between the two classes. I think it was harmless but it's easy enough to avoid. * Omit unnecessary type qualificiation.
1 parent 48889db commit 6160932

File tree

5 files changed

+12
-46
lines changed

5 files changed

+12
-46
lines changed

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import com.google.gson.internal.bind.ArrayTypeAdapter;
2727
import com.google.gson.internal.bind.CollectionTypeAdapterFactory;
2828
import com.google.gson.internal.bind.DefaultDateTypeAdapter;
29-
import com.google.gson.internal.bind.EnumTypeAdapter;
3029
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
31-
import com.google.gson.internal.bind.JsonElementTypeAdapter;
3230
import com.google.gson.internal.bind.JsonTreeReader;
3331
import com.google.gson.internal.bind.JsonTreeWriter;
3432
import com.google.gson.internal.bind.MapTypeAdapterFactory;
@@ -325,7 +323,7 @@ public Gson() {
325323
List<TypeAdapterFactory> factories = new ArrayList<>();
326324

327325
// built-in type adapters that cannot be overridden
328-
factories.add(JsonElementTypeAdapter.FACTORY);
326+
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
329327
factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy));
330328

331329
// the excluder must precede all adapters that handle user-defined types
@@ -388,7 +386,7 @@ public Gson() {
388386
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
389387
this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
390388
factories.add(jsonAdapterFactory);
391-
factories.add(EnumTypeAdapter.FACTORY);
389+
factories.add(TypeAdapters.ENUM_FACTORY);
392390
factories.add(
393391
new ReflectiveTypeAdapterFactory(
394392
constructorConstructor,

gson/src/main/java/com/google/gson/internal/Streams.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.gson.JsonNull;
2222
import com.google.gson.JsonParseException;
2323
import com.google.gson.JsonSyntaxException;
24-
import com.google.gson.internal.bind.JsonElementTypeAdapter;
24+
import com.google.gson.internal.bind.TypeAdapters;
2525
import com.google.gson.stream.JsonReader;
2626
import com.google.gson.stream.JsonToken;
2727
import com.google.gson.stream.JsonWriter;
@@ -43,7 +43,7 @@ public static JsonElement parse(JsonReader reader) throws JsonParseException {
4343
try {
4444
JsonToken unused = reader.peek();
4545
isEmpty = false;
46-
return JsonElementTypeAdapter.ADAPTER.read(reader);
46+
return TypeAdapters.JSON_ELEMENT.read(reader);
4747
} catch (EOFException e) {
4848
/*
4949
* For compatibility with JSON 1.5 and earlier, we return a JsonNull for
@@ -65,7 +65,7 @@ public static JsonElement parse(JsonReader reader) throws JsonParseException {
6565

6666
/** Writes the JSON element to the writer, recursively. */
6767
public static void write(JsonElement element, JsonWriter writer) throws IOException {
68-
JsonElementTypeAdapter.ADAPTER.write(writer, element);
68+
TypeAdapters.JSON_ELEMENT.write(writer, element);
6969
}
7070

7171
public static Writer writerForAppendable(Appendable appendable) {

gson/src/main/java/com/google/gson/internal/bind/EnumTypeAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import java.util.Map;
3333

3434
/** Adapter for enum classes (but not for the base class {@code java.lang.Enum}). */
35-
public class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
36-
public static final TypeAdapterFactory FACTORY =
35+
class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
36+
static final TypeAdapterFactory FACTORY =
3737
new TypeAdapterFactory() {
3838
@Override
3939
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {

gson/src/main/java/com/google/gson/internal/bind/JsonElementTypeAdapter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.gson.JsonObject;
2323
import com.google.gson.JsonPrimitive;
2424
import com.google.gson.TypeAdapter;
25-
import com.google.gson.TypeAdapterFactory;
2625
import com.google.gson.internal.LazilyParsedNumber;
2726
import com.google.gson.stream.JsonReader;
2827
import com.google.gson.stream.JsonToken;
@@ -33,11 +32,8 @@
3332
import java.util.Map;
3433

3534
/** Adapter for {@link JsonElement} and subclasses. */
36-
public class JsonElementTypeAdapter extends TypeAdapter<JsonElement> {
37-
public static final JsonElementTypeAdapter ADAPTER = new JsonElementTypeAdapter();
38-
39-
public static final TypeAdapterFactory FACTORY =
40-
TypeAdapters.newTypeHierarchyFactory(JsonElement.class, ADAPTER);
35+
class JsonElementTypeAdapter extends TypeAdapter<JsonElement> {
36+
static final JsonElementTypeAdapter ADAPTER = new JsonElementTypeAdapter();
4137

4238
private JsonElementTypeAdapter() {}
4339

gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -806,40 +806,12 @@ public void write(JsonWriter out, Locale value) throws IOException {
806806

807807
public static final TypeAdapterFactory LOCALE_FACTORY = newFactory(Locale.class, LOCALE);
808808

809-
/*
810-
* The following adapter and factory fields have not been removed yet and are only deprecated
811-
* for now because external projects might be using them, despite being part of Gson's internal
812-
* implementation.
813-
*/
814-
815-
/**
816-
* @deprecated {@code TypeAdapters} is an internal Gson class. To obtain the adapter for {@link
817-
* JsonElement} and subclasses use instead:
818-
* <pre>{@code
819-
* TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);
820-
* }</pre>
821-
*/
822-
@Deprecated
823809
public static final TypeAdapter<JsonElement> JSON_ELEMENT = JsonElementTypeAdapter.ADAPTER;
824810

825-
/**
826-
* @deprecated {@code TypeAdapters} is an internal Gson class. To obtain the adapter for {@link
827-
* JsonElement} and subclasses use instead:
828-
* <pre>{@code
829-
* TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);
830-
* }</pre>
831-
*/
832-
@Deprecated
833-
public static final TypeAdapterFactory JSON_ELEMENT_FACTORY = JsonElementTypeAdapter.FACTORY;
811+
public static final TypeAdapterFactory JSON_ELEMENT_FACTORY =
812+
newTypeHierarchyFactory(JsonElement.class, JSON_ELEMENT);
834813

835-
/**
836-
* @deprecated {@code TypeAdapters} is an internal Gson class. To obtain the adapter for a
837-
* specific enum class use instead:
838-
* <pre>{@code
839-
* TypeAdapter<MyEnum> adapter = gson.getAdapter(MyEnum.class);
840-
* }</pre>
841-
*/
842-
@Deprecated public static final TypeAdapterFactory ENUM_FACTORY = EnumTypeAdapter.FACTORY;
814+
public static final TypeAdapterFactory ENUM_FACTORY = EnumTypeAdapter.FACTORY;
843815

844816
@SuppressWarnings("TypeParameterNaming")
845817
public static <TT> TypeAdapterFactory newFactory(

0 commit comments

Comments
 (0)