Annotation Processors Support in the NetBeans IDE
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 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.
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.
Read the following parts of the tutorial to learn how to use annotations in the IDE.