
IJCSI International Journal of Computer Science Issues, Vol. 9, Issue 5, No 1, September 2012 ISSN (Online): 1694-0814 www.IJCSI.org 212 Aspect-oriented programming with AspectJ Daniela Gotseva1 and Mario Pavlov2 1 Computer Systems Department, Technical University of Sofia Sofia, Bulgaria 2 Computer Systems Department, Technical University of Sofia Sofia, Bulgaria Pointcut: a construct for selecting join points Abstract Advice: a construct to introduce or alter execution This article describes the fundamental concepts of a complete behaviour AOP system. It discusses the AspectJ language specification and Static crosscutting: constructs for altering the implementation. The article examines the use of the language, as static structure of a system well as its features and tooling support. It lays out a common crosscutting problem to illustrate the general syntax of the Aspect: a module to express all corsscutting traditional AspectJ Language. The development tools of the constructs AspectJ language are described and compared to popular Java development tools. 1.2 Historical perspective Keywords: Aspect-oriented Programming, AspectJ, Aspect- oriented Programming System (AOP), AspectJ Development AspectJ is the first complete AOP system. At the time of Tools (ADT), Eclipse IDE. writing this article, AspectJ is also the best and most widely used AOP system. The initial development of AspectJ was the work of a team at Xerox PARC, led by 1. Introduction Gregor Kiczales. He also coined the terms "crosscutting" and "aspect-oriented programming". After a few releases, Xerox donated AspectJ to the free software community at 1.1 Fundamental concepts http://eclipse.org. A few years later another AOP system (AspectWerkz) merged with AspectJ, adding features, such An aspect-oriented programming system (AOP) is a as annotation based syntax. At the moment, AspectJ has an software system that is a realization of the aspect-oriented alternative implementation (AspectBench), used for programming methodology. In general, a programming experimenting with new features and optimizations. methodology realization consists of two parts: Language specification: an explicit definition of the syntax and semantics of the language 2. AspectJ as an AOP system The AOP language specification defines how programming constructs are expressed. An AOP language must include AspectJ is an extension to the Java programming language constructs for describing data and behaviour, along with that adds AOP capabilities to Java. The AspectJ constructs that describe how to combine data and behavior. implementation consists of the following components [2]: Language reference implementation: a software A compiler (ajc) application that can translate code written in the A debugger (ajdb) language into an executable form. A documentation generator (ajdoc) The AOP language implementation performs the steps A program structure browser (ajbrowser) necessary to convert the source code into executable code Integration with Eclipse, Sun-ONE/Netbeans, according to the language rules. This process is commonly GNU Emacs/XEmacs, JBuilder, and Ant. referred to as compilation. The AspectJ compiler (ajc) is often called a weaver. The A complete AOP system supports the following name originated because of its primary role to weave the fundamental concepts [1, 4-6]: AspectJ extensions into the Java code, that is to weave Join points: identifiable points in the execution of aspects into classes and produce the final executable code. a system AspectJ provides three weaving mechanisms: Copyright (c) 2012 International Journal of Computer Science Issues. All Rights Reserved. IJCSI International Journal of Computer Science Issues, Vol. 9, Issue 5, No 1, September 2012 ISSN (Online): 1694-0814 www.IJCSI.org 213 Source weaving: The source code for aspects and The AspectJ documentation tool (ajdoc) is very similar to classes is fed to the AspectJ compiler/weaver the Java documentation tool (javadoc). It produces similar which produces the final executable code. This output, but also provides additional information, such as mechanism is useful when the entire source code advice affecting a particular method or all code affected by (aspects and classes) is available. It enables both a given aspect, allowing developers to examine easily the the compilation and weaving to be done in one go crosscutting structure of a system. and highly simplifies the building process. Binary weaving: The input to the weaver is in byte-code form, that is classes and aspects are compiled 3. AOP with AspectJ beforehand. The idea is to compile the classes and aspects without weaving and then weave the resulting binary file. In other words, this is a three-step process (see Fig.h 1): 3.1 Implementing a cache 1. Compile classes To describe and illustrate an AOP with AspectJ, we are 2. Compile aspects going to discuss a rather common and wide-spread 3. Weave aspects into classes to produce problem: implementing a cache. A cache is a classical the final binary files crosscutting concern. With traditional object-oriented programming, the caching concern cannot be completely The binary weaving mechanism is very flexible and isolated or separated from the main concern. You can work enables weaving even when some or all of the Java source around this problem to a certain extent leveraging design code is not available, for example the source code is part patterns such as proxy and decorator, but it cannot be of a third party library or for some reason cannot be completely eliminated. compiled alongside the AspectJ code. 2.1 Load-time weaving AOP provides a complete and elegant solution to such a problem. A very simple illustration is shown in code listing A load-time weaver takes compiled classes and aspects, as 1 (Note that this code listing shows three files: well as an XML configuration. The load-time weaver can FileSystem.java, FileSystemCache.java and take various forms, such as JVMTI agent or a classloader. FileSystemCacheAspect.aj): The load-time weaver weaves aspects into classes as they are loaded into the system prior to first use. The XML file Code listing 1: File System Interface. (aop.xml) specifies the weaving configuration. The public interface FileSystem { aop.xml can be placed under the META-INF directory and Collection<File> list(File entry); defined in the MANIFEST.MF as a classpath component. Load-time weaving greatly reduces the risk of adopting byte[] getContents(File entry); and experimenting with AOP because it makes switching } AspectJ on and off just a matter of configuration. public interface FileSystemCache { byte[] getContents(File entry); void putContents(File entry, byte[] contents); } 1 public aspect FileSystemCacheAspect { 2 3 private FileSystemCache cache = new FileSystemCacheImpl(); 4 5 pointcut fileSystemAccess(File entry) : execution(public * FileSystem+.get*(File)) && args(entry); 6 7 byte[] around(File entry) : fileSystemAccess(entry) { 8 byte[] contents = cache.getContents(entry); Fig. 1 Three-step process. 9 if(null == contents) { 10 contents = proceed(entry); Copyright (c) 2012 International Journal of Computer Science Issues. All Rights Reserved. IJCSI International Journal of Computer Science Issues, Vol. 9, Issue 5, No 1, September 2012 ISSN (Online): 1694-0814 www.IJCSI.org 214 11 cache.putContents(entry, contents); Table 1: Pointcut Construct Structures. 12 } 13 return contents; Pointcut construct Used to select... 14 } 15 } call(MethodPattern or calls to the methods or ConstructorPattern) constructors execution(MethodPattern executions of methods or or ConstructorPattern) constructors The example shows the traditional AspectJ syntax. The get(FieldPattern) field read access FileSystemCacheAspect contains one pointcut and one advice that are defined on lines 5 and 7 respectively. Each set(FieldPattern) field write access pointcut definition is of the form: handler(TypePattern) catch blocks pointcut pointcutIdentifier : joinPointSelectionCriteria; within(TypePattern) executions of code defined in classes Each advice definition is of the form withincode(MethodPattern executions of code defined in or ConstructorPattern) methods or constructors [ strictfp ] AdviceSpec [ throws typeList ] : initialization(ConstructorP object initializations pointcutIdentifier { body } attern) preinitialization(Construct object pre-initializations where AdviceSpec is one of orPattern) before(formals) staticinitialization(TypePat static initializer executions after(formals) returning [(formal)] tern) after(formals) throwing [(formal)] adviceexecution() all advice executions after(formals) cflow(Pointcut) join points in the control flow of Type around(formals) any join point P picked out by Pointcut, including P itself cflowbelow(Pointcut) join points in the control flow of and where formal refers to a variable binding like those any join point P picked out by used for method parameters, and formals refers to a Pointcut, but not P itself comma-delimited list of formal.[2] this(Type) join points where the currently executing object is an instance of The following line: Type pointcut fileSystemAccess(File entry) : execution(public * target(Type) join points where the target object (the object on which a call FileSystem+.get*(File)) && args(entry); or field operation is applied to) is creates a pointcut with the name fileSystemAccess. The an instance of Type part after the colon is the join point selection criteria. In args(Type) join points where the arguments this case, it will match on execution
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-