@Cleanup

Overview

You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a try/finally construct. Look at the example below to see how this works.

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via @Cleanup.

With Lombok

01 import lombok.Cleanup;
02 import java.io.*;
03 
04 public class CleanupExample {
05   public static void main(String[] argsthrows IOException {
06     @Cleanup InputStream in = new FileInputStream(args[0]);
07     @Cleanup OutputStream out = new FileOutputStream(args[1]);
08     byte[] b = new byte[10000];
09     while (true) {
10       int r = in.read(b);
11       if (r == -1break;
12       out.write(b, 0, r);
13     }
14   }
15 }

Vanilla Java

01 import java.io.*;
02 
03 public class CleanupExample {
04   public static void main(String[] argsthrows IOException {
05     InputStream in = new FileInputStream(args[0]);
06     try {
07       OutputStream out = new FileOutputStream(args[1]);
08       try {
09         byte[] b = new byte[10000];
10         while (true) {
11           int r = in.read(b);
12           if (r == -1break;
13           out.write(b, 0, r);
14         }
15       finally {
16         if (out != null) {
17           out.close();
18         }
19       }
20     finally {
21       if (in != null) {
22         in.close();
23       }
24     }
25   }
26 }

Small print

In the finally block, the cleanup method is only called if the given resource is not null. However, if you use delombok on the code, a call to lombok.Lombok.preventNullAnalysis(Object o) is inserted to prevent warnings if static code analysis could determine that a null-check would not be needed. Compilation with lombok.jar on the classpath removes that method call, so there is no runtime dependency.

If your code throws an exception, and the cleanup method call that is then triggered also throws an exception, then the original exception is hidden by the exception thrown by the cleanup call. You should not rely on this 'feature'. Preferably, lombok would like to generate code so that, if the main body has thrown an exception, any exception thrown by the close call is silently swallowed (but if the main body exited in any other way, exceptions by the close call will not be swallowed). The authors of lombok do not currently know of a feasible way to implement this scheme, but if java updates allow it, or we find a way, we'll fix it.

You do still need to handle any exception that the cleanup method can generate!