FinalizeManager { public static <T> Reference<T> register( T reference, FinalizeAction finalizeAction, ReferenceFactory referenceFactory) { Reference<T> newReference = referenceFactory.createReference( reference, _referenceQueue); _finalizeActions.put(new IdentityKey(newReference), finalizeAction); return newReference; }  static Reference<T> register(
		T reference, FinalizeAction finalizeAction,
		ReferenceFactory referenceFactory); static final ReferenceFactory PHANTOM_REFERENCE_FACTORY; static final ReferenceFactory SOFT_REFERENCE_FACTORY; static final ReferenceFactory WEAK_REFERENCE_FACTORY; }
@Test public void testManualClear() throws Exception { Object object1 = new Object(); MarkFinalizeAction markFinalizeAction1 = new MarkFinalizeAction(); Reference<Object> reference1 = FinalizeManager.register( object1, markFinalizeAction1, FinalizeManager.WEAK_REFERENCE_FACTORY); Map<Object, FinalizeAction> finalizeActions = ReflectionTestUtil.getFieldValue( FinalizeManager.class, "_finalizeActions"); Assert.assertEquals( markFinalizeAction1, finalizeActions.get(_newIdentityKey(reference1))); reference1.clear(); Assert.assertNull(finalizeActions.get(_newIdentityKey(reference1))); object1 = null; Object object2 = new Object(); MarkFinalizeAction markFinalizeAction2 = new MarkFinalizeAction(); Reference<Object> reference2 = FinalizeManager.register( object2, markFinalizeAction2, FinalizeManager.WEAK_REFERENCE_FACTORY); Assert.assertEquals( markFinalizeAction2, finalizeActions.get(_newIdentityKey(reference2))); reference1.enqueue(); object2 = null; GCUtil.gc(true); _waitUntilMarked(markFinalizeAction2); Assert.assertFalse(markFinalizeAction1.isMarked()); }
@Test public void testRegisterationIdentity() throws Exception { String testString = new String("testString"); MarkFinalizeAction markFinalizeAction = new MarkFinalizeAction(); Reference<?> reference1 = FinalizeManager.register( testString, markFinalizeAction, FinalizeManager.SOFT_REFERENCE_FACTORY); Map<Object, FinalizeAction> finalizeActions = ReflectionTestUtil.getFieldValue( FinalizeManager.class, "_finalizeActions"); Assert.assertEquals( finalizeActions.toString(), 1, finalizeActions.size()); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference1))); Reference<?> reference2 = FinalizeManager.register( testString, markFinalizeAction, FinalizeManager.SOFT_REFERENCE_FACTORY); Assert.assertEquals(reference1, reference2); Assert.assertNotSame(reference1, reference2); Assert.assertEquals( finalizeActions.toString(), 2, finalizeActions.size()); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference1))); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference2))); reference2.clear(); Assert.assertEquals( finalizeActions.toString(), 1, finalizeActions.size()); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference1))); reference2 = FinalizeManager.register( new String(testString), markFinalizeAction, FinalizeManager.SOFT_REFERENCE_FACTORY); Assert.assertEquals( finalizeActions.toString(), 2, finalizeActions.size()); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference1))); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference2))); reference2.clear(); Assert.assertEquals( finalizeActions.toString(), 1, finalizeActions.size()); Assert.assertTrue( finalizeActions.containsKey(_newIdentityKey(reference1))); reference1.clear(); Assert.assertTrue( finalizeActions.toString(), finalizeActions.isEmpty()); }