Aspect Weaver
   HOME

TheInfoList



OR:

An aspect weaver is a
metaprogramming Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse, or transform other programs, and even modi ...
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 communicati ...
. 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 pointcuts and join points, special segments of code that indicate what methods 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 communicati ...
should be added before, after, or throughout the related methods. By doing this, aspect weavers improve
modularity 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 system into varying ...
, keeping code in one place that would otherwise have been interspersed throughout various, unrelated classes.


Motivation

Many
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s are already widely accepted and understood. However, there is no significant 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 t ...
paradigm due to business-related 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 A toolchain is a set of software development tools used to build and otherwise develop software. Often, the tools are executed sequentially and form a pipeline such that the output of one tool is the input for the next. Sometimes the term is us ...
(suite of tools) for development, which is often expensive in both money and time.Kiczales (October 2001), p.7 Primary concerns about roadmaps for the adoption of new technologies include 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 Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
with aspect-oriented programming through minor adaptations such as AspectJ 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++ through the use of an aspect weaver, offering the additional efficiency over AspectJ that is needed for
embedded systems An embedded system is a specialized 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 em ...
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 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.Wand (2004), p.1 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 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 system into varying ...
of cross-cutting concerns.Wand (2004), p.7 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.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 programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming. The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
.Spinczyk (October 2007), p.21


Weaving in AspectJ

In the
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
AspectJ, pointcuts, join points, and the modularized code are defined in an aspect block similar to that of
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
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. 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.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, person, physical countable object (or class thereof), or physical mass ...
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 In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
, 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 (normal ...
, acting both as the aspect weaver and
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
.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 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 A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descr ...
, 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 In computing, a virtual machine (VM) is the virtualization or emulator, emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve ...
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 (normal ...
is needed. 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 switches.


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, 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 a ...
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 needed for locating methods which match the specified pointcuts. One study showed that the AspectJ compiler ajc is about 34% slower than the
Sun Microsystems Sun Microsystems, Inc., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
Java 1.3 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 t ...
*
Preprocessor In computer science, a preprocessor (or precompiler) is a Computer program, program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which i ...
*
Compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...


References


Bibliography

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


Further reading

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