0% found this document useful (0 votes)
139 views7 pages

Annotation Processors Support in The NetBeans IDE

This document discusses using Project Lombok custom annotations in NetBeans IDE. It provides instructions on creating a new Java project, enabling Lombok annotations for the project, and writing an application that uses the Lombok @Data annotation to automatically generate boilerplate code like getters and setters. The instructions are followed by an example that demonstrates how to create a MyBooks class with @Data, generate the code, and call the generated getters from the main method.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views7 pages

Annotation Processors Support in The NetBeans IDE

This document discusses using Project Lombok custom annotations in NetBeans IDE. It provides instructions on creating a new Java project, enabling Lombok annotations for the project, and writing an application that uses the Lombok @Data annotation to automatically generate boilerplate code like getters and setters. The instructions are followed by an example that demonstrates how to create a MyBooks class with @Data, generate the code, and call the generated getters from the main method.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Annotation Processors Support in the NetBeans IDE, Part I: Using

Project Lombok

Written and maintained by Irina Filippova

Contents

 Introduction

 Map of javac Options and IDE Commands

 Using Project Lombok for Custom Annotations

o Creating a New Java Project

o Enabling Custom Annotations for the Project

o Writing an Application Using Lombok Custom Annotations

 Using Own Custom Annotation Processors in the IDE

 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.

Software or Resource Version Required

NetBeans IDE version 6.9

Java Development Kit (JDK) version 6

Note: Support for custom annotation processors was added in the NetBeans IDE 6.9 release. For earlier IDE

versions, this tutorial does not work.


Creating a New Java project
1. Choose File > New Project and select Java Application as a project type.

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.

4. In MyBooks.java, add the following three fields:

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) -

generate. A constructor will be added to your code.

Enabling Custom Annotations (lombok.jar) for the Project


1. Download the lombok.jar file and save it on your system.

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

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, we should add it to the project's classpath, which is the Compile tab.

3. In the Project Properties window, choose the Compiling 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

completion, Navigator, GoTo Type, Find usages, and other.


5. Click OK in the Project Properties window and return to the MyBooks.java file.

Writing an Application Using Lombok Custom Annotations


1. In MyBooks.java file, type @Data before the MyBooks 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 appropriate toString, equals, and hashCode

implementations that involve the fields of the class.

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

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.

23. Now, let's see that the project compiles and the generated artifacts can be called from other

parts of the program.

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

auto-generated artifacts are available from the code completion window.

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

shows that the application successfully compiles.

This means that the artifacts generated by the Lombok annotation processor are accessible from

other parts of the program.

You might also like