Inspectopedia Help

AutoCloseable used without 'try'-with-resources

Reports AutoCloseable instances which are not used in a try-with-resources statement, also known as Automatic Resource Management.

This means that the "open resource before/in try, close in finally" style that had been used before try-with-resources became available, is also reported. This inspection is meant to replace all opened but not safely closed inspections when developing in Java 7 and higher.

Example:

private static void foo() throws IOException { InputStream profile = Thread.currentThread().getContextClassLoader().getResourceAsStream("/someFile"); System.out.println(profile.read()); }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

AutoCloseableResource
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Resource management

Use the following options to configure the inspection:

  • List subclasses of AutoCloseable that do not need to be closed and should be ignored by this inspection.

    Note: The inspection will still report streams returned from the java.nio.file.Files methods lines(), walk(), list() and find(), even when java.util.stream.Stream is listed to be ignored. These streams contain an associated I/O resource that needs to be closed.

  • List methods returning AutoCloseable that should be ignored when called.

  • Whether to ignore an AutoCloseable if it is the result of a method call. When this option is enabled, the results of factory methods will also be ignored.

  • Whether the inspection should report if an AutoCloseable instance is passed as a method call argument. If this option is enabled, the inspection assumes the resource is closed in the called method. Method calls inside a finally block with 'close' in the name and an AutoCloseable argument will not be ignored.

  • Whether to ignore method references to constructors of resource classes.

  • Whether to ignore methods that return a resource and whose name starts with 'get'. This can reduce false positives because most of the getters do not transfer the ownership of the resource, and their call sites are not responsible for closing the resource.

This inspection depends on the Java feature 'Try-with-resources' which is available since Java 7.

Inspection options

Here you can find the description of settings available for the AutoCloseable used without 'try'-with-resources inspection, and the reference of their default values.

Ignored AutoCloseable resource types

[java.util.stream.Stream, java.util.stream.IntStream, java.util.stream.LongStream, java.util.stream.DoubleStream, java.io.ByteArrayOutputStream, java.io.ByteArrayInputStream, java.io.StringBufferInputStream, java.io.CharArrayWriter, java.io.CharArrayReader, java.io.StringWriter, java.io.StringReader, java.util.Formatter, java.util.Scanner, org.springframework.context.ConfigurableApplicationContext, io.micronaut.context.ApplicationContext]

Ignore AutoCloseable instances returned from these methods

None

Ignore AutoCloseable instances returned from all method calls

Not selected

Any method may close resource argument

Default: Selected

Ignore constructor method references which produce AutoCloseable

Default: Selected

Ignore getters that returns resource

Default: Selected

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024