Simple Resource Clean-up
jjb at google.com
Tue Mar 3 10:03:40 PST 2009
> The specification requires that the object in the try () block have a
> "close()" method. Wether the method throws any or no exception, or if it
> returns a value or no value does not matter.
This is a "naming pattern" (of the sort used by serialization and beans).
If we need more flexibility than interfaces provide, I believe that we're
better off going all the way to annotations (see Item 35 in Effective Java,
Second Ed.). This would allow us to use the automatic resource management
statement with types whose "dispose" method wasn't named close (such as
java.awt.graphics.dispose). This is listed as a "design alternative" at the
bottom of my proposal.
Here's how I summarize the pros and cons: "[Use of an annotation] allows the
use of a different method names (such as destroy and terminate) and eases
the use of the new construct with existing resource types. But it is more
“magical” and does not mesh as well with Java’s type system." I do see this
as a viable alternative, but with pros and cons. I believe it would be
relatively straightforward to retrofit my proposal to use method annotations
instead of (or in addition to) an interface.
More information about the coin-dev