_Original issue created by aklitzing on 2012-05-31 at 10:18 AM_
I like to request that the static methods of com.google.common.io.Closeables will be extended to accept an AutoCloseable [1], too. I'm using ResultSet and Connection for JDBC a lot and I miss the simple Closeables.closeQuietly() in my finally block.
[1] http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
_Original comment posted by [email protected] on 2012-05-31 at 10:25 AM_
AutoClosable only came into existence in 1.7 and is outside of scope for Guava at the present moment.
However when Guava moves to 1.7 Closable extends AutoClosable so this should be a trivial change then.
_Original comment posted by aklitzing on 2012-05-31 at 10:25 AM_
Well, AutoCloseable exists since Java 1.7 ...... then it would be nice if ResultSet, Connection and Statement would be supported by com.google.common.io.Closeables.
_Original comment posted by wasserman.louis on 2012-05-31 at 10:27 AM_
The point is that Guava depends on only Java 6, so it can't use AutoCloseable.
_Original comment posted by wasserman.louis on 2012-05-31 at 10:28 AM_
I think that we're all in agreement that much of common.io is made obsolete by Java 7, but we're a ways away from identifying what gaps exist in Java 7, how we might go about filling them, and rewriting or redoing common.io to address those concerns.
_Original comment posted by [email protected] on 2012-05-31 at 10:45 AM_
I don't see anything wrong with adding support for ResultSet, Connection & Statement, although you risk adding .close() support to damn near every class in existance :P
In Java 7, all the aformentioned classes extend AutoClosable so it should be trivial to phase out those methods for 1.7.
+0.5 maybe?
_Original comment posted by wasserman.louis on 2012-05-31 at 10:53 AM_
Here is the complete list of classes that implement AutoCloseable in Java 7 that don't implement Closeable in Java 6 (just obtained by a simple diff):
AbstractSelector, AsynchronousFileChannel, AsynchronousServerSocketChannel, AsynchronousSocketChannel, DatagramSocket, FileCacheImageInputStream, FileCacheImageOutputStream, FileImageInputStream, FileImageOutputStream, FileLock, FileSystem, ImageInputStreamImpl, ImageOutputStreamImpl, JarFile, MemoryCacheImageInputStream, MemoryCacheImageOutputStream, MLet, MulticastSocket, PrivateMLet, Scanner, Selector, ServerSocket, Socket, SSLServerSocket, SSLSocket, URLClassLoader, XMLDecoder, XMLEncoder, ZipFile
I'm not sure how many of these are _new_ classes in Java 6, but...I think that most of them were in Java 6, but not implementing Closeable.
However...hmmmm. Observation: if you use Java 7 with Guava (even though Guava is built for JDK 6), almost all of the classes in the above list have been retrofitted to implement Closeable, which means that the Closeables utilities from Guava should work on them, no? So are you on Java 7? If so, what's the problem?
If you're _not_ on Java 7...I don't think we can feasibly add 29 additional overloads to every Guava method that expects a Closeable. =/ We have to draw a line somewhere, I think?
_Original comment posted by wasserman.louis on 2012-05-31 at 10:58 AM_
Okay, I see. AutoCloseable is a new superinterface of Closeable.
That said, a quick diff suggests that in JDK 7, there are only three types implementing AutoCloseable without implementing Closeable: FileLock, XMLDecoder, XMLEncoder. So as long as you know the specific type of the closeable thing, and you're not just passing it around, you can still use the Closeables utilities.
_Original comment posted by [email protected] on 2012-06-01 at 12:07 AM_
I don't quite understand why anyone would want to use Closeables at all in JDK 7 in the first place. Isn't the new try syntax a lot easier?
_Original comment posted by [email protected] on 2012-06-01 at 07:40 AM_
@锘縆evin I think in some instances you might have to hold the resource open for an unknown period of time and then Closables would come in handy for cleaning up the resources.
_Original comment posted by aklitzing on 2012-06-01 at 08:35 AM_
I don't want to use Closeables in JDK7 I saw to late that AutoCloseable is JDK7. I just want to use Closeables for JDBC classes like ResultSet, Statement and Connection.
_Original comment posted by [email protected] on 2012-06-19 at 05:46 PM_
Since all these things should just work in the JDK 1.7 future, I don't think it makes sense for us to add special one-off support for things like ResultSet now. I see nothing for Guava to do in this area at the moment.
Status: WontFix
Labels: Type-Enhancement, Package-IO
I just copy and paste Closer and find Closeable replace to AutoCloseable, and rename Closer to AutoCloser :smile:
Why is it such a problem to add this fix? That won't break any backwards compatibility and it is pretty straightforward
@alexeygrigorev I think it's because Guava would then require Java 7, and considering it currently only requires Java 6 (to support Android and legacy users), this would be a big no-no.
However, it's worth noting that the Guava team are currently in the process of upgrading Guava for Java 8 (they will keep a Java-6-compatible version around for Android), which would implicitly make it Java-7-compatible too, so hopefully we won't have to wait too long for com.google.common.io.Closeables to accept AutoCloseables.
Since the latest version requires Java 8 this should be reopened.
Also pretty please adjust Closer. Thanks!
Most helpful comment
Since the latest version requires Java 8 this should be reopened.