From 02185489561d6c9ba273da03491355721ff93b20 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Fri, 25 Aug 2023 14:15:11 -0500 Subject: [PATCH] #2436 - Updates to User guide & comments to indicate exception cases and fix for 7 test cases of Parameterized Type --- UserGuide.md | 4 +++- gson/src/main/java/com/google/gson/GsonBuilder.java | 11 ++++++++--- .../test/java/com/google/gson/GsonBuilderTest.java | 5 ++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/UserGuide.md b/UserGuide.md index f519865434..3a42653bdb 100644 --- a/UserGuide.md +++ b/UserGuide.md @@ -405,7 +405,9 @@ gson.registerTypeAdapter(MyType.class, new MyDeserializer()); gson.registerTypeAdapter(MyType.class, new MyInstanceCreator()); ``` -`registerTypeAdapter` call checks if the type adapter implements more than one of these interfaces and register it for all of them. +`registerTypeAdapter` call checks +1. if the type adapter implements more than one of these interfaces and register it for all of them. +2. if the type adapter is for Object class or (JsonElements or any of its subclasses), it throws IllegalArgumentException. #### Writing a Serializer diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index f0c346f834..ccd37ba2b0 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -41,6 +41,8 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; + +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.DateFormat; import java.util.ArrayDeque; @@ -337,7 +339,7 @@ public GsonBuilder enableComplexMapKeySerialization() { * *

In general using inner classes with Gson should be avoided; they should be converted to {@code static} * nested classes if possible. - * + *Ø * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ @@ -664,6 +666,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern + * @throws IllegalArgumentException if the Type adapter being registered is for Object class or (JsonElements or any of its subclasses) */ @CanIgnoreReturnValue public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { @@ -689,8 +692,9 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { } private boolean isTypeObjectOrJsonElements(Type type) { - return (type == Object.class - || JsonElement.class.isAssignableFrom((Class) type)); + return (!(type instanceof ParameterizedType) && + (type == Object.class + || JsonElement.class.isAssignableFrom((Class) type))); } /** @@ -724,6 +728,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, * {@link JsonSerializer} or {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern + * @throws IllegalArgumentException if the Type adapter being registered is for a JsonElements or any of its subclasses * @since 1.7 */ @CanIgnoreReturnValue diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 48b43aaf6c..2a50598b31 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -184,8 +184,7 @@ public void testRegisterTypeAdapterForCoreType() { String.class, }; for (Type type : types) { - GsonBuilder gsonBuilder = new GsonBuilder(); - assertThat(gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder); + new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER); } } @@ -282,6 +281,6 @@ public void testRegisterTypeHierarchyAdapterJsonElements() { assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); } - assertThat(gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder); + gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); } }