After failing to wrap a XMLStreamReader in a try-with-resources after discovering it's close() method, I thought about checking what other classes have a public void close() method in the JDK but don't implement AutoCloseable.

So I wrote a small program that enumerates all classes present in in the jrt image with a public void close() method that does not
directly or indirectly implement AutoCloseable:
https://gist.github.com/DasBrain/8d50e02e35654870e2c2c00bf3f79954 (Output & Code, Code requires JDK 14 with preview features and ASM)

As I am not an expert in any of those areas where classes showed up, and I don't know if it was an oversight, or if there is a good reason for not implementing AutoCloseable. Implementing AutoCloseable is a double edged sword, a few IDEs warn about resource leaks based on that interface. And treat an AutoCloseable passed to methods who return an AutoCloseable as the return value wraping the parameter.

I did look at a few classes:
* javax.xml.stream.XMLStreamReader:Javadoc: "Frees any resources associated with this Reader. This method does not close the underlying input source." Better not implement it? * javax.naming.Context: Javadoc: "This method releases this context's resources immediately, instead of waiting for them to be released automatically by the garbage collector." Implement AutoCloseable? * java.util.logging.Handler: Lifecycle seems to be managed by a LogManager. Better not implement AutoCloseable? * jdk.internal.jrtfs.ExplodedImage: class is package private, overrides close() from SystemImage while increasing visibility to public.

I did not restrict my enumeration to public and exported types - it was easier not to do that and I'm lazy.

This touches many unrelated areas, but David Holmes suggested that I take it to core-libs-dev. I know, I just did some static code analysis, but it seems that there might be some low hanging fruits. And for public classes I wish to have at least some rationale why a class with a public void close() method doesn't implement AutoCloseable.

Thanks,
Johannes

Classes found:
com/sun/jdi/connect/spi/Connection
com/sun/jndi/dns/BaseNameClassPairEnumeration
com/sun/jndi/dns/DnsClient
com/sun/jndi/dns/DnsContext
com/sun/jndi/dns/Resolver
com/sun/jndi/ldap/AbstractLdapNamingEnumeration
com/sun/jndi/ldap/LdapCtx
com/sun/jndi/ldap/LdapReferralContext
com/sun/jndi/ldap/LdapSchemaCtx
com/sun/jndi/ldap/ext/StartTlsResponseImpl
com/sun/jndi/rmi/registry/BindingEnumeration
com/sun/jndi/rmi/registry/NameClassPairEnumeration
com/sun/jndi/rmi/registry/RegistryContext
com/sun/jndi/toolkit/dir/ContextEnumerator
com/sun/jndi/toolkit/dir/HierMemDirCtx
com/sun/jndi/toolkit/dir/HierMemDirCtx$BaseFlatNames
com/sun/jndi/toolkit/dir/LazySearchEnumerationImpl
com/sun/jndi/toolkit/url/GenericURLContext
com/sun/media/sound/AudioFloatFormatConverter$AudioFloatInputStreamChannelMixer com/sun/media/sound/AudioFloatFormatConverter$AudioFloatInputStreamResampler
com/sun/media/sound/AudioFloatInputStream
com/sun/media/sound/AudioFloatInputStream$BytaArrayAudioFloatInputStream
com/sun/media/sound/AudioFloatInputStream$DirectAudioFloatInputStream
com/sun/media/sound/ModelAbstractOscillator
com/sun/media/sound/ModelDirector
com/sun/media/sound/ModelOscillatorStream
com/sun/media/sound/ModelStandardDirector
com/sun/media/sound/ModelStandardIndexedDirector
com/sun/media/sound/RIFFWriter$RandomAccessByteWriter
com/sun/media/sound/RIFFWriter$RandomAccessFileWriter
com/sun/media/sound/RIFFWriter$RandomAccessWriter
com/sun/media/sound/SoftAbstractResampler$ModelAbstractResamplerStream
com/sun/media/sound/SoftMainMixer
com/sun/media/sound/SoftMixingDataLine$AudioFloatInputStreamResampler
com/sun/media/sound/SoftMixingMainMixer
com/sun/media/sound/SoftMixingSourceDataLine$NonBlockingFloatInputStream
com/sun/naming/internal/VersionHelper$InputStreamEnumeration
com/sun/org/apache/xerces/internal/impl/XMLStreamFilterImpl
com/sun/org/apache/xerces/internal/impl/XMLStreamReaderImpl
com/sun/org/apache/xerces/internal/xinclude/XIncludeTextReader
com/sun/org/apache/xml/internal/serializer/EmptySerializer
com/sun/org/apache/xml/internal/serializer/SerializationHandler
com/sun/org/apache/xml/internal/serializer/SerializerBase
com/sun/org/apache/xml/internal/serializer/ToHTMLSAXHandler
com/sun/org/apache/xml/internal/serializer/ToUnknownStream
com/sun/org/apache/xml/internal/serializer/WriterChain
com/sun/tools/javac/api/JavacTaskPool$ReusableContext$ReusableJavaCompiler
com/sun/tools/javac/file/JavacFileManager$1
com/sun/tools/javac/file/JavacFileManager$ArchiveContainer
com/sun/tools/javac/file/JavacFileManager$Container
com/sun/tools/javac/file/JavacFileManager$DirectoryContainer
com/sun/tools/javac/file/JavacFileManager$JRTImageContainer
com/sun/tools/javac/file/Locations
com/sun/tools/javac/main/JavaCompiler
com/sun/tools/javac/processing/JavacProcessingEnvironment$DiscoveredProcessors
com/sun/tools/javac/processing/JavacProcessingEnvironment$ServiceIterator
com/sun/tools/jdi/SharedMemoryConnection
com/sun/tools/jdi/SocketConnection
com/sun/tools/sjavac/PubApiExtractor
com/sun/xml/internal/stream/Entity$ScannedEntity
com/sun/xml/internal/stream/XMLEventReaderImpl
com/sun/xml/internal/stream/writers/XMLDOMWriterImpl
com/sun/xml/internal/stream/writers/XMLEventWriterImpl
com/sun/xml/internal/stream/writers/XMLStreamWriterImpl
java/awt/SplashScreen
java/util/logging/ConsoleHandler
java/util/logging/FileHandler
java/util/logging/Handler
java/util/logging/MemoryHandler
java/util/logging/SocketHandler
java/util/logging/StreamHandler
javax/naming/Context
javax/naming/InitialContext
javax/naming/NamingEnumeration
javax/naming/directory/BasicAttribute$ValuesEnumImpl
javax/naming/directory/BasicAttributes$AttrEnumImpl
javax/naming/directory/BasicAttributes$IDEnumImpl
javax/naming/ldap/StartTlsResponse
javax/naming/spi/ContinuationContext
javax/smartcardio/CardChannel
javax/sql/PooledConnection
javax/swing/ProgressMonitor
javax/swing/text/rtf/RTFReader$AttributeTrackingDestination
javax/swing/text/rtf/RTFReader$ColortblDestination
javax/swing/text/rtf/RTFReader$Destination
javax/swing/text/rtf/RTFReader$DiscardingDestination
javax/swing/text/rtf/RTFReader$FonttblDestination
javax/swing/text/rtf/RTFReader$StylesheetDestination
javax/swing/text/rtf/RTFReader$StylesheetDestination$StyleDefiningDestination
javax/swing/text/rtf/RTFReader$TextHandlingDestination
javax/xml/stream/XMLEventReader
javax/xml/stream/XMLEventWriter
javax/xml/stream/XMLStreamReader
javax/xml/stream/XMLStreamWriter
javax/xml/stream/util/EventReaderDelegate
javax/xml/stream/util/StreamReaderDelegate
jdk/internal/jrtfs/ExplodedImage
jdk/internal/util/xml/XMLStreamWriter
jdk/internal/util/xml/impl/XMLStreamWriterImpl
jdk/jfr/internal/consumer/ChunkParser
jdk/jshell/SourceCodeAnalysisImpl
jdk/tools/jlink/internal/Archive
jdk/tools/jlink/internal/DirArchive
jdk/tools/jlink/internal/JarArchive
jdk/tools/jlink/internal/JmodArchive
org/graalvm/compiler/code/CompilationResult
org/graalvm/compiler/code/DataSection
org/graalvm/compiler/debug/DiagnosticsOutputDirectory
org/w3c/dom/html/HTMLDocument
sun/awt/image/ImageDecoder
sun/jvm/hotspot/debugger/DataSource
sun/jvm/hotspot/debugger/InputLexer
sun/jvm/hotspot/debugger/MappedByteBufferDataSource
sun/jvm/hotspot/debugger/RandomAccessFileDataSource
sun/jvm/hotspot/debugger/posix/AddressDataSource
sun/jvm/hotspot/debugger/posix/elf/ELFFile
sun/jvm/hotspot/debugger/posix/elf/ELFFileParser$ELFFileImpl
sun/jvm/hotspot/debugger/win32/coff/COFFFile
sun/jvm/hotspot/debugger/win32/coff/COFFFileParser$COFFFileImpl
sun/jvm/hotspot/debugger/windbg/AddressDataSource
sun/jvm/hotspot/debugger/windbg/DLL
sun/net/ProgressSource
sun/net/httpserver/ExchangeImpl
sun/net/www/URLConnection
sun/rmi/log/ReliableLog
sun/rmi/runtime/Log$InternalStreamHandler
sun/rmi/transport/Connection
sun/rmi/transport/tcp/TCPConnection
sun/security/smartcardio/ChannelImpl
sun/tools/java/ClassPath

Reply via email to