Fix $Gson$Types equals method for TypeVariable when its generic declaration is not a Class (#2599)

* Fix $Gson$Types equals method for TypeVariable when its generic declaration is not a Class

* Test $Gson$Types equals method with TypeVariable when its generic declaration is not a Class

* Add @SuppressWarnings in GsonTypesTest.java
This commit is contained in:
William 2024-01-30 23:29:07 +01:00 committed by GitHub
parent 12406d04dc
commit 11b2732412
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -217,7 +217,7 @@ public final class $Gson$Types {
}
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 {

View File

@ -19,6 +19,8 @@ package com.google.gson.internal;
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 final class GsonTypesTest {
}
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<TypeVariableTest> c1 = TypeVariableTest.class.getConstructor(Object.class);
Constructor<TypeVariableTest> 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 {
@SuppressWarnings({"UnusedMethod", "UnusedVariable", "TypeParameterUnusedInFormals"})
public <T> TypeVariableTest(T parameter) {}
@SuppressWarnings({"UnusedMethod", "UnusedVariable", "TypeParameterUnusedInFormals"})
public <T> T method() {
return null;
}
}
}