How to Use the @Cleanup Annotation of Project Lombok

This article explains how to make use of the @Cleanup annotation of Project Lombok.
This post will focus on how to make use of the @Cleanup
annotation of Project Lombok. The Lombok Project is a java library that helps a developer generate boilerplate code. By simply adding the Lombok library to your IDE and build path, the Lombok library will auto-generate the Java bytecode, as per the annotations, into the class files.
The @Cleanup
annotation is added to any local variable declaration of a resource that needs to be closed after the successful execution of the resource. Project Lombok will generate the try / finally
clauses and invoking the close()
method.
Article Series
This article forms part of a multi-part series on how to use the Project Lombok to generate boilerplate code in java projects.
- How to Use the @AllArgsConstructor Annotation of Project Lombok
- How to Use the @Builder Annotation of Project Lombok
- How to Use the @CleanUp Annotation of Project Lombok
- How to Use the @Data Annotation of Project Lombok
- How to Use the @EqualsAndHashCode Annotation of Project Lombok
- How to Use the @Getter and @Setter Annotations of Project Lombok
Define Maven Dependencies
The following dependencies should be included in the pom.xml
file.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
Example: @Cleanup
The @Cleanup
annotation ensures that resources are cleaned up by adding the try .. finally
blocks and invoking the close()
method on the resource. This example illustrates how to add the @Cleanup
annotation to a local variable and as a result, the close method will be invoked within a try/finally
construct. Look at the sample code below to see how this works.
Origial Class
public class CleanupExample01 {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream("inputFile.txt");
@Cleanup OutputStream out = new FileOutputStream("outputFile.txt");
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}
To truly appreciate the magic of the Lombok library, you should compile the CleanupExample01
class by making use of maven to build your project. As part of the compile process, Lombok will generate the boilerplate code depending on the type of annotation you used. To see the boilerplate code, you should decompile CleanupExample01.class
file. An easy way to do this is to make use of the following URL:
Java Decompiler: http://www.javadecompilers.com
Decompiled Class
public static void main(final String[] args) throws IOException {
final InputStream in = new FileInputStream("inputFile.txt");
try {
final OutputStream out = new FileOutputStream("outputFile.txt");
try {
final byte[] b = new byte[10000];
while (true) {
final int r = in.read(b);
if (r == -1) {
break;
}
out.write(b, 0, r);
}
}
finally {
if (Collections.singletonList(out).get(0) != null) {
out.close();
}
}
}
finally {
if (Collections.singletonList(in).get(0) != null) {
in.close();
}
}
}
Summary
Congratulations !!! You have successfully used the @Cleanup
annotation of Project Lombok. Please look out for more examples on how to make use of Project Lombok to simplify your Java coding experience. Follow me on any of the different social media platforms and feel free to leave comments.