Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions gson/src/test/java/com/google/gson/internal/GsonTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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 {

public <T> TypeVariableTest(T parameter) {}

public <T> T method() {
return null;
}

}

}
Loading