Skip to content

Commit

Permalink
Remove special handling for hashCode/equals as it breaks Mockito's in…
Browse files Browse the repository at this point in the history
…ternal model.
  • Loading branch information
raphw committed Aug 24, 2021
1 parent 4f81d4f commit 1a8946f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
Expand Up @@ -44,7 +44,6 @@
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
import net.bytebuddy.matcher.ElementMatcher;
import org.mockito.Answers;
import org.mockito.codegen.InjectionBase;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport.CrossClassLoaderSerializableMock;
Expand Down Expand Up @@ -241,21 +240,11 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
.serialVersionUid(42L)
.defineField("mockitoInterceptor", MockMethodInterceptor.class, PRIVATE)
.implement(MockAccess.class)
.intercept(FieldAccessor.ofBeanProperty());

if (features.defaultAnswer != Answers.CALLS_REAL_METHODS) {
builder =
builder.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
} else {
builder =
builder.method(isHashCode())
.intercept(dispatcher)
.method(isEquals())
.intercept(dispatcher);
}
.intercept(FieldAccessor.ofBeanProperty())
.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
if (features.serializableMode == SerializableMode.ACROSS_CLASSLOADERS) {
builder =
builder.implement(CrossClassLoaderSerializableMock.class)
Expand Down
Expand Up @@ -45,8 +45,7 @@ public <T> Class<T> mockClass(final MockFeatures<T> params) {
params.mockedType,
params.interfaces,
params.serializableMode,
params.stripAnnotations,
params.defaultAnswer),
params.stripAnnotations),
() -> bytecodeGenerator.mockClass(params),
BOOTSTRAP_LOCK);
} catch (IllegalArgumentException exception) {
Expand Down Expand Up @@ -86,18 +85,15 @@ private static class MockitoMockKey extends TypeCache.SimpleKey {

private final SerializableMode serializableMode;
private final boolean stripAnnotations;
private final Answer defaultAnswer;

private MockitoMockKey(
Class<?> type,
Set<Class<?>> additionalType,
SerializableMode serializableMode,
boolean stripAnnotations,
Answer defaultAnswer) {
boolean stripAnnotations) {
super(type, additionalType);
this.serializableMode = serializableMode;
this.stripAnnotations = stripAnnotations;
this.defaultAnswer = defaultAnswer;
}

@Override
Expand All @@ -113,16 +109,14 @@ public boolean equals(Object object) {
}
MockitoMockKey that = (MockitoMockKey) object;
return stripAnnotations == that.stripAnnotations
&& serializableMode.equals(that.serializableMode)
&& Objects.equals(defaultAnswer, that.defaultAnswer);
&& serializableMode.equals(that.serializableMode);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (stripAnnotations ? 1 : 0);
result = 31 * result + serializableMode.hashCode();
result = 31 * result + Objects.hashCode(defaultAnswer);
return result;
}
}
Expand Down

1 comment on commit 1a8946f

@cdalexndr
Copy link

@cdalexndr cdalexndr commented on 1a8946f Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks spies calling real equals/hashcode method #2331

Please sign in to comment.