CloseableProvides
Providing Closeable resources makes their lifecycle unclear

Severity
WARNING

The problem

If you provide Closeable resources through dependency injection, it can be difficult to effectively manage the lifecycle of the Closable:

class MyModule extends AbstractModule() {
  @Provides
  FileOutputStream provideFileStream() {
    return new FileOutputStream("/tmp/outfile");
  }
}

...

class Client {
  private final FileOutputStream fos;
  @Inject Client(FileOutputStream fos, OtherDependency other, ...) {
     this.fos = fos;
  }
  void doSomething() throws IOException {
    fos.write("hello!");
  }
}

There are a number of issues with this approach as it relates to resource management:

The preferred solution is to not inject closable resources, but instead, objects that can expose short-lived closable resources that are used as necessary. The following example uses Guava’s CharSink as the resource manager object:

class MyModule extends AbstractModule() {
  @Provides
  CharSink provideCharSink() {
    return Files.asCharSink(new File("/tmp/outfile"), StandardCharsets.UTF_8);
  }
}

...

class Client {
  private final CharSink sink;
  @Inject Client(CharSink sink, OtherDependency other, ...) {
     this.sink = sink;
  }
  void doSomething() throws IOException {
    sink.write("hello!"); // Opens the file at this point, and closes once its done.
  }
}

If there’s not a similar non-closable resource, you can write a simple wrapper:

class ResourceManager {
  @Inject ResourceManager(@Config String configs, ...) {}

  /**
   * Returns a new thing for you to use and dispose of
   */
  OutputStream provideInstance() { return new...(); }
}

...
class Client {
  private final ResourceManager resource;
  @Inject Client(ResourceManager resource, OtherDependency other, ...) {
     this.resource = resource;
  }
  void doSomething() {
    try (OutputStream actualStream = resource.provideInstance()) {
      // write to actualStream, closing with try-with-resources
    }
  }
}

This pattern can be extended to other resources: as opposed to injecting database connection handles directly, inject connection pool objects that require your object to ask for those connection objects when they’re needed and close them safely.

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("CloseableProvides") to the enclosing element.