Search in sources :

Example 11 with PhantomReference

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);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) ClassLoaders.inMemoryClassLoader(org.mockitoutil.ClassLoaders.inMemoryClassLoader) Test(org.junit.Test)

Example 12 with PhantomReference

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);
    }
}
Also used : PhantomReference(java.lang.ref.PhantomReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

PhantomReference (java.lang.ref.PhantomReference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)8 WeakReference (java.lang.ref.WeakReference)5 Reference (java.lang.ref.Reference)4 SoftReference (java.lang.ref.SoftReference)4 Test (org.junit.Test)2 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MethodHandle (java.lang.invoke.MethodHandle)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1