Skip to content

Package: GCCollectable

GCCollectable

nameinstructionbranchcomplexitylinemethod
GCCollectable(Object)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getPhantomRef()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getQueue()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isCollectable()
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 3
75%
M: 0 C: 1
100%
isResult()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setResult(boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.test.common.spi;
15:
16: import java.lang.ref.PhantomReference;
17: import java.lang.ref.ReferenceQueue;
18:
19: /**
20: * Convenience class for expecting objects to be garbage collected. Because GC
21: * is non-deterministic there's no time constrained guarantee, when
22: * {@code isCollectable} returns true.
23: *
24: * @author emueller
25: */
26: public class GCCollectable {
27:
28:         private final ReferenceQueue<Object> queue;
29:         private final PhantomReference<Object> phantomRef;
30:
31:         private boolean result;
32:         private GCPhantomThread t;
33:
34:         /**
35:          * Constructor.
36:          *
37:          * @param obj
38:          * the object that is supposed to be finalized
39:          */
40:         public GCCollectable(Object obj) {
41:                 queue = new ReferenceQueue<Object>();
42:                 phantomRef = new PhantomReference<Object>(obj, getQueue());
43:                 setResult(false);
44:                 t = new GCPhantomThread(queue, phantomRef);
45:                 t.start();
46:         }
47:
48:         /**
49:          * Checks whether the the object contained by the {@link GCCollectable} may
50:          * be finalized.
51:          *
52:          * @return {@code true}, if the contained object may be finalized,
53:          * {@code false} otherwise
54:          */
55:         public boolean isCollectable() {
56:                 try {
57:                         t.join();
58:                 } catch (InterruptedException e) {
59:                         return false;
60:                 }
61:                 return t.didCollectReference();
62:         }
63:
64:         public synchronized ReferenceQueue<Object> getQueue() {
65:                 return queue;
66:         }
67:
68:         public synchronized boolean isResult() {
69:                 return result;
70:         }
71:
72:         public synchronized void setResult(boolean result) {
73:                 this.result = result;
74:         }
75:
76:         public synchronized PhantomReference<Object> getPhantomRef() {
77:                 return phantomRef;
78:         }
79:
80: }