Annotations, Part I: Using Project Lombok for Custom Annotations
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
To demonstrate how custom annotations work inside the NetBeans IDE, we will use Project Lombok, which provides a convenient way of automatically generating several Java code elements, such as getters, setters, constructors and others. For more information about its features, visit the Project Lombok's website. However, keep in mind that Project Lombok includes some features that might not work in all development environments.
Requirements
To complete this tutorial, you need the following software and resources.
Software or Resource | Version Required |
---|---|
9.0 or later |
|
version 7 or 8 |
|
v1.12.4 or newer |
Creating a New Java project
In this exercise you create a simple Java project and class that is named MyBooks.java
which will demonstrate annotations in action.
-
Choose File > New Project from the main menu to open the New Project wizard.
-
Select the Java Application project type in the Java category. Click Next.
-
In the Name and Location page of the wizard, type
TestAnn
as the project name. -
Type
testann.TestBooks
in the Create Main Class field to replace the default class name. Click Finish.

When you click Finish, the IDE creates the Java application project and opens the TestBooks.java
class in the editor. You can see that the new project is now visible in the Projects window and that the TestBooks.java
class is in the testann
package under the Source Packages node.
-
Right-click the
testann
package node in the Projects window and choose New > Java class. -
Type
MyBooks
for the Class Name and confirm that the class will be created in thetestann
package. Click Finish.
When you click Finish the IDE opens the new class in the editor.
-
In the source editor, add the following three fields to
MyBooks.java
.
package testann;
public class MyBooks {
*private int year; //fields
private String title;
private String author;*
}
-
Place your insert cursor in the class declaration and press Ctrl-Space to invoke the editor’s code completion support.
-
Select
MyBooks (int year, String title, String author) - generate
in the code completion list to generate a constructor forMyBooks.java
.

-
Save your changes.
Enabling Custom Annotations (lombok.jar) for the Project
In this exercise you will modify the project’s properties window to add a library to the project’s classpath and enable annotation processing in the editor.
-
Download the lombok.jar file and save it on your system.
-
Right-click the
TestAnn
project’s node and choose Properties. -
Select the Libraries category in the Project Properties dialog.
-
Click Add JAR/Folder in the Compile tab and locate the
lombok.jar
file that you downloaded.

The resources added on the Compile tab correspond to the -classpath
option of the Java compiler. As lombok.jar
is a single JAR file that contains both annotation definitions and annotation processors, you should add it to the project’s classpath, which is the Compile tab.
-
Choose the Compiling category in the Project Properties window.
-
Confirm that the Enable Annotation Processing checkbox is selected (it is enabled by default) and select the Enable Annotation Processing in Editor checkbox.

The Enable Annotation Processing checkbox enables annotation processing while building and compiling your project. If the checkbox is not selected, the -proc:none
option is passed to the Java compiler, and compilation takes places without any annotation processing. So, if you want to process annotations in your code, the Enable Annotation Processing checkbox must be selected.
By selecting the Enable Annotation Processing in Editor checkbox, you make annotation processing results visible in the editor. Any additional artifacts that are generated by annotation processors (classes, methods, fields, etc.) become visible in the IDE Editor and available in code completion, Navigator, GoTo Type, Find usages, and others.
-
Click OK in the Project Properties window and return to the
MyBooks.java
file.
If you expand the Libraries node in the Projects window, you can see that lombok.jar
is now listed as a project library.

Writing an Application Using Lombok Custom Annotations
-
In
MyBooks.java
file, type@Data
before theMyBooks
class declaration.@Data
is an annotation that generates the boilerplate code for Java classes: getters for all fields, setters for all non-final fields, and appropriatetoString
,equals
, andhashCode
implementations that involve the fields of the class.
To learn more about what annotations are supported by Project Lombok, refer to the Lombok Features Overview.
-
Click the hint in the editor’s left margin and add import for
lombok.Data
.

The resulting code in the Editor should look like the example below.
package testann;
import lombok.Data;
@Data
public class MyBooks {
private int year; //fields
private String title;
private String author;
public MyBooks(int year, String title, String author) {
this.year = year;
this.title = title;
this.author = author;
}
}
Note that necessary code artifacts, such as getters, setters, toString, etc, have been generated and you can see them in the Navigator window. The @Data
annotation generated all the boilerplate code that is needed for a typical class.

You can also invoke the code completion window (Ctrl-Space) and see that the generated artifacts are available for picking them. Now, let’s see that the project compiles and the generated artifacts can be called from other parts of the program.
-
Open the
TestBooks.java
file with the main method and add the following code (in bold) to create a new object of theMyBooks
class.
package testann;
public class TestBooks {
public static void main(String[] args) {
*MyBooks books = new MyBooks(2009, "My Beautiful Dream", "John Smith");*
}
}
-
Add the following code to print out the values of the
books
variable.
To return the values, we call the getter methods that were auto-generated by lombok.jar
. While you are typing, note that the auto-generated artifacts are available from the code completion window.
package testann;
public class TestBooks {
public static void main(String[] args) {
MyBooks books = new MyBooks(2009, "My Beautiful Dream", "John Smith");
*System.out.println("Year: " + books.getYear() + ", Title: " + books.getTitle() + ", Author: " + books.getAuthor());*
}
}
-
Save your changes.
-
Right-click the project node in the Projects window and choose Run (F6).
When you run the application you should see the following output that shows that the application compiled successfully.

You can see that the artifacts generated by the Lombok annotation processor are accessible from other parts of the program.
Next Step
-
Java SE Documentation - Annotations
-
Java SE Tutorial - Annotations