org.mockito.internal.invocation.mockref.MockWeakReference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-core Show documentation
Show all versions of mockito-core Show documentation
Mockito mock objects library core API and implementation
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation.mockref;
import java.io.ObjectStreamException;
import java.lang.ref.WeakReference;
/**
* A weak reference that is converted into a strong reference when serialized.
* See {@link MockReference}.
*/
public class MockWeakReference implements MockReference {
private final WeakReference ref;
public MockWeakReference(T t) {
this.ref = new WeakReference(t);
}
private Object writeReplace() throws ObjectStreamException {
return new MockStrongReference(get(), true);
}
@Override
public T get() {
T ref = this.ref.get();
if (ref == null) {
throw new IllegalStateException(
"The mock object was garbage collected. "
+ "This should not happen in normal circumstances when using public API. "
+ "Typically, the test class keeps strong reference to the mock object "
+ "and it prevents getting the mock collected. Mockito internally needs "
+ "to keep weak references to mock objects to avoid memory leaks for "
+ "certain types of MockMaker implementations. If you see this exception "
+ "using Mockito public API, please file a bug. For more information see "
+ "issue #1313.");
}
return ref;
}
}