Skip to content

Commit 2a68093

Browse files
committed
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
1 parent 1be92f8 commit 2a68093

File tree

2 files changed

+2
-19
lines changed

2 files changed

+2
-19
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public boolean equals(Object other) {
163163
if (other == this) {
164164
return true;
165165
}
166-
if (other == null || !getClass().isAssignableFrom(other.getClass())) {
166+
if (other == null || other.getClass() != getClass()) {
167167
return false;
168168
}
169169
OverrideMetadata that = (OverrideMetadata) other;
@@ -182,7 +182,7 @@ public boolean equals(Object other) {
182182

183183
@Override
184184
public int hashCode() {
185-
int hash = Objects.hash(this.beanType.getType(), this.beanName, this.strategy);
185+
int hash = Objects.hash(getClass().hashCode(), this.beanType.getType(), this.beanName, this.strategy);
186186
return (this.beanName != null ? hash : hash +
187187
Objects.hash(this.field.getName(), Arrays.hashCode(this.field.getAnnotations())));
188188
}

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java

-17
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,6 @@ <T> T createSpy(String name, Object instance) {
9898
return (T) mock(toSpy, settings);
9999
}
100100

101-
@Override
102-
public boolean equals(@Nullable Object other) {
103-
if (other == this) {
104-
return true;
105-
}
106-
// For SpyBean we want the class to be exactly the same.
107-
if (other == null || other.getClass() != getClass()) {
108-
return false;
109-
}
110-
return super.equals(other);
111-
}
112-
113-
@Override
114-
public int hashCode() {
115-
return getClass().hashCode() * 29 + super.hashCode();
116-
}
117-
118101
@Override
119102
public String toString() {
120103
return new ToStringCreator(this)

0 commit comments

Comments
 (0)