public final class CloseGuard
extends java.lang.Object
A simple example:
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();
}
}
}
In usage where the resource to be explicitly cleaned up are
allocated after object construction, CloseGuard protection can
be deferred. For example:
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();
}
}
}
When used in a constructor calls to 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 open should occur just after
resource acquisition.
Note that the null check on guard in the finalizer is to
cover cases where a constructor throws an exception causing the
guard to be uninitialized.
| Modifier and Type | Class and Description |
|---|---|
static interface |
CloseGuard.Reporter
Interface to allow customization of reporting behavior.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Marks this CloseGuard instance as closed to avoid warnings on
finalization.
|
static CloseGuard |
get()
Returns a CloseGuard instance.
|
static CloseGuard.Reporter |
getReporter()
Returns non-null CloseGuard.Reporter.
|
void |
open(java.lang.String closer)
If CloseGuard is enabled,
open initializes the instance
with a warning that the caller should have explicitly called the
closer method instead of relying on finalization. |
static void |
setEnabled(boolean enabled)
Used to enable or disable CloseGuard.
|
static void |
setReporter(CloseGuard.Reporter reporter)
Used to replace default Reporter used to warn of CloseGuard
violations.
|
void |
warnIfOpen()
If CloseGuard is enabled, logs a warning if the caller did not
properly cleanup by calling an explicit close method
before finalization.
|
public static CloseGuard get()
#open(String) can be used to set up the instance to warn on
failure to close. If CloseGuard is disabled, a non-null no-op
instance is returned.public static void setEnabled(boolean enabled)
public static void setReporter(CloseGuard.Reporter reporter)
public static CloseGuard.Reporter getReporter()
public void open(java.lang.String closer)
open initializes the instance
with a warning that the caller should have explicitly called the
closer method instead of relying on finalization.closer - non-null name of explicit termination methodjava.lang.NullPointerException - if closer is null, regardless of
whether or not CloseGuard is enabledpublic void close()
public void warnIfOpen()