Learn AspectJ Using Eclipse AJDT | 2 | WebReference

Learn AspectJ Using Eclipse AJDT | 2


Learn AspectJ Using Eclipse AJDT [con't]

Building an AspectJ Project from Scratch

  1. Inside Eclipse, select the menu item File > New > Project... to open the New Project wizard.
  2. Select AspectJ Project and then click Next.

    Figure 4. New Project Dialog

  3. On the next page, type "AspectJ Tutorial" in the Project name field and click Finish.

    Figure 5. AspectJ Project Dialog

    The Java perspective opens inside the workbench with the new AspectJ project in the Package Explorer. Notice that the AspectJ libraries have been imported into the project for you:

    Figure 6. AspectJ Project in Package Explorer

  4. Select the "AspectJ Tutorial" project in the Package Explorer and then select File > New > Package.... to open the New Java Package wizard. Type "com.webreference.aspectj_tutorial" in the name field and click Finish:

    Figure 7. Java Package Dialog

  5. Select the package that you just created in the Package Explorer and then select File > New > Class.... to open the New Java Class wizard. Type "MainConcern" in the name field, select the option to allow Eclipse to create a main method and click Finish:

    Figure 8. Java Class Dialog

  6. Type the following into your new class:

  7. The Date and DateFormat objects won't be recognized by the compiler, so they will trigger errors. You'll need to import both. To do that, you can click on the red X and select the "Import '<CLASSNAME>' (<PACKAGE NAME>)" from the popup menu:

    Figure 9. Suggestion Popup

  8. You can run the main application now to see what the MainConcern process displays. To do that, right-click the MainConcern.java file in the Package Explorer and select "Run As -> 2 AspectJ/Java Application" from the popup menu:

    Figure 10. Run As... Popup Menu

    That will produce the current time in the Console:
  9. Figure 11. showTime() Output

Create the Aspect

With our main class completed, we can now turn our attention to the creation of an Aspect, which is the modularization of one cross-cutting secondary concern.

  1. In the Package Explorer view, select the "com.webreference.aspectj_tutorial" package. From the package's context menu, select New > Aspect.

    Figure 12. New Aspect Dialog

    Make sure that "AspectJ Tutorial/src" appears in the Source Folder field and that "com.webreference.aspectj_tutorial" appears in the Package field.
  2. In the Name field, type "SecondaryConcern":

    Figure 13. New Aspect Properties Dialog

  3. A dialog will appear asking you if you'd like to switch to Aspect view. Click OK. The new file is opened in the editor. It contains the new aspect, the constructor and comments.
  4. Change the body of the aspect to the following:

    Aspects (classes annotated with @Aspect) may have methods and fields just like any other class. They may also contain pointcut, advice, and introduction (inter-type) declarations. Pointcuts determine join points of interest and thus enable us to control when advice executes. These join points can be method or constructor invocations and executions, the handling of exceptions, field assignments and accesses, etc. A pointcut declaration has two parts: a signature (comprising a name and any parameters) and a pointcut expression that determines exactly which method executions we are interested in.

    AOP supports a number of AspectJ pointcut designators for use in pointcut expressions, including call, handler, execution, within, cflow, this and target. Execution is the primary pointcut designator you will use when working with AspectJ. It sets the pointcut to when a particular method body executes. The pointcut is identified by a signature that can include wild cards. In this case, there are wild cards in the return type position (the *) and in the argument list position (..).

    A piece of advice brings together a pointcut and a body of code to define aspect implementation that runs at join points picked out by the pointcut. Our after returning advice runs just after the join point, but only if it returns normally. Without the returning() qualifier, the advice would run regardless of whether the MainConern() method returns normally or throws an exception. Other advice types include before() and around().

  5. Run the program again by clicking the Run button on the main toolbar. This time a second line appears with our sign-off:

    Figure 14. showTime() Output with Aspect

Adding Aspects to an Existing Java Project

If you have an existing Java project to which you want to add aspects, you can easily convert it to an AspectJ project. Select the project you wish to convert in the Package Explorer, right-click to display the contextual menu, and select "Convert to AspectJ Project":

Figure 15. Convert to AspectJ Project Menu

That will import the AspectJ libraries. From there the process to create your aspects is the same as we did above.


I have found AspectJ to be a great way to implement additional application logging and debugging during development. I can easily convert any Java application into an AspectJ project for unit testing, and then convert it back again for production rollout by highlighting the AspectJ project in the Package Explorer and selecting "Remove AspectJ Capability" from the contextual menu.

Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest release is called "The Rabbit of Seville". Loosely based on Rossini's The Barber of Seville overture, Rob's amazing rendition includes a full orchestra and numerous guitar tracks. It is sold on his site as a high bitrate MP3 for only $0.99 cents! Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: September 13, 2010