Section 23.1. ASPECTJ


23.1. ASPECTJ

AspectJ consists of a slightly modified form of Java, called JCore, for expressing the core functionality of a program, plus a set of aspect languages. In version 0.1 of AspectJ, two aspect languages are supported: Cool for expressing synchronization concerns, and Ridl for expressing remote data transfer and method invocation concerns.

JCore is essentially identical to Java, save that the keyword synchronized and the wait, notify, and notifyAll methods have been removed to ensure appropriate separation of synchronization concerns.

Cool encapsulates the synchronization aspect of AspectJ programs. A snippet of code called a coordinator is written when coordination is desired for a particular class or set of classes. This coordination can be described on a per-instance or a per-class basis. Each coordinator describes the synchronization on and between methods of the class or classes it coordinates via three constructs: selfex, mutex, and guards. A selfex specification on a method means that only one thread can concurrently execute that method. A mutex specification on two or more methods means that if one thread were executing one of these methods, no other thread could concurrently execute any of the other methods. A mutex specification does not imply a selfex specification. Finally, the guard construct is provided for more complicated, potentially dynamically changing synchronization relationships; they permit the specification and enforcement of essentially arbitrary pre- and post-conditions on the execution of a method. A given method is permitted to simultaneously have a selfex specification and multiple mutex and guard specifications. Version 0.1 limits each class to a single coordinator.

Ridl allows the specification of remote interfaces for classes and the data transfer behavior to be used when these remote interfaces are invoked. Only methods specified in the remote interface can be invoked remotely. The remote interface specifies how the input parameters and return value for each method should be transferred over a network: by passing a copy of the remote object, or by passing a global reference to the remote object. Furthermore, the fields of any of these objects can be individually specified as being passed by copy, passed by reference, or skipped altogether. The rationale behind such specifications is that the implementor of the remote interface has knowledge of the way these objects are being used, and would therefore know whether it were costlier to make multiple communications to a remote host, or to copy an object all at once. Ridl version 0.1 is built on top of Java's Remote Method Invocation (RMI) protocol.

Each JCore class, Cool coordinator, and Ridl remote interface specification must reside in a separate file. At compile time, a tool called an aspect weaver combines these separate specifications into a set of Java classes, which are then compiled to produce executable bytecodes. A tool similar to make performs weaving and compilation only on those classes that are impacted by changes to the source files.

Figure 23-1 shows a portion of a class and its attendant coordinator and remote interface that were used in our studies. Since addBook() alters bookCount while numBooks() returns its value, the two methods should not be called concurrently. The coordinator Query.cool contains a mutex specification to ensure this condition. The remote interface Query.ridl indicates that the book to be added to the query should be passed by copy, while the library from which it came should be passed by reference.

Figure 23-1. Snippets of AspectJ code.
 Query.jcore public class Query {   Hashtable books;   int bookCount = 0;   public void addBook( Book b, Library source ) {    if( !books.containsKey( b ) ) {      books.put( b, source );      bookCount++;    }   }   public long numBooks() {     return bookCount;   } } Query.cool coordinator Query {   mutex{ addBook, numBooks }; } Query.ridl remote Query {   void addBook( Book b, Library source ) {     b: copy;     source: gref;   } } 



Aspect-Oriented Software Development
Aspect-Oriented Software Development with Use Cases
ISBN: 0321268881
EAN: 2147483647
Year: 2003
Pages: 307

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net