Some Classes with a public void close() don't implement AutoCloseable

Johannes Kuhn info at j-kuhn.de
Wed Apr 15 13:35:38 UTC 2020


Hi,

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.

For this reason I wrote a small program that enumerates all classes 
present in in my 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, I don't know if that was 
just an oversight or intended.

The XMLStreamReaderImpl will just set a flag to indicate that this 
instance can be reused, with a comment //xxx: Check what this function 
is intended to do.
The most likely candidate for adding AutoCloseable is 
javax.naming.Context in my opinion.
Not sure about java.util.logging.Handler. As far as I know those are 
closed by the Logger and closed when reloading the configuration for 
example.

Anyway, thought I share this. We could maybe add a few implements 
AutoCloseable or add some comments why they don't implement that.
Some IDEs rely on resource leak warnings on the interface, so adding the 
interface can be double edged sword.

With best regards,
Johannes Kuhn

Output:
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



More information about the discuss mailing list