Project Coin: Bringing it to a Close(able)
As a follow-up to the [URL="http://blogs.sun.com/darcy/entry/project_coin_arm_api"]initial API changes to support automatic resource management (ARM)[/URL] I wrote an annotation processor, [URL="http://blogs.sun.com/darcy/resource/ProjectCoin/CloseableFinder.java"]CloseableFinder[/URL], 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 [I]idempotent[/I], repeated calls to close have no effect. If a close method is defined to [I]not[/I] 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:
[INDENT] javac -proc:only \
-processor CloseableFinder \
-processorpath [I]Path_to_processor[/I] \
[I]List_of_files[/I] [/INDENT][URL="http://mail.openjdk.java.net/pipermail/build-dev/2010-April/002886.html"]A thread on build-dev[/URL] 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 [URL="http://blogs.sun.com/darcy/resource/ProjectCoin/Jdk7CloseableCandidates.txt"]finds many candidate types to be retrofitted[/URL]. 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 [URL="http://hg.openjdk.java.net/jdk7/tl/jdk/rev/425960cef714"]pushed into JDK 7[/URL] 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. [URL="http://cr.openjdk.java.net/~darcy/OpenJdkDevGuide/OpenJdkDevelopersGuide.v0.777.html#official_unofficial"]Unofficial JDK APIs[/URL] in other namespaces might be examined for retrofitting in the future. The compiler changes to support the ARM language feature remain in progress.
[url=http://blogs.sun.com/darcy/entry/project_coin_bring_close]Read More about [Project Coin: Bringing it to a Close(able)...[/url]