Project Coin: Bringing it to a Close(able)

As a follow-up to the initial API changes to support automatic resource management (ARM) I wrote an annotation processor, CloseableFinder, to programmatically look for types that were candidates to be retrofitted as Closeable or AutoCloseable.

The processor issues a note for a type that has a public no-args instance method returning void whose name is "close" where the type does not already implement/extend Closeable or AutoCloseable. Based on the exceptions a close method is declared to throw, the processor outputs whether the type is a candidate to be retrofitting to just AutoCloseable or to either of Closeable and AutoCloseable. Which of Closeable and AutoCloseable is more appropriate can depend on the semantics of the close method not captured in its signature. For example, Closeable.close is defined to be idempotent, repeated calls to close have no effect. If a close method is defined to not be idempotent, without changing the specification the type can only be correctly retrofitted to AutoCloseable.

To use the processor, first compile it and then configure your compiler or IDE to run the processor. The processor can be compiled under JDK 6. Once compiled, it can be run either under JDK 6 or under a JDK 7 build that has the AutoCloseable interface; the processor will configure itself appropriately based on the JDK version it is running under. For javac, the command line to run the processor can look like:

javac -proc:only \\
-processor CloseableFinder \\
-processorpath Path_to_processor \\
List_of_files

A thread on build-dev discusses how to run an annotation processor over the JDK sources; a larger than default heap size may be needed to process all the files in one command. When run over the JDK 7 sources, the processor finds many candidate types to be retrofitted. After consulting with the teams in question, an additional nine types were retrofitted to work with ARM, two in java.beans, two in java.io, one in java.util, and four in javax.sound; these additional retrofittings have been pushed into JDK 7 and will appear in subsequent builds.

Besides the potential updating of JDBC at some point in the future, other significant retrofitting of JDK classes in java.\* and javax.\* to AutoCloseable/Closeable should not be expected. Unofficial JDK APIs in other namespaces might be examined for retrofitting in the future. The compiler changes to support the ARM language feature remain in progress.

Comments:

It worries me that some classes may be AutoCloseable, but not Closeable. It's more intuitive to consider the AutoCloseable type as a subtype of Closeable - the naming of these types certainly induces that thinking! Perhaps we should make some sacrifice and avoid making an API AutoCloseable, if that API cannot be Closeable also. It's probably too far in the process to propose that AutoCloseable should extend Closeable, but perhaps this is best handled just as a best-practice. We can use the opportunity to educate developers - any non-idempotent close() is a broken close()...

Posted by Osvaldo Pinali Doederlein on July 06, 2010 at 10:19 PM PDT #

@Osvaldo,

The type playing the role of AutoCloseable must be a supertype of java.io.Closeable to make the exception declarations work out. Some types cannot extend/implement java.io.Closeable because it is declared to throw IOException.

Posted by Joe Darcy on July 07, 2010 at 12:49 AM PDT #

Well, I have been waiting for something that looks like a Closeable interface for the java.sql classes since 2004. I just hate that I must have serveral close methods that are identical except for the argument. The interface or duck typing would have been nice.

I reported an RFE for a closeable interface for the java.sql classes back in 2004.
http://bugs.sun.com/view_bug.do?bug_id=5105637

Posted by Kaj Bjurman on July 07, 2010 at 04:34 PM PDT #

Hi Joe,

Off topic, but any ideas why the Java team is not releasing a version of JDK bundled with an rt.jar binary that is compiled with debugging information on (i.e. -g)?

Posted by Behrang Saeedzadeh on July 07, 2010 at 11:23 PM PDT #

@Kaj,

JDBC 4.1 in JDK 7 has been retrofitted to implement AutoCloseable:
http://blogs.sun.com/darcy/entry/project_coin_jdbc_4_1

Posted by Joe Darcy on September 14, 2010 at 06:41 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

darcy

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News

No bookmarks in folder

Blogroll