Chapter 1. Aspect Orientation Overview
This chapter gives a brief overview of aspect orientation. What is discussed here is by no means a definitive description of aspect-oriented concepts, but it should present a flavor of these concepts and the related terminology. This provides a reference that will prove useful as you implement the practical recipes throughout the rest of the book.
A Brief History of Aspect Orientation
Aspect orientation is not a completely new approach to writing software. In much the same way as virtual machine systems were not an entirely new concept when Java became recognized and adopted by the software community, there have been tools and development environments for some time that support some of the capabilities that are now being placed under the banner of aspect orientation. Like Java, aspect orientation is becoming a commonly adopted and de facto approach to practicing older ideas that can be traced to almost the beginning of software development.
Development environments and tools that weave code,
pragma
instructions, and even debuggers all
contain some of the behavior that underlies the aspect-oriented
approach. But the significant difference is in the philosophy behind
the approach and how that philosophy drives the technology and tools.
Aspect orientation is not about any one of these technologies on its
own, though it is a new and more modular implementation of the
advantages that these technologies have brought to their own domains
in the past.
All that said, the philosophical and conceptual underpinnings of aspect orientation are not a subject for this type of book. If you are interested in finding out more about this side of the technology, it’s best to search Google for “Aspect-Oriented Software Development.” This book focuses on practical approaches to understanding the technology; it is about getting the job done by harnessing the impressive power of aspect-oriented software development.
AspectJ
It is fair to say that the most significant work to date that is actually labeled under the banner of aspect orientation was completed at that historical wellspring of computing innovation, the Xerox Palo Alto Research Center (PARC). Xerox initially invested in producing special-purpose aspect-oriented languages prior to moving to a general-purpose model in Java. AspectJ was the outcome of this effort and is the core development tool for the recipes found throughout this book.
At the time of this writing, AspectJ is a rapidly maturing aspect-oriented development tool with a wealth of examples available. In 2002, Xerox PARC made the important decision of transferring the development of AspectJ to a more open forum on the eclipse.org web site. Current download figures for AspectJ show that interest in the approach is increasing at an exponential rate, and that the software development community is recognizing that aspect orientation is an extremely important evolution in software development. Now is the time to use this book’s real-world, aspect-oriented recipes to add this new and powerful tool to your software development repertoire.
A Definition of Aspect Orientation
Before getting into the actual recipes, it is worth briefly introducing some of the concepts and terms that are used throughout this book.
Cross-Cutting Concerns
The basic premise of aspect-oriented programming is to enable developers to express modular cross-cutting concerns in their software. So what does this mean? A cross-cutting concern is behavior, and often data, that is used across the scope of a piece of software. It may be a constraint that is a characteristic of your software or simply behavior that every class must perform.
The most common example of a cross-cutting concern, almost the “Hello World” of the aspect-oriented approach, is that of logging (covered in Chapter 21). Logging is a cross-cutting concern because it affects many areas across the software system and it intrudes on the business logic. Logging is potentially applied across many classes, and it is this form of horizontal application of the logging aspect that gives cross-cutting its name.
Aspects
An aspect is another term for a cross-cutting concern. In aspect orientation, the aspects provide a mechanism by which a cross-cutting concern can be specified in a modular way. To fully harness the power of aspects, we need to have some basic concepts in place to allow us to specify and apply aspects in a generic manner. We must be able to:
Define the aspects in a modular fashion
Apply aspects dynamically
Apply aspects according to a set of rules
Provide a mechanism and a context for specifying the code that will be executed for that particular aspect
The aspect-oriented approach provides a set of semantics and syntactical constructs to meet these demands so that aspects can be applied generically regardless of the type of software being written. These constructs are advice, join points, and pointcuts.
Advice
The code that is executed when an aspect is invoked is called advice. Advice contains its own set of rules as to when it is to be invoked in relation to the join point that has been triggered.
Tip
Chapter 13 deals directly with recipes for different forms of advice and shows some of the more advanced features of advice that are available within AspectJ, such as precedence between multiple advices.
Join Points
Join points are simply specific points within the application that may or may not invoke some advice. The specific set of available join points is dependent on the tools being used and the programming language of the application under development. The following join points are supported in AspectJ:
Join when a method is called
Join during a method’s execution
Join when a constructor is invoked
Join during a constructor’s execution
Join during aspect advice execution
Join before an object is initialized
Join during object initialization
Join during static initializer execution
Join when a class’s field is referenced
Join when a class’s field is assigned
Join when a handler is executed
Pointcuts
Pointcuts are the AspectJ mechanism for declaring an interest in a join point to initiate a piece of advice. They encapsulate the decision-making logic that is evaluated to decide if a particular piece of advice should be invoked when a join point is encountered.
The concept of a pointcut is crucial to the aspect-oriented approach because it provides an abstract mechanism by which to specify an interest in a selection of join points without having to tie to the specifics of what join points are in a particular application.
Tip
How to define and use pointcuts is shown in the recipes found in Chapter 4 though Chapter 12.
Putting It All Together
Figure 1-1 shows the relationships between join points, aspects, pointcuts, advice, and your application classes.
Where to Go for More Information
For more detailed information on the concepts and philosophy behind aspect orientation, check out the following web sites:
- http://www.parc.xerox.com/research/csl/projects/aspectj/default.html
The official information on the AspectJ project at Xerox PARC.
- http://www.eclipse.org/aspectj
The official AspectJ development technologies download site with links to support tools.
- http://www.eclipse.org/ajdt
Eclipse has a set of AspectJ Development Tools collected as a downloadable feature using the built-in update site mechanism from the listed web site.
- http://sourceforge.net/projects/aspectj4jbuildr/
A plug-in for Borland JBuilder that allows integration with AspectJ is available at this site.
- http://sourceforge.net/projects/aspectj4netbean/
A plug-in for NetBeans to support AspectJ development is available from this web site.
Get AspectJ Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.