ITWissen.info - Tech know how online

AspectJ

AspectJ is an extension of the Javaprogramming language with special constructs for aspect-oriented programming (AOP). AspectJ was already developed in 1996 by a team at the Xerox Palo Alto Research Center - significantly influenced by Georg Kiczales. Since 2002 AspectJ is also part of the Eclipse project, which provides up-to-date development tools. With the introduction of AOP, Java is only extended by a specific classAspect, which encapsulates the AOP-specific methods. The functional Java program code is interwoven with an Aspect at the previously defined interweaving points. In addition to the Java Runtime Environment (JRE), a corresponding compiler - also called Weaver - and the AspectJ Runtime Library are required to execute AspectJ program code. An Aspect is thus already present when a program is started, since it has been woven into the existing code by the Weaver, and is thus a physical part of the application and therefore does not need to be started separately.

AspectJ was the first aspect-oriented programming language, and since it is also based on the widespread programming language Java, AspectJ also has a wide distribution as well as a high acceptance.

For a comprehensible explanation of AspectJ, the following terms must first be explained.

Aspect Aspects implement so-called Crosscutting Concerns - also called crosscutting functionalities. This refers to general functionality such as logging, authentication, etc.

Join Point A join point is the point within a software where the AspectJ code is supposed to join the program flow. Join points can be executions of methods, the execution as well as the call of a constructor or the reference to data structures (variables).

Point Cut A point cut is a set of join points - also called interconnection points. This set can be empty or contain one, several or all join points.

Advice This is a term specific to AspectJ and defines what is implemented at the join points of a point cut and when. Advices consist of normal Java program code. For the time of insertion of the AspectJ code can be defined: before, after and around.

Inter Type Declaration With the use of AOP, dynamic extension of classes is possible, which is called Inter Type Declaration.

Aspects, Point Cuts and Join Point

Aspects, Point Cuts and Join Point

The figure illustrates the relationship between aspects, point cuts and join points. There, the join point is a fixed location in the program flow that coordinates aspect and non-aspect oriented code.

Aspect-oriented programming extends the existing Java classes by a single special class Aspect, which then encapsulates its own data structures and methods. A central element here are the point cuts, which can be built up from the above-mentioned methods, among others, or can be formed by a number of further selection methods. AspectJ intervenes in the structure of the classes of an application at runtime, without the application specifically knowing about the existence of the aspect class. Since AspectJ allows, among other things, the use of wildcards with regard to the selection of classes, methods, etc. by pattern matching, the aspect does not need to know anything about the class to which it ultimately accesses. The implementation of an aspect - the weaving of an aspect into the Java program code - is then done either at compile time or at runtime, which then affects the performance of an application.

AspectJ supports generic and parameterized types in pointcuts, intertype declarations, and inheritance. Since version 5 of AspectJ, features such as annotations, generics, autoboxing, and unboxing can be used. Annotations can mark aspects and within aspects methods, fields, constructors, advices etc. with metadata. Annotations can also be specified in pointcuts as additional conditions for interweaving points.

Representing the Notations of AspectJ

Representing the Notations of AspectJ

The following is an example of representing the notation of AspectJ. The aspect Trace defines a pointcut for calling all methods with add with arbitrary arguments as well as arbitrary return value. In addition, Trace defines two advices.

For working with AspectJ the following components are basically combined in one package:

  • The AspectJ compiler (weaver) ajc.
  • The AspectJ interpreter aj.
  • The AspectJ Runtime Library ASPECTJRT_LIB.
The weaving of a Java class with an aspect is done statically based on the source code using the compiler ajc. To do this, ajc first compiles the Java classes and the aspects and then interweaves them based on the bytecode generated. In order to then use the application created in this way, the well-known Java Runtime Environment (JRE) is then sufficient.

The advantages of AspectJ include a better modularization of the application, the possibility of code reuse and it can be used very flexibly, since it is possible to switch quickly and without loss of performance between Java and AspectJ code.

These are countered by the following disadvantages: The definition of point cuts requires an unrestricted knowledge of the application classes, AspectJ code is not automatically restructurable, the creation of a point cut with associated advice is often more time-consuming than direct Java code, the use of AspectJ requires a special development environment for more complex projects, and the syntax of AspectJ sometimes deviates considerably from Java syntax.

AspectJ is subject to constant further development and is supported by a number of development environments such as Emacs, JBuilder and Netbeans. Also, of course, by Eclipse, which runs both an AspectJ project and a separate AspectJ Development Tools project. The latter develops plugins for AspectJ Eclipse. By integrating it as a plugin into the Eclipse development environment, a special editor for AspectJ is available, as well as a number of other features:

  • Automatic generation of AspectJ documentation.
  • The interconnection points are displayed in the source code.
  • Aspects can be configured controlled by a wizard.
  • Build files can be edited and managed.
  • Debugging can be done graphically.
  • The source code is automatically completed.
Another implementation of AspectJ is the AspectBench compiler (also called abc), which is both a reference compiler to ajc and a complementary framework for implementing extensions to AspectJ. In this regard, abc also aims to optimize the language core of AspectJ. In this respect, abc is a university project from the USA, Canada and Denmark. For this purpose, abc builds on the two independent frameworks Polyglot and Soot. In addition, the complete runtime library was taken from ajc. A major difference is that compiling aspects are not supported separately.

Informationen zum Artikel
Englisch: AspectJ
Updated at: 14.04.2010
#Words: 1719
Links: