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


First steps

Now that you've installed AJDT, let's explore how it supports the creation and running of AspectJ applications. To this end, we'll create a simple AspectJ application consisting of just one aspect and one class, and in so doing examine the AJDT features that we'll need to achieve this. The application is slightly contrived, but it serves our purpose. In it, we simply calculate the square of two numbers. At first, every time the program is given a number, it calculates the square from scratch -- and we've added a Thread.sleep() call within this method to ensure that this takes some time. By adding a cache aspect, however, we can check to see whether the result has already been calculated for a given input. If so, the cached value is returned; if not, then the square is calculated as normal and the result is added to the cache. Thus, the cache aspect will improve our application's performance. (For a discussion on using aspects for caching, please follow the Aspects Blog link in the Resources section.)

If you've used the JDT Eclipse tools for Java development, then using AJDT for AspectJ development will be a familiar experience. To create a new AspectJ project, follow these steps:

  1. Select Window>Open Perspective>Java to open the Java perspective.

  2. Select File>New>AspectJ Project to open the New AspectJ Project wizard.

  3. Call the project Caching Example, click Next, and add a source folder named src.

  4. Click Finish to create the new project in your workspace.

Note: If this is the first AspectJ project you've created in your workspace, the AJDT Preferences Configuration wizard will appear after step 4. This wizard configures some Eclipse settings that will make your life easier. Accept the wizard's defaults and click Finish.

Expand the project node in the Package Explorer and you'll see that the AspectJ runtime library aspectjrt.jar has been added in the same way that the JRE system library was. Now, select your project and go to File>New>Package to create a package called caching, and go to File>New>Class to create a class called Application in your caching package. This is just the same as if the project were a Java project.

Now edit your class so that it looks like Listing 1.

Listing 1. Main class for our application


package caching;

public class Application {

public static void main(String[] args) {
System.out.println("The square of 45 is " + calculateSquare(45));
System.out.println("The square of 64 is " + calculateSquare(64));
System.out.println("The square of 45 is " + calculateSquare(45));
System.out.println("The square of 64 is " + calculateSquare(64));
}

private static int calculateSquare(int number) {
try {Thread.sleep(7000);}
catch (InterruptedException ie) {}
return number * number;
}
}

From the code in Listing 1, you can see that in our class we have a main() method that makes four calls to the calculateSquare() method, the second pair of calls being a repeat of the first pair. The calculateSquare() method does nothing for a bit and then returns the square of the input argument.

Before adding our cache aspect, let's run the application. As with a Java application in a Java project, to run the application you need to select in the Package Explorer the Java class that contains the main() method. In our case, this is our only class: Application.java. Once you've selected this class, right-click it to open the contextual menu and select Run>Java Application. You'll notice that it takes a while to calculate the squares.

Now let's add an aspect to our application. The process of creating an aspect has the same look and feel as the process of creating a Java class. To create an aspect called Cache, go to File>New>Aspect to open the New Aspect wizard. As with the New Java Class wizard, you'll need to choose the package in which to create your aspect, give it a name, and then click Finish. In the Package Explorer, you should see that a file called Cache.aj has been created. By default, aspects are created in files with the .aj extension, although you can have aspects in .java files, too.

Edit Cache.aj so that it contains the code in Listing 2.

Listing 2. An aspect to add a cache to the our application


package caching;

import java.util.Hashtable;

public aspect Cache {
private Hashtable valueCache;

pointcut calculate(int i) : args(i)
&& (execution(int Application.calculateSquare(int)));

int around(int i) : calculate(i) {
if (valueCache.containsKey(new Integer(i))) {
return ((Integer) valueCache.get(new Integer(i))).intValue();
}
int square = proceed(i);
valueCache.put(new Integer(i), new Integer(square));
return square;
}

public Cache() {
valueCache = new Hashtable();
}
}

From the code in Listing 2, you can see that in our Cache aspect, we first create a hashtable to record the numbers and their squares. We then specify a pointcut called calculate. This matches the execution of any method named Application.calculateSquare() that accepts and returns an integer value. Because we only want to execute the Application.calculateSquare() method if the square for the given number hasn't already been calculated, we'll use around advice. The advice takes an integer argument and returns an integer value. In the body of our advice, we first check to see if the input value is in the hashtable. If it is, we return the corresponding result; if it isn't, then we proceed with the execution of the calculateSquare() method. Before returning from the around advice, we update the hashtable with these new values.

If you have Eclipse set to build projects automatically, all that's left is to run your AspectJ application! If this is not the case, select your AspectJ project in the Package Explorer and click the Build button, as shown in Figure 1. You're now ready to go. Since you've already run an earlier version of this application, you should have a run configuration all set up. Therefore, to run the AspectJ application, select Run button from the task bar (Figure 1), then select Application. You'll notice that it now takes much less time to calculate the squares the second time around!

Figure 1. Build and run buttons



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