Package org.h2.util
Class CloseWatcher
- java.lang.Object
-
- java.lang.ref.Reference<T>
-
- java.lang.ref.PhantomReference<java.lang.Object>
-
- org.h2.util.CloseWatcher
-
public class CloseWatcher extends java.lang.ref.PhantomReference<java.lang.Object>A phantom reference to watch for unclosed objects.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.AutoCloseablecloseableThe closeable object.private java.lang.StringopenStackTraceThe stack trace of when the object was created.private static java.lang.ref.ReferenceQueue<java.lang.Object>queueThe queue (might be set to null at any time).private static java.util.Set<CloseWatcher>refsThe reference set.
-
Constructor Summary
Constructors Constructor Description CloseWatcher(java.lang.Object referent, java.lang.ref.ReferenceQueue<java.lang.Object> q, java.lang.AutoCloseable closeable)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.AutoCloseablegetCloseable()java.lang.StringgetOpenStackTrace()Get the open stack trace or null if none.static CloseWatcherpollUnclosed()Check for an collected object.static CloseWatcherregister(java.lang.Object o, java.lang.AutoCloseable closeable, boolean stackTrace)Register an object.static voidunregister(CloseWatcher w)Unregister an object, so it is no longer tracked.
-
-
-
Field Detail
-
queue
private static final java.lang.ref.ReferenceQueue<java.lang.Object> queue
The queue (might be set to null at any time).
-
refs
private static final java.util.Set<CloseWatcher> refs
The reference set. Must keep it, otherwise the references are garbage collected first and thus never enqueued.
-
openStackTrace
private java.lang.String openStackTrace
The stack trace of when the object was created. It is converted to a string early on to avoid classloader problems (a classloader can't be garbage collected if there is a static reference to one of its classes).
-
closeable
private java.lang.AutoCloseable closeable
The closeable object.
-
-
Method Detail
-
pollUnclosed
public static CloseWatcher pollUnclosed()
Check for an collected object.- Returns:
- the first watcher
-
register
public static CloseWatcher register(java.lang.Object o, java.lang.AutoCloseable closeable, boolean stackTrace)
Register an object. Before calling this method, pollUnclosed() should be called in a loop to remove old references.- Parameters:
o- the objectcloseable- the object to closestackTrace- whether the stack trace should be registered (this is relatively slow)- Returns:
- the close watcher
-
unregister
public static void unregister(CloseWatcher w)
Unregister an object, so it is no longer tracked.- Parameters:
w- the reference
-
getOpenStackTrace
public java.lang.String getOpenStackTrace()
Get the open stack trace or null if none.- Returns:
- the open stack trace
-
getCloseable
public java.lang.AutoCloseable getCloseable()
-
-