From 2a680934eeb23d2c5e95be59301c7e5bb2a7310a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Tue, 11 Jun 2024 12:03:31 +0200 Subject: [PATCH] Harmonize equals/hashCode of OverrideMetadata to use class identity This commit harmonizes the equals/hashCode behavior of OverrideMetadata to always take the implementation class as a factor for its identity. This is important as two OverrideMetadata implementations could use the same strategy and other settings while creating the override value in a totally different way. This commit makes sure they are identified as different. Closes gh-33005 --- .../context/bean/override/OverrideMetadata.java | 4 ++-- .../mockito/MockitoSpyBeanOverrideMetadata.java | 17 ----------------- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java index 70510fa3796f..dcda79bcf750 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java @@ -163,7 +163,7 @@ public boolean equals(Object other) { if (other == this) { return true; } - if (other == null || !getClass().isAssignableFrom(other.getClass())) { + if (other == null || other.getClass() != getClass()) { return false; } OverrideMetadata that = (OverrideMetadata) other; @@ -182,7 +182,7 @@ public boolean equals(Object other) { @Override public int hashCode() { - int hash = Objects.hash(this.beanType.getType(), this.beanName, this.strategy); + int hash = Objects.hash(getClass().hashCode(), this.beanType.getType(), this.beanName, this.strategy); return (this.beanName != null ? hash : hash + Objects.hash(this.field.getName(), Arrays.hashCode(this.field.getAnnotations()))); } diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java index 5078a5dab59b..931b92c4b323 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java @@ -98,23 +98,6 @@ T createSpy(String name, Object instance) { return (T) mock(toSpy, settings); } - @Override - public boolean equals(@Nullable Object other) { - if (other == this) { - return true; - } - // For SpyBean we want the class to be exactly the same. - if (other == null || other.getClass() != getClass()) { - return false; - } - return super.equals(other); - } - - @Override - public int hashCode() { - return getClass().hashCode() * 29 + super.hashCode(); - } - @Override public String toString() { return new ToStringCreator(this)