Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Develop Aspect-Oriented Java Applications with Eclipse and AJDT

By Matt Chapman, Dr. Helen Hawkins
2005-05-16


The Spacewar example

Now that we have introduced AJDT with a simple example, it's time to explore the support it offers to AspectJ programmers working on slightly larger projects. We will revisit the Outline view, see how to selectively add and remove aspects from an application, and finally see how to get an overview of a project using the powerful Visualization perspective.

Adding aspects to existing Java projects
If you have an existing Java project to which you want to add aspects, you can easily convert it to an AspectJ project. Just select the project you wish to convert in the Package Explorer, right-click to display the contextual menu, and select Convert to AspectJ Project. Similarly, if for some reason you wish to convert it back to a Java project, select your AspectJ project in the Package Explorer and select Remove AspectJ Nature from the contextual menu.

Helpfully, AJDT includes the examples from the core AspectJ distribution, plus a wizard that makes adding them to your workspace very easy. The Spacewar example is one of the larger examples, and it serves our purpose well. (Again, for this portion of the article, you will need AJDT version 1.1.11 or later, as the Spacewar example wasn't included in earlier versions.) Let's get right to it:

  1. In Eclipse, go to File>New>Other

  2. In the resulting dialog, expand the AspectJ folder, then the AspectJ Examples folder, and select Spacewar Example.

  3. Click the Next button. You can enter a name for the project or accept the default.

  4. Click Finish and the New Examples wizard will create an AspectJ project for you, add all of the required files for this example to it, and switch to the Java perspective, if you're not there already.

If you have Eclipse set to build automatically, the example will also be compiled -- if not, you'll need to trigger the build yourself, as we did above with our simple application.

Let's check to see that everything came out OK, and see what the example looks like when it runs. The main() method for the example is in Game.java in the spacewar package; to launch it, select the file, and go to Run>Run As>Java Application. Two game windows should then appear. You use the cursor keys and space bar to control the game. The rest you can figure out for yourself!

We will now explore some of the Spacewar code to see more of the Outline view and editor markers in action. Let's take a look at the SpaceObjectPainting aspect, which is defined as an inner aspect in the Display1 class. Open Display1.java in the editor and expand the node for the SpaceObjectPainting aspect in the Outline view, as shown in Figure 5.

Figure 5. Outline view of SpaceObjectPainting aspect

Here we see several icons, different from the around advice icon we saw earlier. The first represents an abstract inter-type declaration -- the aspect is defining a paint() method on behalf of the SpaceObject class. You can see this by expanding the node. Next is another inter-type declaration, for a field this time, Ship.color. Next comes some after returning advice declared on an anonymous pointcut, which applies when a method returns normally -- that is, without throwing an exception. Some of the other icons you might see in the Outline view, and as markers in the gutter of the editor, are:

  • Before advice ()
  • Declare parents (), where a class is defined to implement an interface, or extend a class
  • Declare warning () or declare error (), which can be used to trigger a compiler warning or error if a certain policy is not adhered to
  • Declare soft (), where a checked exception has been softened to an unchecked exception


Tutorial Pages:
» Updated tools make AOP easier for beginners and veterans alike
» Installing AJDT
» First steps
» The Outline view and editor markers
» The Spacewar example
» Build configurations
» Aspect Visualization perspective
» Debugging
» Generating documentation
» Further options
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Java Validation With Dynamic Proxies

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources