2.3 Creating an AspectJ Project
The first thing we need to do before we can start using AspectJ in the Simple Insurance application is to convert the Simple Insurance project from a plain Java project into an AspectJ project. Converting the project to an AspectJ project enables us to use the AspectJ language to implement the application, and the AspectJ compiler to build it.
2.3.1 Converting an Existing Java Project
Converting an existing Java project to an AspectJ project is simple. Select the project in the Package Explorer, and choose Convert to AspectJ Project from the context menu (see Figure 2.10). The AJDT Preferences Configuration Wizard may pop up during the conversion process; refer to the next section to learn more about it.
Figure 2.10 Converting from a Java project to an AspectJ project.
After performing the conversion, you will notice a couple of changes in the Package Explorer, as highlighted in Figure 2.11. First, the project icon has changed from the Java project icon (a folder with a J decoration, as shown in Figure 2.10), to the AspectJ project icon (a folder with an AJ decoration). Second, a new jar file has been added to the project's build path, using the Eclipse path variable ASPECTJRT_LIB. This path variable is defined by the AJDT plug-ins to point to the AspectJ runtime library Jar file (aspectjrt.jar) shipped with AJDT. The AspectJ runtime library is a small library (about 35K) that all AspectJ programs need when they execute. This is the only runtime requirement for AspectJthat aspectjrt.jar be somewhere on the class paththe AspectJ compiler produces regular Java class files that can be executed on any Java Virtual Machine (JVM) and look just like any other Java application to the JVM. Third, a new file called build.ajproperties has been created in the project. This file stores configuration information about the building of the project, which we look at in detail later.
Figure 2.11 An AspectJ project.
If you ever want to turn an AspectJ project back into a regular Java project, the process is equally simple: select the project in the Package Explorer and choose Remove AspectJ Nature (see Figure 2.12). See the sidebar "Eclipse Builders and Natures" if you are curious as to what goes on when a project is converted to an AspectJ project and back again.
Figure 2.12 Converting an AspectJ project back into a Java project.
Eclipse Builders and Natures
Projects in Eclipse have one or more project natures associated with them. A project nature tells the rest of the world what kind of capabilities the project has. Java projects have the Java nature, and tools that work with Java programs can offer their workbench contributions for working with those projects. AspectJ projects have both the Java nature and the AspectJ nature, so Java tools should continue working, but they also benefit from the AspectJ specific tools too. Builders are closely associated with natures. Normally when a nature is added to a project, it installs a project builder. Projects can have multiple builders, just as they can have multiple natures. Java projects use the Java builder (the Eclipse Java compiler), but AspectJ projects use the AspectJ builder. When you convert a Java project to an AspectJ project, the AspectJ nature is added to the project, and the Java builder is replaced with the AspectJ builder (any other builders defined for the project are left alone). When you remove the AspectJ nature from a project, the AspectJ nature is removed, and the AspectJ builder is replaced with the Java builder. Of course, if you have AspectJ-specific artifacts in your projects, you might get build errors when the Java builder encounters them.
2.3.2 Configuring the Workbench for AspectJ Development
The very first time you activate the AspectJ tools inside a workspace (for example, by converting a project to an AspectJ project, or by creating a new AspectJ project) you will see the AJDT Preferences Configuration Wizard appear (see Figure 2.13).
Figure 2.13 AJDT Preferences Configuration Wizard.
This wizard offers to make two configuration customizations for you that will make working with AspectJ inside Eclipse a much more pleasant experience:
Make the AspectJ editor default for ".java" The standard Java editor that is associated with .java files does not understand the extra keywords that AspectJ introduces, and neither does the Outline view associated with it understand aspect constructs. Choosing this customization makes the AspectJ editor be associated with .java files by default. The AspectJ editor is an extension of the Java editor offering all of the Java editor features, plus the capability to understand AspectJ programs.
Disable analyzing of annotations while typing The Java Development Tools include an eager parser that analyzes your program as you type, giving you early indications of problems you will have when you compile the code. This eager parser does not understand AspectJ; hence it incorrectly reports errors (the infamous "red squiggles") in your AspectJ program, which can be distracting. Figure 2.14 shows an example of the problem. The eager parser does not recognize the type AnAspect (because aspects are not present in the type-space that it uses for name resolution), and so highlights it as a problem when in fact the AspectJ compiler will build the source file perfectly fine. Selecting this customization deactivates the eager parsing. The AJDT development team is working on an update to the tools support that enables the eager parser to understand AspectJ constructs, too. When this new version is available, it will be possible to turn the "analyze annotations while typing" option back on again for AspectJ programs.
Figure 2.14 Don't get the red squiggles; disable "analyze annotations while typing."
2.3.3 Creating a New AspectJ Project
Of course, it is also possible to create a new AspectJ project from scratch without converting an existing Java project. To do this, you use the New AspectJ Project Wizard. You can reach the wizard via the Eclipse workbench menus by selecting File > New > Project, and then AspectJ Project. Alternatively, use the New icon on the toolbar and select AspectJ Project from there (see Figure 2.15).
Figure 2.15 Launching the New AspectJ Project Wizard.
When the wizard launches, the dialog box shown in Figure 2.16 displays. It behaves exactly like the New Java Project wizard, except that the project it creates at the end is an AspectJ one.
Figure 2.16 The New AspectJ Project Wizard.