Most Read This Week
Java Annotation Facility - A Primer
JDK 5 Has Changed Source Code Generation in a Seminal Way
Mar. 9, 2005 12:00 AM
Annotation Processing Tool
When invoked, the apt goes through the following sequence of operations: First, it determines what annotations are present in the source code being operated on. Next, it looks for annotation processor factories. It then asks the factories what annotations they process and, if the factory processes an annotation present in source files being operated on, the apt asks the factory to provide an annotation processor. Next, the annotation processors are run. If the processors have generated new source files, the apt will repeat this process until no new source files are generated. This high-level sequence is indicated in Figure 1.
To write a factory class, a developer has to rely on packages that aren't part of the standard SDK. The packages used are:
Using Annotation for Generating Struts-config.xml
Before we get to the nitty-gritty of annotating and generating configuration files, we need to understand why this needs to be done.
We need to do this because a developer who has to write code using the struts framework finds himself copying and pasting information from the source code to the deployment descriptor and vice versa. For instance, if we change the name of the Action class and don't change the XML file, the application doesn't work correctly. The way to sidestep this issue is to isolate the changes to one location and let the utility tool generate the deployment descriptor. We will cover how to use the metadata facility to achieve this automatic configuration file generation.
Before we start we have to make sure that we have the right development tools to do what we're trying to do. Currently very few IDEs support Java 5. Among Open Source IDEs, NetBeans 4.0 beta 2 looks promising (in spite of a few runtime exceptions) so most of the code in this article was written and tested with it. The first step in the process is defining the annotation types for the various elements that make up the struts framework. The component parts of the framework are: Action, Form bean (also known as Action Forms), Exceptions, Validator, Plug-ins etc.
The annotation type for Struts Action is as follows:
As you can see, I have the annotation types as return types. This is necessary because we could have forward and exception elements defined by the action element. The XML snippet from that struts-config.xml that we're trying to generate will clarify the need for embedding other annotation types in the action annotation-type declaration.
The next step is to annotate the code with the annotation type. In our case we will annotate a struts action class. This is done as follows:
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads