HOME

TheInfoList



OR:

An aspect weaver is a metaprogramming utility for aspect-oriented languages designed to take instructions specified by aspects (isolated representations of significant concepts in a program) and generate the final implementation
code In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communication ...
. The weaver integrates aspects into the locations specified by the software as a pre- compilation step. By merging aspects and classes (representations of the structure of entities in the program), the weaver generates a woven class. Aspect weavers take instructions known as '' advice'' specified through the use of
pointcut In aspect-oriented programming, a pointcut is a set of join points. Pointcut specifies where exactly to apply advice, which allows separation of concerns and helps in modularizing business logic. Pointcuts are often specified using class names or ...
s and
join point In computer science, a join point is a point in the control flow of a program where the control flow can arrive via two different paths. In particular, it's a basic block that has more than one predecessor. In aspect-oriented programming a set o ...
s, special segments of code that indicate what
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
should be handled by aspect code. The implementation of the aspect then specifies whether the related
code In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communication ...
should be added before, after, or throughout the related methods. By doing this, aspect weavers improve
modularity Broadly speaking, modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use. The concept of modularity is used primarily to reduce complexity by breaking a sy ...
, keeping code in one place that would otherwise have been interspersed throughout various, unrelated classes.


Motivation

Many programming languages are already widely accepted and understood. However, the desire to create radically different programming languages to support the
aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying ...
paradigm is not significant due to business-related concerns; there are risks associated with adopting new technologies.Kiczales (October 2001), p.2 Use of an entirely new language relies on a business's ability to acquire new developers. Additionally, the existing code base of a business would need to be discarded. Finally, a business would need to acquire a new toolchain (suite of tools) for development, which is often both an expense in both money and time.Kiczales (October 2001), p.7 Primary concerns about roadmaps for the adoption of new technologies tend to be the need to train new developers and adapt existing processes to the new technology.Colyer (2003), p.6 To address these business concerns, an aspect weaver enables the use of widely adopted languages like Java with aspect-oriented programming through minor adaptations such as
AspectJ AspectJ is an aspect-oriented programming (AOP) extension created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become a widely use ...
which work with existing tools.Kiczales (October 2001), p.5 Instead of developing an entirely new language, the aspect weaver interprets the extensions defined by AspectJ and builds "woven" Java code which can then be used by any existing Java compiler. This ensures that any existing object oriented code will still be valid aspect-oriented code and that development will feel like a natural extension of the object-oriented language.Kiczales (June 2001), p.3 The AspectC++ programming language extends
C++ C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
through the use of an aspect weaver, offering the additional efficiency over
AspectJ AspectJ is an aspect-oriented programming (AOP) extension created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become a widely use ...
that is necessary for
embedded systems An embedded system is a computer system—a combination of a computer processor, computer memory, and input/output peripheral devices—that has a dedicated function within a larger mechanical or electronic system. It is ''embedded'' a ...
while still retaining the benefits of aspect-oriented programming.Spinczyk (2002), p.1


Implementation

Aspect weavers operate by taking instructions specified by aspects, known as ''advice'', and distributing it throughout the various classes in the program automatically. The result of the weaving process is a set of classes with the same names as the original classes but with additional code injected into the classes' functions automatically. The advice specifies the exact location and functionality of the injected code.Wand (2004), p.1 Through this weaving process, aspect weavers allow for code which would have otherwise been duplicated across classes. By eliminating this duplication, aspect weavers promote
modularity Broadly speaking, modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use. The concept of modularity is used primarily to reduce complexity by breaking a sy ...
of
cross-cutting concern In aspect-oriented software development, cross-cutting concerns are aspects of a program that affect several modules, without the possibility of being encapsulated in any of them. These concerns often cannot be cleanly decomposed from the rest ...
s.Wand (2004), p.7 Aspects define the implementation code which would have otherwise been duplicated and then use
pointcut In aspect-oriented programming, a pointcut is a set of join points. Pointcut specifies where exactly to apply advice, which allows separation of concerns and helps in modularizing business logic. Pointcuts are often specified using class names or ...
s and
join point In computer science, a join point is a point in the control flow of a program where the control flow can arrive via two different paths. In particular, it's a basic block that has more than one predecessor. In aspect-oriented programming a set o ...
s 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.Viega (November 2000), p.2 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 A computer programmer, sometimes referred to as a software developer, a software engineer, a programmer or a coder, is a person who creates computer programs — often for larger computer software. A programmer is someone who writes/creates ...
.Spinczyk (October 2007), p.21


Weaving in AspectJ

In the programming language
AspectJ AspectJ is an aspect-oriented programming (AOP) extension created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become a widely use ...
, 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.Wang (July 2007), p.4 The example at right shows a potential implementation of an aspect which logs the entry and exit of all
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
. Without an aspect weaver, this feature would necessitate duplication of code in the class for every method. Instead, the entry and exit code is defined solely within the aspect.Avgustinov (2007), p.2 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 An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
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.Hilsdale (2004), pp.5–6


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 Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normall ...
, acting both as the aspect weaver and
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs th ...
.Hilsdale (2004), p.2McEachen (2005), p.1 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 In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is a way of executing computer code that involves compilation during execution of a program (at run time) rather than before execution. This may co ...
into the execution of aspect-oriented code to address performance demands.Popovici (2003), p.1 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%.Sato (September 2003), p.17 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.Sato (September 2003), p.2 The requirement of a new virtual machine is contrary to one of the original design goals of AspectJ. To accomplish just-in-time weaving, a change to the virtual machine that executes the compiled
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normall ...
is necessary. A proposed solution for AspectJ uses a layered approach which builds upon the existing Java Virtual Machine to add support for join point management and callbacks to a ''Dynamic Aspect-Oriented Programming Engine''.Papovici (2003), p.3 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.Sato (September 2003), p.11 The use of breakpoints in this manner has been shown to reduce performance due to a very large number of
context switch In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes ...
es.


Performance

Aspect weavers' performance, as well as the performance of the code that they produce, has been a subject of analysis. It is preferable that the improvement in modularity supplied by aspect weaving does not impact run-time performance. Aspect weavers are able to perform aspect-specific optimizations.Gal (2001), p.3 While traditional optimizations such as the elimination of unused special variables from aspect code can be done at
compile-time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
, some optimizations can only be performed by the aspect weaver. For example, AspectJ contains two similar but distinct keywords, thisJoinPoint, which contains information about this particular instance of woven code, and thisJoinPointStaticPart, which contains information common to all instances of code relevant to that set of advice. The optimization of replacing thisJoinPoint with the more efficient and static keyword thisJoinPointStaticPart can only be done by the aspect weaver. By performing this replacement, the woven program avoids the creation of a join point
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
on every execution. Studies have shown that the unnecessary creation of join point objects in AspectJ can lead to a performance overhead of 5% at run-time, while performance degradation is only approximately 1% when this object is not created.Colyer (2003), p.2 Compile-time performance is generally worse in aspect weavers than their traditional compiler counterparts due to the additional work necessary for locating methods which match the specified pointcuts. A study done showed that the AspectJ compiler ajc is about 34% slower than the Sun Microsystems
Java 1.3 The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community ...
compiler and about 62% slower than the Java 1.4 compiler.Hilsdale (2004), p.7


See also

*
Aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying ...
* Preprocessor *
Compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs th ...


References


Bibliography

* * * * * * * * * * * * * *


Further reading

* {{DEFAULTSORT:Aspect Weaver Aspect-oriented programming Aspect-oriented software development Compiler construction Programming language implementation Articles with example Java code