-
public final class CloseGuardCloseGuard is a mechanism for flagging implicit finalizer cleanup of resources that should have been cleaned up by explicit close methods (aka "explicit termination methods" in Effective Java).
A simple example:
In usage where the resource to be explicitly cleaned up are allocated after object construction, CloseGuard protection can be deferred. For example:{@code * class Foo { * * private final CloseGuard guard = CloseGuard.get(); * * ... * * public Foo() { * ...; * guard.open("cleanup"); * } * * public void cleanup() { * guard.close(); * ...; * } * * protected void finalize() throws Throwable { * try { * if (guard != null) { * guard.warnIfOpen(); * } * cleanup(); * } finally { * super.finalize(); * } * } * } * }
When used in a constructor calls to{@code * class Bar { * * private final CloseGuard guard = CloseGuard.get(); * * ... * * public Bar() { * ...; * } * * public void connect() { * ...; * guard.open("cleanup"); * } * * public void cleanup() { * guard.close(); * ...; * } * * protected void finalize() throws Throwable { * try { * if (guard != null) { * guard.warnIfOpen(); * } * cleanup(); * } finally { * super.finalize(); * } * } * } * }{@code open}should occur at the end of the constructor since an exception that would cause abrupt termination of the constructor will mean that the user will not have a reference to the object to cleanup explicitly. When used in a method, the call to{@code open}should occur just after resource acquisition.Note that the null check on
{@code guard}in the finalizer is to cover cases where a constructor throws an exception causing the{@code guard}to be uninitialized.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceCloseGuard.ReporterInterface to allow customization of reporting behavior.
-
Method Summary
Modifier and Type Method Description static CloseGuardget()Returns a CloseGuard instance. static voidsetEnabled(boolean enabled)Used to enable or disable CloseGuard. static voidsetReporter(CloseGuard.Reporter reporter)Used to replace default Reporter used to warn of CloseGuardviolations. static CloseGuard.ReportergetReporter()Returns non-null CloseGuard.Reporter. voidopen(String closer)If CloseGuard is enabled, {@code open}initializes the instancewith a warning that the caller should have explicitly called the{@code closer}method instead of relying on finalization.voidclose()Marks this CloseGuard instance as closed to avoid warnings onfinalization. voidwarnIfOpen()If CloseGuard is enabled, logs a warning if the caller did notproperly cleanup by calling an explicit close methodbefore finalization. -
-
Method Detail
-
get
static CloseGuard get()
Returns a CloseGuard instance. If CloseGuard is enabled,
{@code * #open(String)}can be used to set up the instance to warn onfailure to close. If CloseGuard is disabled, a non-null no-opinstance is returned.
-
setEnabled
static void setEnabled(boolean enabled)
Used to enable or disable CloseGuard. Note that CloseGuard onlywarns if it is enabled for both allocation and finalization.
-
setReporter
static void setReporter(CloseGuard.Reporter reporter)
Used to replace default Reporter used to warn of CloseGuardviolations. Must be non-null.
-
getReporter
static CloseGuard.Reporter getReporter()
Returns non-null CloseGuard.Reporter.
-
open
void open(String closer)
If CloseGuard is enabled,
{@code open}initializes the instancewith a warning that the caller should have explicitly called the{@code closer}method instead of relying on finalization.- Parameters:
closer- non-null name of explicit termination method
-
close
void close()
Marks this CloseGuard instance as closed to avoid warnings onfinalization.
-
warnIfOpen
void warnIfOpen()
If CloseGuard is enabled, logs a warning if the caller did notproperly cleanup by calling an explicit close methodbefore finalization. If CloseGuard is disabled, no action isperformed.
-
-
-
-