Feeds

Aspect oriented programming with Java

A practical introduction

Reducing security risks from open source software

A simple example

As a simple example of an implementation of an Aspect let us implement the world's simplest logging Aspect. To do this we will log what happens during the execution of the classic Hello World Java program. For the purposes of this example, the Hello World program I shall use is presented below:

package com.reg.dev.aspects;

public class HelloWorld {

        public void print() {
                System.out.println("Hello World");
        }
        
        public static void main(String [] args) {
                HelloWorld hw = new HelloWorld();
                hw.print();
        }
}

We can now implement an Aspect using AspectJ to provide for system logging. To do this I downloaded the AJDT: AspectJ Development Tools Eclipse Based plug-in (make sure you get the correct version for your Eclipse) from here. This provided me with not only the AspectJ runtime, but also various Wizards and browsers to support AOP development.

I then implemented my logging aspect within Eclipse. A screen dump of this is presented in Figure 1. Note that the file generated has a *.aj extension and that the implementation of the before and after advice is a piece of standard Java code. Before exploring what the implementation does, let us describe in plain English the purpose of this Aspect. Essentially this aspect specifies that before executing the HellWorld.print method, we will printout "Entering print" and after returning normally from the method we will printout "Existing print".

Screenshot showing an AspectJ Logger Aspect

In the example in Figure 1, the whole aspect is called Logger, log is the pointcut that implements the join point for this aspect and there are two advice points. Note that AspectJ makes the following pointcuts available:

  • Method call and execution
  • Constructor call and execution
  • Read/write access to a field
  • Exception handler execution
  • Object and class initialization execution

In the example above, I have defined a single execution pointcut. This specifies that on the execution of the HelloWorld.print method two advices should be applied. One before the method executes and one after the method has returned (other options include running after an exception has been thrown). The advice implements the behaviour of the aspect.

To run the combined application (i.e. the HelloWorld program and the Aspect) within Eclipse, I merely run the HelloWorld application as normal. However, the byte codes of the class and the aspect point cuts are dynamically weaved to create the following output:

Entering print
Hello World
Exiting print

Thus the core behaviour of the HelloWorld program and the behaviour of the logging Aspect have successfully been interwoven in the running system without the need to clutter up the original clean source code with calls to the logging subsystem.

Development methodology

A quick note on AOP development. It is clear to me that to identify the concerns which should be implemented as an Aspect is not necessarily straightforward nor is it directly covered by traditional software engineering methodologies. However, a fairly intuitive approach is to separate the AOP development process into some basic steps. These are essentially:

  • Aspectual decomposition: Decompose systems requirements in order to identify crosscutting and common concerns.
  • Concern implementation: Design and implement each concern as a separate aspect. Test and debug.
  • Aspectual re-composition: Identify the cross cut points within your application where the aspect should be applied.

See the References, below for more information on Aspect Oriented Software Development.

Summary

AOP is certainly an important element in the Software Engineers toolbox. It is not, I believe, a replacement for Object Orientation, rather it is an augmentation. Using Java to implement the core business logic of an application is still an excellent approach. Indeed, using Java to implement the core behaviour of an aspect also works very well. What AOP gives you is a way of separating and managing the different aspects of modern, complex, software systems.

References

  • The AspectJ Programming Guide (a CVS log of updates to this is available here).
  • Aspect-Oriented Programming, Proceedings of the European Conference on Object-Oriented Programming. Kiczales, Gregor, John Lamping, Anurag Mendhekar, Chris Maeda, Cristina Lopes, Jean-Marc Loingtier, and John Irwin (1997). Vol.1241, pp.220-242. This is the seminal paper originating AOP.
  • Aspect-Oriented Software Development. Filman, Robert E.; Elrad, Tzilla; Clarke, Siobhan; Aksit, Mehmet; ISBN 0-32121-976-7; available at Cash 'n' Carrion here.
  • AspectJ in Action: Practical Aspect-Oriented Programming. Laddad, Ramnivas; ISBN 1-93011-093-6; available at Cash 'n' Carrion here.

The Power of One eBook: Top reasons to choose HP BladeSystem

More from The Register

next story
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Mozilla fixes CRITICAL security holes in Firefox, urges v31 upgrade
Misc memory hazards 'could be exploited' - and guess what, one's a Javascript vuln
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
Apple: We'll unleash OS X Yosemite beta on the MASSES on 24 July
Starting today, regular fanbois will be guinea pigs, it tells Reg
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
prev story

Whitepapers

Top three mobile application threats
Prevent sensitive data leakage over insecure channels or stolen mobile devices.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.