From 1096f78caad0e61173065f96c594ca93e032806b Mon Sep 17 00:00:00 2001 From: William Degrange Date: Sun, 14 Jan 2024 23:26:54 +0100 Subject: [PATCH 1/3] Fix $Gson$Types equals method for TypeVariable when its generic declaration is not a Class --- gson/src/main/java/com/google/gson/internal/$Gson$Types.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java index fc6b1a27d8..6ccdaa3eb5 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java @@ -217,7 +217,7 @@ public static boolean equals(Type a, Type b) { } TypeVariable va = (TypeVariable) a; TypeVariable vb = (TypeVariable) b; - return va.getGenericDeclaration() == vb.getGenericDeclaration() + return Objects.equals(va.getGenericDeclaration(), vb.getGenericDeclaration()) && va.getName().equals(vb.getName()); } else { From 4b9b2eb199d1d94db60c699cbd0bef4cebbb1902 Mon Sep 17 00:00:00 2001 From: William Degrange Date: Mon, 22 Jan 2024 18:59:44 +0100 Subject: [PATCH 2/3] Test $Gson$Types equals method with TypeVariable when its generic declaration is not a Class --- .../google/gson/internal/GsonTypesTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java index 1e9d613e4a..9ab5f9e277 100644 --- a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java +++ b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java @@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; @@ -98,4 +100,37 @@ public static Type getFirstTypeArgument(Type type) throws Exception { } return $Gson$Types.canonicalize(actualTypeArguments[0]); } + + @Test + public void testEqualsOnMethodTypeVariables() throws Exception { + Method m1 = TypeVariableTest.class.getMethod("method"); + Method m2 = TypeVariableTest.class.getMethod("method"); + + Type rt1 = m1.getGenericReturnType(); + Type rt2 = m2.getGenericReturnType(); + + assertThat($Gson$Types.equals(rt1, rt2)).isTrue(); + } + + @Test + public void testEqualsOnConstructorParameterTypeVariables() throws Exception { + Constructor c1 = TypeVariableTest.class.getConstructor(Object.class); + Constructor c2 = TypeVariableTest.class.getConstructor(Object.class); + + Type rt1 = c1.getGenericParameterTypes()[0]; + Type rt2 = c2.getGenericParameterTypes()[0]; + + assertThat($Gson$Types.equals(rt1, rt2)).isTrue(); + } + + private static final class TypeVariableTest { + + public TypeVariableTest(T parameter) {} + + public T method() { + return null; + } + + } + } From adcf98f9065b110cd880f4d179db8712de718bd4 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 29 Jan 2024 17:38:25 +0100 Subject: [PATCH 3/3] Add @SuppressWarnings in GsonTypesTest.java --- gson/src/test/java/com/google/gson/internal/GsonTypesTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java index 9ab5f9e277..245b859326 100644 --- a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java +++ b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java @@ -125,8 +125,10 @@ public void testEqualsOnConstructorParameterTypeVariables() throws Exception { private static final class TypeVariableTest { + @SuppressWarnings({"UnusedMethod", "UnusedVariable", "TypeParameterUnusedInFormals"}) public TypeVariableTest(T parameter) {} + @SuppressWarnings({"UnusedMethod", "UnusedVariable", "TypeParameterUnusedInFormals"}) public T method() { return null; }