Skip to content

Commit

Permalink
Fix a class loading deadlock involving TypeAdapters.
Browse files Browse the repository at this point in the history
Fixes #2739.
  • Loading branch information
eamonnmcmanus committed Sep 13, 2024
1 parent 48889db commit e96abc5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
10 changes: 6 additions & 4 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.google.gson.internal.bind.ArrayTypeAdapter;
import com.google.gson.internal.bind.CollectionTypeAdapterFactory;
import com.google.gson.internal.bind.DefaultDateTypeAdapter;
import com.google.gson.internal.bind.EnumTypeAdapter;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.google.gson.internal.bind.JsonElementTypeAdapter;
import com.google.gson.internal.bind.JsonTreeReader;
import com.google.gson.internal.bind.JsonTreeWriter;
import com.google.gson.internal.bind.MapTypeAdapterFactory;
Expand Down Expand Up @@ -325,7 +323,9 @@ public Gson() {
List<TypeAdapterFactory> factories = new ArrayList<>();

// built-in type adapters that cannot be overridden
factories.add(JsonElementTypeAdapter.FACTORY);
@SuppressWarnings("deprecation")
TypeAdapterFactory jsonElementFactory = TypeAdapters.JSON_ELEMENT_FACTORY;
factories.add(jsonElementFactory);
factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy));

// the excluder must precede all adapters that handle user-defined types
Expand Down Expand Up @@ -388,7 +388,9 @@ public Gson() {
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
factories.add(jsonAdapterFactory);
factories.add(EnumTypeAdapter.FACTORY);
@SuppressWarnings("deprecation")
TypeAdapterFactory enumFactory = TypeAdapters.ENUM_FACTORY;
factories.add(enumFactory);
factories.add(
new ReflectiveTypeAdapterFactory(
constructorConstructor,
Expand Down
9 changes: 6 additions & 3 deletions gson/src/main/java/com/google/gson/internal/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.bind.JsonElementTypeAdapter;
import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
Expand All @@ -43,7 +43,9 @@ public static JsonElement parse(JsonReader reader) throws JsonParseException {
try {
JsonToken unused = reader.peek();
isEmpty = false;
return JsonElementTypeAdapter.ADAPTER.read(reader);
@SuppressWarnings("deprecation")
JsonElement element = TypeAdapters.JSON_ELEMENT.read(reader);
return element;
} catch (EOFException e) {
/*
* For compatibility with JSON 1.5 and earlier, we return a JsonNull for
Expand All @@ -64,8 +66,9 @@ public static JsonElement parse(JsonReader reader) throws JsonParseException {
}

/** Writes the JSON element to the writer, recursively. */
@SuppressWarnings("deprecation")
public static void write(JsonElement element, JsonWriter writer) throws IOException {
JsonElementTypeAdapter.ADAPTER.write(writer, element);
TypeAdapters.JSON_ELEMENT.write(writer, element);
}

public static Writer writerForAppendable(Appendable appendable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.util.Map;

/** Adapter for enum classes (but not for the base class {@code java.lang.Enum}). */
public class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
public static final TypeAdapterFactory FACTORY =
class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
static final TypeAdapterFactory FACTORY =
new TypeAdapterFactory() {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import java.util.Map;

/** Adapter for {@link JsonElement} and subclasses. */
public class JsonElementTypeAdapter extends TypeAdapter<JsonElement> {
public static final JsonElementTypeAdapter ADAPTER = new JsonElementTypeAdapter();
class JsonElementTypeAdapter extends TypeAdapter<JsonElement> {
static final JsonElementTypeAdapter ADAPTER = new JsonElementTypeAdapter();

public static final TypeAdapterFactory FACTORY =
static final TypeAdapterFactory FACTORY =
TypeAdapters.newTypeHierarchyFactory(JsonElement.class, ADAPTER);

private JsonElementTypeAdapter() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,6 @@ public void write(JsonWriter out, Locale value) throws IOException {

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

/*
* The following adapter and factory fields have not been removed yet and are only deprecated
* for now because external projects might be using them, despite being part of Gson's internal
* implementation.
*/

/**
* @deprecated {@code TypeAdapters} is an internal Gson class. To obtain the adapter for {@link
* JsonElement} and subclasses use instead:
Expand Down

0 comments on commit e96abc5

Please sign in to comment.