2.4 Creating the PolicyChangeNotification Aspect
The Simple Insurance project is now ready for use with AspectJ. It was automatically built by the AspectJ compiler when we converted it to an AspectJ project, so now would be a good time to run the test suite again and verify that the AspectJ compiler has indeed built the project correctly. The tests should all pass correctly.
We are ready to begin the refactoring. Recall that we want to replace the calls to notifyListeners that are scattered throughout the policy class hierarchy with a modular implementation in an aspect. Just as to create a new class we would use the New Class Wizard, so to create a new aspect we use the New Aspect Wizard.
2.4.1 The New Aspect Wizard
We want to create an aspect in the insurance.model.impl package where the policy implementation classes are defined. Select the insurance.model.impl package in the Package Explorer, and from the context menu choose New > Aspect. This launches the New Aspect Wizard, as shown in Figure 2.17. (You can also get to this wizard from the File > New workbench menu, and the New icon on the toolbar.)
Figure 2.17 The New Aspect Wizard.
Notice that the New Aspect Wizard is very similar to the New Class Wizard. The source folder and package fields are prefilled because we launched the wizard from the context menu of the package in the Package Explorer. We just need to provide a name for the aspect and then click Finish. Because the aspect is going to encapsulate the implementation of policy-change notification, we have called it PolicyChangeNotification. Upon completion of the wizard, the aspect is created and opened in the editor, as shown in Figure 2.18.
Figure 2.18 A skeletal aspect.
In the Package Explorer, you can see that the wizard has created a new source file called PolicyChangeNotification.aj. AJDT uses the .aj extension for source files containing aspects, but you can configure it to use .java instead if you prefer. See the sidebar "Choice of File Suffix: .java or .aj"
In the editor, you can see the basic form of an aspect. Notice that it looks just like a class definition, except that it uses the aspect keyword in place of the keyword class. In AspectJ, aspects are first class entities in the program just as classes are. There are a lot of similarities between aspects and classesin fact pretty much anything you can declare in a class you can declare in an aspect, too, but aspects can also do things that classes cannot do. We look at some of those things in Section 2.5.
If you have the Outline view open (Window > Show View > Outline), you will see an outline like that shown in Figure 2.19. This shows us that the source file contains no import declarations (the package statement and any import declarations work in exactly the same way for an aspect as they do for a class), and a single aspect with no members.
Figure 2.19 The Outline view.
Chapter 9 provides full details on aspects in AspectJ, but you already know a surprising amount because of your familiarity with classes in Java. You certainly know enough to continue following the examples in this chapter, so let's move on and make our PolicyChangeNotification aspect do something useful.
Choice of File Suffix: .java or .aj
The AspectJ compiler doesn't care whether you name your source files with the .aj extension or with the .java extensionit treats them both equally. So you could name all your source files with the .aj extension, or all with the .java extension, or with any combination of the two, and it wouldn't make any difference. It does make a difference, however, if you convert an AspectJ project back to a Java project for some reason. The Java compiler ignores files with an .aj extension, which can make the move easier (but you're still going to have to cope with the fact that all the functions your aspects were implementing are now missing). However, using the .aj extension for files containing regular Java classes has the consequence that the types defined in them are not visible to Eclipse's Java model. By default therefore, AJDT creates new classes in source files with a .java extension, and new aspects in source files with an .aj extension. If you want to change this behavior, you can do so via the AspectJ project Properties page accessible via the Properties option in the context menu when a project is selected.