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 the AOP, Java is only extended by a specific class Aspect, which encapsulates the AOP-specific methods. The functional Java program code is interwoven with an Aspect at the previously defined interweaving points. For the execution of AspectJ program code, a corresponding compiler - also called Weaver - and the AspectJ Runtime Library are required in addition to the Java Runtime Environment( JRE). 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 based besides still on the far common programming language Java, also AspectJ possesses likewise a wide spreading as well as a high acceptance.

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

AspectAspects implement so-called crosscutting concerns - also called crosscutting functionalities. This refers to general functionality such as logging, authentication, etc.

Join Point Join point refers to the point within a software where the AspectJ code is 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 specific term of AspectJ and defines what is implemented at which time at the join points of a point cut. 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 application of the AOP a 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 illustration clarifies the relationship between aspects, point cuts and join points. There, the Join Point is a fixed point 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 are the Point Cuts, which can be built up among other things from the methods mentioned above and/or be formed still by a set 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 permits among other things the use of Wildcards regarding the selection of the classes, methods among other things by pattern matching, also the aspect does not have to know anything from the class, on which it finally accesses. The implementation of an aspect - the weaving of an aspect into the Java program code - then takes place either with the compilation or at runtime, which then has an impact on 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 weaving points.

Representation of the notations of AspectJ

Representation of the notations of AspectJ

The following is an example of how the notation of AspectJ is represented. 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 the work with AspectJ the following components are in principle united in a 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 with the help of the compiler ajc. For this purpose, ajc first compiles the Java classes and the aspects and then interweaves them based on the byte code generated in this way. In order to use then the application developed in such a way, the well-known Java Runtime Environment (JRE) is sufficient.

Among the advantages of AspectJ are a better modularization of the application, the possibility of the reuse of code and it can be used very flexibly, since fast and without losses of performance between Java and AspectJ code can be switched.

These are offset by the following disadvantages: The definition of Point Cuts presupposes an unrestricted knowledge of the application classes, AspectJ code is not automatically restructurable, the production of a Point Cut with associated Advice is frequently more complex than direct Java code, the use of AspectJ presupposes a special development environment with more complex projects and the syntax of AspectJ deviates partially substantially from the 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 context, 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.

Informations:
Englisch: AspectJ
Updated at: 14.04.2010
#Words: 1069
Links: extension (EXTN), aspect, acoustic overload point (automotive) (AOP), Eclipse, project
Translations: DE
Sharing:    

All rights reserved DATACOM Buchverlag GmbH © 2024