Aspect weavers operate by taking instructions specified by
aspects, known as
advice, and automatically distributing it throughout the various
classes in the program. The result of the weaving process is a set of classes with the same names as the original classes but with additional code automatically injected into the classes'
functions. The advice specifies the exact location and functionality of the injected code. Through this weaving process, aspect weavers allow for code which otherwise would have been duplicated across classes. By eliminating this duplication, aspect weavers promote
modularity of
cross-cutting concerns. Aspects define the implementation code which otherwise would have been duplicated and then use
pointcuts and
join points to define the advice. During weaving, the aspect weaver uses the pointcuts and join points, known as a
pointcut designator, to identify the positions in candidate classes at which the implementation should be injected. The implementation is then injected into the classes at the points identified, thus permitting the code to be executed at the appropriate times without relying on manual duplication by the
programmer.
Weaving in AspectJ In the
programming language AspectJ, pointcuts, join points, and the modularized code are defined in an aspect block similar to that of
Java classes. Classes are defined using Java syntax. The weaving process consists of executing the aspect advice to produce only a set of generated classes that have the aspect implementation code woven into it. The example at right shows a potential implementation of an aspect which logs the entry and exit of all
methods. Without an aspect weaver, this feature would require
duplication of code in the class for every method. Instead, the entry and exit code is defined solely within the aspect. The aspect weaver analyzes the advice specified by the pointcut in the aspect and uses that advice to distribute the implementation code into the defined class. The code differs slightly in each method due to slight variances in requirements for the method (as the method
identifier has changed). The aspect weaver determines the appropriate code to generate in each situation as defined by the implementation advice and then injects it into methods matching the specified pointcut.
Weaving to bytecode Instead of generating a set of woven
source code, some AspectJ weavers instead weave the aspects and
classes together directly into
bytecode, acting both as the aspect weaver and
compiler. It is expected that the performance of aspect weavers which also perform the compilation process will require more computation time due to the weaving process involved. However, the bytecode weaving process produces more efficient runtime code than would usually be achieved through compiled woven source.
Run-time weaving Developments in AspectJ have revealed the potential to incorporate
just-in-time compilation into the execution of aspect-oriented code to address performance demands. At
run-time, an aspect weaver could translate aspects in a more efficient manner than traditional, static weaving approaches. Using AspectJ on a
Java Virtual Machine, dynamic weaving of aspects at run-time has been shown to improve code performance by 26%. While some implementations of just-in-time virtual machines implement this capability through a new virtual machine, some implementations can be designed to use features that already exist in current virtual machines. An alternative implementation uses a weaving engine that uses
breakpoints to halt execution at the pointcut, select an appropriate method, embed it into the application, and continue. The use of breakpoints in this manner has been shown to reduce performance due to a very large number of
context switches. ==Performance==