Top Description Fields Constructors Methods
org.h2.util

public Class CloseWatcher

extends PhantomReference<Object>
Class Inheritance
Imports
java.io.PrintWriter, .StringWriter, java.lang.ref.PhantomReference, .ReferenceQueue, java.util.Collections, .HashSet, .Set

A phantom reference to watch for unclosed objects.

Field Summary

Modifier and TypeField and Description
private AutoCloseable
closeable

The closeable object.

private String
openStackTrace

The stack trace of when the object was created.

private static final ReferenceQueue<Object>
queue

Hides java.lang.ref.Reference.queue.

The queue (might be set to null at any time).
private static final Set<CloseWatcher>
refs

The reference set.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public AutoCloseable
public String

Returns:

the open stack trace
getOpenStackTrace
()

Get the open stack trace or null if none.

public static CloseWatcher

Returns:

the first watcher
pollUnclosed
()

Check for an collected object.

public static CloseWatcher

Returns:

the close watcher
register
(Object
the object
o
,
AutoCloseable
the object to close
closeable
,
boolean
whether the stack trace should be registered (this is relatively slow)
stackTrace
)

Register an object.

public static void
unregister(CloseWatcher
the reference
w
)

Unregister an object, so it is no longer tracked.

Inherited from java.lang.ref.PhantomReference:
get

Field Detail

closeableback to summary
private AutoCloseable closeable

The closeable object.

openStackTraceback to summary
private 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).

queueback to summary
private static final ReferenceQueue<Object> queue

Hides java.lang.ref.Reference.queue.

The queue (might be set to null at any time).

refsback to summary
private static final Set<CloseWatcher> refs

The reference set. Must keep it, otherwise the references are garbage collected first and thus never enqueued.

Constructor Detail

CloseWatcherback to summary
public CloseWatcher(Object referent, ReferenceQueue<Object> q, AutoCloseable closeable)

Method Detail

getCloseableback to summary
public AutoCloseable getCloseable()
getOpenStackTraceback to summary
public String getOpenStackTrace()

Get the open stack trace or null if none.

Returns:String

the open stack trace

pollUnclosedback to summary
public static CloseWatcher pollUnclosed()

Check for an collected object.

Returns:CloseWatcher

the first watcher

registerback to summary
public static CloseWatcher register(Object o, AutoCloseable closeable, boolean stackTrace)

Register an object. Before calling this method, pollUnclosed() should be called in a loop to remove old references.

Parameters
o:Object

the object

closeable:AutoCloseable

the object to close

stackTrace:boolean

whether the stack trace should be registered (this is relatively slow)

Returns:CloseWatcher

the close watcher

unregisterback to summary
public static void unregister(CloseWatcher w)

Unregister an object, so it is no longer tracked.

Parameters
w:CloseWatcher

the reference