Annotation Processors Support in The NetBeans IDE
Annotation Processors Support in The NetBeans IDE
Project Lombok
Contents
Introduction
See Also
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 other. 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.
To complete this tutorial, you need the following software and resources.
Note: Support for custom annotation processors was added in the NetBeans IDE 6.9 release. For earlier IDE
2. In the Name and Location page of the New Project wizard, type TestAnn as the project name.
In the Create Main Class field, replace the default value with testann.TestBooks.
3. In the TestAnn project, create a new class MyBooks which we will use to show annotations in
action. Right-click the TestAnn project's node and choose New > Java class. Name the new
class MyBooks.
5. package testann;
6.
7.
8. public class MyBooks {
9. private int year; //fields
10. private String title;
11. private String author;
12.
13.
14. }
15. Add a constructor to MyBooks.java. Press Ctrl-Space to invoke the editor's code completion
support, and choose MyBooks (int year, String title, String author) -
2. Right-click the TestAnn project's node and choose Properties. In the Project Properties window,
select the Libraries > Compile tab, click Add JAR/Folder, and navigate to the lombok.jar file
compiler. As lombok.jar is a single JAR file that contains both annotation definitions and
annotation processors, we should add it to the project's classpath, which is the Compile tab.
4. On the Compiling tab, ensure that the Enable Annotation Processing checkbox is selected (it is
enabled by default). Also, select the Enable Annotation Processing in Editor checkbox.
The Enable Annotation Processing checkbox enables annotation processing while building and
compiling your project. If this 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 second checkbox, Enable Annotation Processing in Editor, 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
@Data is an annotation that generates the boilerplate code for Java classes: getters for all fields,
setters for all non-final fields, and appropriate toString, equals, and hashCode
To learn more about what annotations are supported by Project Lombok, refer to the Lombok's
Features Overview.
2. Click the hint on the editor's lefthand margin and add import for lombok.Data.
The resulting code in the Editor should look like the example below:
3. package testann;
4.
5. import lombok.Data;
6.
7.
8. @Data
9. public class MyBooks {
10.
11. private int year; //fields
12. private String title;
13. private String author;
14.
15. public MyBooks(int year, String title, String author) {
16. this.year = year;
17. this.title = title;
18. this.author = author;
19. }
20.}
21.
22.
Note that necessary code artifacts, such as getters, setters, toString, etc, have been generated
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
23. Now, let's see that the project compiles and the generated artifacts can be called from other
Open the Testbook.java file with the main method and create a new object of the MyBooks
class.
24.package testann;
25.
26.public class TestBooks {
27.
28. public static void main(String[] args) {
29.
30. MyBooks books = new MyBooks(2009, "My Beautiful Dream", "John
Smith");
31. }
32.}
33. Add a string that prints 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
34.package testann;
35.public class TestBooks {
36. public static void main(String[] args) {
37. MyBooks books = new MyBooks(2009, "My Beautiful Dream", "John
Smith");
38. System.out.println("Year: " + books.getYear() + ", Title: " +
books.getTitle() + ", Author: " + books.getAuthor());
39. }
40.}
41. Save the project, right-click it and choose Run (F6). You should see the following output that
This means that the artifacts generated by the Lombok annotation processor are accessible from