Sample contributed by Jesse Glick.
This two-part tutorial demonstrates how you can attach annotation processors to a project and use them while working on your code in the IDE. NetBeans IDE includes built-in support for custom annotation processors. Now you can conveniently specify annotation processors to run with your project and see the results of annotation processing directly in the Java Editor through code completion and navigation.
The first part of the tutorial shows the use of the third-party annotation processor, Project Lombok, in the NetBeans IDE.
The second part of the tutorial provides explanations of how to add a self-written annotation processor to a project. The sample code for this part of the tutorial is contributed by Jesse Glick.
To complete this tutorial, you need the following software and resources.
|Software or Resource||Version Required|
|NetBeans IDE||7.0, 7.1, 7.2, 7.3|
|Java Development Kit (JDK)||version 6 or 7|
Annotations are a mechanism of the Java programming language that is used to hold metadata about the elements of your application. Annotations hold meta-information on how the annotated elements should be processed by the compiler, during deployment or at runtime. In other words, annotations are comments to your code that can be processed by other programs and tools.
You can use custom annotations to accomplish a variety of tasks: mark parts of your application (e.g. copyright information, test methods, etc.), automatically generate code, parse command-line options, develop web services, and others. The information on how custom annotations should be processed is passed to the Java compiler through custom annotation processors. JSR 269 implemented in JDK 6 provides an official API for writing annotation processors. You can either write your own custom annotation processors or use third-party solutions.
For starting information about annotations in JDK 6, refer to the following resources:
In practice, annotations are most widely used in combination with Java Persistence API (JPA), which is part of the Java EE specification, and some other technologies, such as JAXB (Java Architecture for XML Binding). Using JPA, Java classes can be annotated as entities that later can be persisted to a storage. To develop JPA-based applications, it is convenient to use frameworks, for example, EclipseLink that is bundled with the IDE. As a starting point for more information on writing JPA-based applications in the NetBeans IDE, see Getting Started with Java EE 6 Applications.
As mentioned above, in Java SE 6 javac, annotation processing was incorporated as an integral functionality of the Java compiler. The compiler automatically searches for annotation processors by default at user class path (unless annotation processing is explicitly disabled). In addition, the search path or a path to particular annotation processors can be specified by using javac options. In the table below, you can see a map of the javac options related to annotation processing and the corresponding commands in the IDE. For more information on javac options in JDK 6, see javac - Java programming language compiler.
Note. In the IDE, the annotation processing options for all Java application with the exception of NetBeans platform applications are specified in the Project Properties window. To open the project's Properties window window, right-click your project and choose Properties.
|Java 6 javac Options||IDE Command||Description|
Project Properties > Build > Compiling > Annotation Processors field.
Specify a fully qualified name of the annotation processor in the Annotation Processors field.
|Explicitly specifies the annotation processor to run. This option eliminates the need to create service provider-configuration files (META-INF/services/javax.annotation.processing.Processor)|
Project Properties > Build > Compiling > Enable Annotation Processing checkbox
When the checkbox is disabled, the project is compiled without any annotation processing.
|Compilation proceeds without any annotation processing. The annotation processor discovery mechanism is not used during compilation.|
Project Properties > Libraries > Processor tab
Specify the path to an IDE project, library or a JAR file that contains an annotation processor. Use this option if an annotation processor and annotations are packaged into separate JAR files.
|Specifies where to find annotation processors; if this option is not given, the classpath is searched for processors (see below).|
Project Properties > Libraries > Compile tab
Specify the path to an IDE project, library or a JAR file that contains an annotation processor and annotation declarations. Use this option if an annotation processor and annotations are packaged into a single JAR file.
|Specifies where to find user class files, and (optionally) annotation processors and source files. This path is searched for annotation processors if the -processorpath option is not specified.|
Project Properties > Build > Compiling > Processor Options field
Add options that should be passed to the annotation processor associated with your project. This value is optional.
|(Optional) Options to pass to annotation processors.|
Read the following parts of the tutorial to learn how to use annotations in the IDE.