User:DukeAnt/Aspect weaver
语言 | AspectC++, AspectJ |
---|---|
类型 | Aspect-oriented programming |
An aspect weaver is a metaprogramming utility for aspect-oriented languages designed to take instructions specified by aspects (isolated representations of a significant concepts in a program) and generate the final implementation code. 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 either a woven class (which can then be compiled into bytecode) or woven bytecode directly.
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 should be added before, after, or throughout the related methods. By doing this, aspect weavers improve modularity, 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 paradigm is not significant due to business-related concerns; there are risks associated with adopting new technologies.[1] 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.[2] 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.[3]
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 which work with existing tools.[4] 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.[5] The AspectC++ programming language extends C++ through the use of an aspect weaver, offering the additional efficiency over AspectJ that is necessary for embedded systems while still retaining the benefits of aspect-oriented programming.[6]
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.[7]
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 of cross-cutting concerns.[8] Aspects define the implementation code which would have otherwise 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.[9] 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.[10]
aspect Logger {
pointcut method() : execution(* *(..));
before() : method() {
System.out.println("Entering " +
thisJoinPoint.getSignature().toString());
}
after() : method() {
System.out.println("Leaving " +
thisJoinPoint.getSignature().toString());
}
}
public class Foo {
public void bar() {
System.out.println("Executing Foo.bar()");
}
public void baz() {
System.out.println("Executing Foo.baz()");
}
}
|
A sample aspect and class defined in the AspectJ programming language |
public class Foo {
public void bar() {
System.out.println("Entering Foo.bar()");
System.out.println("Executing Foo.bar()");
System.out.println("Leaving Foo.bar()");
}
public void baz() {
System.out.println("Entering Foo.baz()");
System.out.println("Executing Foo.baz()");
System.out.println("Leaving Foo.baz()");
}
}
|
The woven class that results from executing an aspect weaver on the above sample |
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.[11]
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 necessitate duplication of code in the class for every method. Instead, the entry and exit code is defined solely within the aspect.[12]
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.[13]
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.[14][15] While 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.[16] 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%.[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.[18][19] The requirement of a new virtual machine is contrary to one of the original design goals of AspectJ.[5]
To accomplish just-in-time weaving, a change to the virtual machine that executes the compiled bytecode 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.[19] 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.[20] The use of breakpoints in this manner has been shown to reduce performance due to a very large number of context switches.[17]
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.[21] 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 on every execution.[14] 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.[22]
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 compiler and about 62% slower than the Java 1.4 compiler.[23]
See also
References
- ^ Kiczales (October 2001), p.2
- ^ Kiczales (October 2001), p.7
- ^ Colyer (2003), p.6
- ^ Kiczales (October 2001), p.5
- ^ 5.0 5.1 Kiczales (June 2001), p.3
- ^ Spinczyk (2002), p.1
- ^ Wand (2004), p.1
- ^ Wand (2004), p.7
- ^ Viega (November 2000), p.2
- ^ Spinczyk (October 2007), p.21
- ^ Wang (July 2007), p.4
- ^ Avgustinov (2007), p.2
- ^ Hilsdale (2004), pp.5–6
- ^ 14.0 14.1 Hilsdale (2004), p.2
- ^ McEachen (2005), p.1
- ^ Popovici (2003), p.1
- ^ 17.0 17.1 Sato (September 2003), p.17
- ^ Sato (September 2003), p.2
- ^ 19.0 19.1 Papovici (2003), p.3
- ^ Sato (September 2003), p.11
- ^ Gal (2001), p.3
- ^ Colyer (2003), p.2
- ^ Hilsdale (2004), p.7
Bibliography
- Avgustinov, Pavel; Hajiyev, Elnar; Ongkingco, Neil; de More, Oege; Sereni, Damien; Tibble, Julian; Verbaere, Mathieu. Semantics of Static Pointcuts in AspectJ. Proceedings of the 34th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (ACM). 2007: 11–23. ISBN 1-59593-575-4. doi:10.1145/1190216.1190221.
- Colyer, Adrian; Clement, Andy; Bodkin, Ron; Hugunin, Jim. Using AspectJ for Component Integration in Middleware (PDF). Companion of the 18th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications. 2003: 339–344 [23 January 2009]. ISBN 1-58113-751-6. doi:10.1145/949344.949440.
- Gal, Andreas; Schröder-Preikschat, Wolfgang; Spinczyk, Olaf. On Minimal Overhead Operating Systems andAspect-Oriented Programming (PDF). Proceedings of the 4th Workshop on Object-Orientation and Operating Systems at the 15th European Conference on Object-Oriented Programming (ECOOP-OOOSW). 2001 [27 January 2010].
- Hilsdale, Erik; Hugunin, Jim. Advice Weaving in AspectJ (PDF). Proceedings of the 3rd international conference on Aspect-oriented software development (ACM). 2004: 24–35 [23 January 2009]. ISBN 1-58113-842-3. doi:10.1145/976270.976276.
- Kiczales, Gregor; Hilsdale, Erik; Hugunin, Jim; Kersten, Mik; Palm, Jeffrey; Griswold, William. Getting Started with AspectJ. Communications of the ACM (ACM). October 2001, 44 (10): 59–65. doi:10.1145/383845.383858.
- Kiczales, Gregor; Hilsdale, Erik; Hugunin, Jim; Kersten, Mik; Palm, Jeffery; Griswold, William G. An Overview of AspectJ (PDF). Proceedings of the European Conference on Object-Oriented Programming. Lecture Notes in Computer Science. June 2001, 2072: 327–354 [4 January 2010]. ISBN 978-3-540-42206-8. doi:10.1007/3-540-45337-7_18.
- McEachen, Nathan; Alexander, Roger. Distributing Classes with Woven Concerns – An Exploration of Potential Fault Scenarios. Proceedings of the 4th International Conference on Aspect-Oriented Software Development (ACM). 2005: 192–200. ISBN 1-59593-043-4. doi:10.1145/1052898.1052915.
- Popovici, Andrei; Alonso, Gustavo; Gross, Thomas. Just-In-Time Aspects: Efficient Dynamic Weaving for Java. Proceedings of the 2nd International Conference on Aspect-Oriented Software Development (ACM). 2003: 100 109. ISBN 1-58113-660-9. doi:10.1145/643603.643614.
- Sato, Yoshiki; Chiba, Shigeru; Tatsubori, Michiaki. A Selective, Just-In-Time Aspect Weaver (PDF). Proceedings of the 2nd international conference on Generative programming and component engineering. September 2003: 189–208 [4 January 2010].
- Spinczyk, Olaf; Gal, Andreas; Schröder-Preikschat, Wolfgang. AspectC++: An Aspect-Oriented Extension to the C++ Programming Language (PDF). Proceedings of the Fortieth International Conference on Tools Pacific. 2002, 21: 53–60 [4 January 2010].
- Spinczyk, Olaf; Lohmann, Daniel. The design and implementation of AspectC++ (PDF). Knowledge-Based Systems. October 2007, 20 (7): 636–651 [23 January 2010]. doi:10.1016/j.knosys.2007.05.004.
- Viega, John; Voas, Jeffrey. Can Aspect-Oriented Programming lead to More Reliable Software?. IEEE Software. November 2000, 17 (6): 19–21. doi:10.1109/52.895163.
- Wand, Michael; Kiczales, Gregor; Dutchyn, Christopher. A semantics for advice and dynamic join points in aspect-oriented programming (PDF). ACM Transactions on Programming Languages and Systems (TOPLAS) (ACM). 2004: 890–910 [23 January 2009].
- Wang, Yi; Zhao, Jianjun. Specifying Pointcuts in AspectJ (PDF). Proceedings of the 21st Annual International Computer Software and Applications Conference. July 2007, 2: 5–10 [23 January 2010]. ISBN 0-7695-2870-8. doi:10.1109/COMPSAC.2007.196.
Further reading
- Suzuki, Junichi; Yamamoto, Yoshikazu. Moreira, A. M.; Demeyer, Moreira , 编. Extending UML with Aspects: Aspect Support in the Design Phase (PDF). Proceedings of the Workshop on Object-Oriented Technology. June 1999, 1743: 299–300 [4 January 2010].