Closeable that collects Closeable resources and closes them all when it is
closed. This is intended to approximately emulate the behavior of Java 7's try-with-resources statement in JDK6-compatible code. Running on Java 7, code using this
should be approximately equivalent in behavior to the same code written with try-with-resources.
Running on Java 6, exceptions that cannot be thrown must be logged rather than being added to the
thrown exception as a suppressed exception.
This class is intended to be used in the following pattern:
Closer closer = Closer.create();
try {
InputStream in = closer.register(openInputStream());
OutputStream out = closer.register(openOutputStream());
// do stuff
} catch (Throwable e) {
// ensure that any checked exception types other than IOException that could be thrown are
// provided here, e.g. throw closer.rethrow(e, CheckedException.class);
throw closer.rethrow(e);
} finally {
closer.close();
}
Note that this try-catch-finally block is not equivalent to a try-catch-finally block using
try-with-resources. To get the equivalent of that, you must wrap the above code in another
try block in order to catch any exception that may be thrown (including from the call to close()).
This pattern ensures the following:
Closeable resource that is successfully registered will be closed later.
Throwable is thrown in the try block, no exceptions that occur when attempting
to close resources will be thrown from the finally block. The throwable from the try block
will be thrown.
An exception that is suppressed is not thrown. The method of suppression used depends on the version of Java the code is running on:
Throwable.addSuppressed(Throwable).
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | Closer.
Suppresses exceptions by logging them. |
| pack-priv static class | Closer.
Suppresses exceptions by adding them to the exception that will be thrown using JDK7's addSuppressed(Throwable) mechanism. |
| pack-priv static interface | Closer.
Suppression strategy interface. |
| Modifier and Type | Field and Description |
|---|---|
| private final Deque | |
| private static final Closer. | SUPPRESSOR
The suppressor implementation to use for the current Java version. |
| pack-priv final Closer. | |
| private Throwable |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements java. Closeable instances that have been added to this Closer.
|
| public static Closer | |
| public <C extends @Nullable Closeable> C | |
| public RuntimeException | |
| public <X extends Exception> RuntimeException | |
| public <X1 extends Exception, X2 extends Exception> RuntimeException |
| stack | back to summary |
|---|---|
| private final Deque<Closeable> stack | |
| SUPPRESSOR | back to summary |
|---|---|
| private static final Closer. The suppressor implementation to use for the current Java version. | |
| suppressor | back to summary |
|---|---|
| pack-priv final Closer. | |
| thrown | back to summary |
|---|---|
| private Throwable thrown
| |
| Closer | back to summary |
|---|---|
pack-priv Closer(Closer.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Closes all
| |
| create | back to summary |
|---|---|
| public static Closer create() Creates a new | |
| register | back to summary |
|---|---|
| public <C extends @Nullable Closeable> C register(C closeable) Registers the given
| |
| rethrow | back to summary |
|---|---|
| public RuntimeException rethrow(Throwable e) throws IOException Stores the given throwable and rethrows it. It will be rethrown as is if it is an Note Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type. This method always throws, and as such should be called as
| |
| rethrow | back to summary |
|---|---|
| public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, Class-:X Stores the given throwable and rethrows it. It will be rethrown as is if it is an Note Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type. This method always throws, and as such should be called as
| |
| rethrow | back to summary |
|---|---|
| public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, Class-:X1, Class-:X2 Stores the given throwable and rethrows it. It will be rethrown as is if it is an Note Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type. This method always throws, and as such should be called as
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final Closer. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | suppress(Closeable closeable, Throwable thrown, Throwable suppressed)
Implements com. suppressed) which was thrown when attempting to close
the given closeable.
|
| INSTANCE | back to summary |
|---|---|
| pack-priv static final Closer. | |
| LoggingSuppressor | back to summary |
|---|---|
| pack-priv LoggingSuppressor() | |
| suppress | back to summary |
|---|---|
| public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) Implements com. Doc from com. Suppresses the given exception (
| |
| Modifier and Type | Field and Description |
|---|---|
| private final Method |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | suppress(Closeable closeable, Throwable thrown, Throwable suppressed)
Implements com. suppressed) which was thrown when attempting to close
the given closeable.
|
| pack-priv static Closer. |
| addSuppressed | back to summary |
|---|---|
| private final Method addSuppressed | |
| SuppressingSuppressor | back to summary |
|---|---|
| private SuppressingSuppressor(Method addSuppressed) | |
| suppress | back to summary |
|---|---|
| public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) Implements com. Doc from com. Suppresses the given exception (
| |
| tryCreate | back to summary |
|---|---|
pack-priv static Closer.
| |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| suppress | back to summary |
|---|---|
| public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) Suppresses the given exception ( | |