use of java.lang.ref.PhantomReference in project mockito by mockito.
the class TypeCachingMockBytecodeGeneratorTest method ensure_cache_is_cleared_if_no_reference_to_classloader_and_classes.
@Test
public void ensure_cache_is_cleared_if_no_reference_to_classloader_and_classes() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache = inMemoryClassLoader().withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar")).build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator = new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Class<?> the_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(classloader_with_life_shorter_than_cache.loadClass("foo.Bar"), Collections.<Class<?>>emptySet(), SerializableMode.NONE));
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
Reference<Object> typeReference = new PhantomReference<Object>(the_mock_type, referenceQueue);
// when
classloader_with_life_shorter_than_cache = is_no_more_referenced();
the_mock_type = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(referenceQueue.poll()).isEqualTo(typeReference);
}
use of java.lang.ref.PhantomReference in project jdk8u_jdk by JetBrains.
the class LFGarbageCollectedTest method doTest.
@Override
public void doTest() {
try {
TestMethods testCase = getTestMethod();
data = testCase.getTestCaseData();
MethodHandle adapter;
try {
adapter = testCase.getTestCaseMH(data, TestMethods.Kind.ONE);
} catch (NoSuchMethodException ex) {
throw new Error("Unexpected exception", ex);
}
mtype = adapter.type();
Object lambdaForm = INTERNAL_FORM.invoke(adapter);
if (lambdaForm == null) {
throw new Error("Unexpected error: Lambda form of the method handle is null");
}
String debugName = (String) DEBUG_NAME.get(lambdaForm);
if (debugName != null && debugName.startsWith("identity_")) {
// Ignore identity_* LambdaForms.
return;
}
ph = new PhantomReference(lambdaForm, rq);
lambdaForm = null;
adapter = null;
collectLambdaForm();
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Error("Unexpected exception", ex);
}
}
Aggregations