From b2e191e7eebcc9b6edcaf0358771ada73da20cca Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Thu, 28 Nov 2024 16:30:52 +0100 Subject: [PATCH] Adds test where elements from 2 generic types are cast to their type --- .../extended_contract/GenericTypesTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java index 554c4ab08..43bddc2f6 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java @@ -90,6 +90,11 @@ public void succeed_whenToStringLooksAtNonCollectionGenericContent() { EqualsVerifier.forClass(SparseArrayToStringContainer.class).verify(); } + @Test + public void succeed_whenEqualsLooksAtGenericContent_givenTwoGenericFields() { + EqualsVerifier.forClass(TwoGenericsContainerWithIntrospection.class).verify(); + } + @Test public void succeed_whenClassHasTypeVariableThatExtendsSomething() { EqualsVerifier.forClass(TypeVariableExtendsContainer.class).verify(); @@ -745,6 +750,38 @@ public String toString() { } } + public static final class TwoGenericsContainerWithIntrospection { + + private final List stringList = new ArrayList<>(); + private final List intList = new ArrayList<>(); + + @SuppressWarnings("unused") + @Override + public boolean equals(Object obj) { + if (stringList != null && stringList.size() > 0) { + String key = stringList.get(0); // force a cast + } + if (intList != null && intList.size() > 0) { + Integer key = intList.get(0); // force a cast + } + + if (!(obj instanceof TwoGenericsContainerWithIntrospection)) { + return false; + } + TwoGenericsContainerWithIntrospection other = + (TwoGenericsContainerWithIntrospection) obj; + return ( + Objects.equals(stringList, other.stringList) && + Objects.equals(intList, other.intList) + ); + } + + @Override + public int hashCode() { + return Objects.hash(stringList, intList); + } + } + @SuppressWarnings("unused") static final class TypeVariableExtendsContainer> {