I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

A Tour of AOP

09.12.2008
| 26512 views |
  • submit to reddit

The AOP Implementation

The AOP implementation performs two logical steps: It first combines the individual concerns using the weaving rules, and then it converts the resulting information into executable code. AOP implementation, thus, requires the use of a processor—weaver—to perform these steps.

The weaver can be implemented in various ways. A simple way is through source-to-source translation. Here, the weaver processes source code for individual classes and aspects and produces woven source code. A regular language compiler may then process the formed code. The aspect compiler then feeds this converted code to the base language compiler to produce the final executable code. Using this approach, a Java-based AOP implementation converts individual source input files into woven Java source code and then lets the Java compiler convert it into the byte code (in fact, this implementation technique was used in early implementations of AspectJ). This simple approach suffers from several drawbacks as the executable code cannot be easily traced back to the source files written. For example, stack traces would indicate line numbers in woven source modules.

Another approach could be that the source code would first be compiled into class files using the base language compiler. The class files would then be fed to the aspect compiler, which would weave the aspects into the class files to produce woven class files. Figure 1 shows a schematic of a compiler-based AOP language implementation.

Figure 1 An AOP language implementation that provides a weaver in the form of a compiler. The compiler takes the implementation of the core and crosscutting concerns and weaves them together to form the final system.Figure 1 An AOP language implementation that provides a weaver in the form of a compiler. The compiler takes the implementation of the core and crosscutting concerns and weaves them together to form the final system.

It may also be possible to push the weaving process close to execution of the system. For example, if the implementation of AOP is Java-based, a special class loader or a VM agent could perform the weaving. Such an implementation will first load the byte code for the aspects, weave them into the classes as they are being loaded, and supply those woven versions of the classes to the underlying virtual machine (VM).

Yet another implementation possibility is through use of automatically created proxies. In this case, each object that needs weaving may be wrapped inside a proxy. Such implementation typically works well in conjunction with another framework that controls creation of objects. In this way, the framework can wrap each created object in a proxy.

So far, we have looked at the mechanics of an AOP system. Now we take a look at the fundamental concepts behind AOP.