Learn AspectJ Using Eclipse AJDT | WebReference

Learn AspectJ Using Eclipse AJDT

By Rob Gravelle


[next]

No idea what aspect-oriented programming (AOP) or pointcuts are? No worries, you've come to the right place! This article is all about AspectJ, the Java-based de facto standard for aspect-oriented programming (AOP). Released in 2001, it uses Java-like syntax, emphasizes simplicity and usability for end users, and is included in integrated development environment (IDE) plugins for displaying cross-cutting structure.

In this article, I demonstrate this exciting technology by creating an AspectJ project using the Eclipse AspectJ Development Tools (AJDT).

Separation of Web Components

From my earliest days as a Web developer I have seen a gradual shift from all-encompassing HTML pages toward a new design idiom of creating distinct components made up of HTML content, external JavaScript files, Cascading Style Sheets (CSS), and even XML. The impetus for separating contents from presentation was machine readability, a key ingredient of the Web 2.0 paradigm.

A similar transformation has been taking place in the business logic tier. In addition to object-oriented programming (OOP), whose aim is to decompose real world problems into objects that abstract behavior and data in a single unit, AOP emerged in the early 2000s to better isolate secondary or supporting functions from the main program's business logic.

The Problem Defined

Many frameworks have emerged in recent years in an effort to minimize the writing of "boilerplate" code. That is, code that all but the most basic applications require, including database transactions, security and logging. In AOP parlance, such processes all exemplify what we would call cross-cutting concerns. They are thusly named because these are aspects of a program that affect other concerns. Typically, each of these aspects is scattered among or tangled with other code, making it harder to understand and maintain. Moreover, because the aspects are spread over a number of unrelated functions, changing one can require modifying numerous methods and/or modules.

In the following code, which reads data from a text file, the simplicity of this simple action is obfuscated by the peripheral steps required to create the XMLHttpRequest object and handle the response:

Here is the same code rewritten using the Prototype framework.

One thing that frameworks such as Protoype.js do well is to hide the boilerplate code so that the specific actions of the method are made more apparent. Unfortunately, what non-AOP frameworks lack is the ability to separate common recurring tasks from the main code line. Consider the following Java method, which is littered with calls to the logger (whose only function is to debug the code):

There are really two concerns in the above code:

  • The main business concern, or initializing and running some scheduled events
  • The cross-cutting logging concern

AOP helps to alleviate this problem by allowing the programmer to express cross-cutting concerns in standalone modules called aspects. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). Storing cross-cutting concerns as aspects allows both the source code and the places to execute it to be maintained in one place.

It's time to try our hand at creating our own aspect.

Setting Up the AJDT Environment

The AspectJ Development Tools (AJDT) project provides Eclipse platform-based tool support for aspect-oriented software development with AspectJ. Eclipse AJDT makes it a snap to create new AOP applications or add aspects to existing ones. We'll be doing both, starting with creating a new AspectJ project.

On the Eclipse AJDT download page, you'll find builds for various versions of Eclipse. Select the one that matches your version of Eclipse.

Alternatively, you can do the following:

  1. Go to Help->Software Updates->Find and Install... .
  2. Search for new features to install and add a new remote site called AJDT Update Site with the URL https://download.eclipse.org/technology/ajdt/30/update. If you need a proxy server to access the Internet, you can set your proxy preferences via Window>Preferences>Install/Update.
  3. Expand the AJDT Update Site node and select AspectJ.
  4. Choose to install the Eclipse AspectJ Development Tools, and then read and accept the copyright statement.
  5. Click Finish.

If you don't have Eclipse, you can download it here. Extract the files to the base directory of your Eclipse installation. The files are arranged so that the plugins will go into the plugins subfolder:

Figure 1. Eclipse AJDT Plugins in Explorer

When you launch Eclipse, you'll be greeted by the Welcome page. It will contain a link to the AspectJ development documentation.

Figure 2. Eclipse Welcome Page

There will also be two AspectJ branches in the Help Contents tree:

Figure 3. Eclipse Help

We are now ready to build our project.


[next]