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
This commit is contained in:
Stéphane Nicoll 2024-06-11 12:03:31 +02:00
parent 1be92f82af
commit 2a680934ee
2 changed files with 2 additions and 19 deletions

View File

@ -163,7 +163,7 @@ public abstract class OverrideMetadata {
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 abstract class OverrideMetadata {
@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())));
}

View File

@ -98,23 +98,6 @@ class MockitoSpyBeanOverrideMetadata extends MockitoOverrideMetadata {
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)