00 Lieberman TOM 9/01 lo - Semantic Scholar

33 downloads 10567 Views 108KB Size Report
method on one of the classes, or divide the operation into methods on each of the .... Professor of Computer Science at Northeastern. University in Boston, MA.
ASPECT-ORIENTED PROGRAMMING WITH

ADAPTIVE METHODS Java programmers can easily experiment with AOP ideas by using the DJ library. An operation in an object-oriented program often involves several

A

different collaborating classes. There are usually two ways to implement such an operation: either put the whole operation into one method on one of the classes, or divide the operation into methods on each of the classes involved. The drawback of the former is that too much information about the structure of

Karl Lieberherr, Doug Orleans, and Johan Ovlinger

the classes (is-a and has-a relationships) needs to be tangled into each such method, making it difficult to adapt to changes in the class structure. However, the latter scatters the operation across multiple classes, making it difficult to adapt when the operation changes. To resolve this conflict, the Demeter project has developed the notion of an adaptive method (known in previous work as a propagation pattern [3]). An adaptive method encapsulates the behavior of COMMUNICATIONS OF THE ACM October 2001/Vol. 44, No. 10

39

an operation into one place, thus avoiding the scattering problem, but also abstracts over the class structure, thus avoiding the tangling problem as well. To do this, the behavior is expressed as a high-level description of how to reach the participants of the computation (called a traversal strategy [4]), plus what to do when each participant has been reached (called an adaptive visitor). Both the traversal strategy and the adaptive visitor mention only a minimal set of classes that participate in the operation; the information about the connections between these classes is abstracted out. In Java, reflection provides us with an elegant way to achieve this abstraction: the class structure can be accessed directly at runtime. The DJ—for Demeter/Java—library is a Java A simple adaptive method. import edu.neu.ccs.demeter.dj.ClassGraph; import edu.neu.ccs.demeter.dj.Visitor; class Company { static ClassGraph cg = new ClassGraph () ; // class structure Double sumSalaries () { String s = “from Company to Salary” ; // traversal strategy Visitor v = new Visitor () { // adaptive visitor private double sum; public void start () { sum = 0.0 }; public void before (Salary host) { sum += host. getValue() ; } public Object getReturnValue () { return new Double (sum) ; } }; return (Double) cg. traverse (this, s, v) ; } // . . . rest of Company definition . . . }

package that supports this style of programming by providing tools to interpret a traversal strategy and an adaptive visitor in the context of the underlying class structure.

A Simple Example The figure appearing here shows a small adaptive method written in Java using the DJ library. The purpose of the code is to sum the values of all the Salary objects reachable by has-a relationships from a Company object. It works as follows: the static variable cg is a class graph object that represents the program’s class structure. A class graph is a simple form of a UML class diagram that describes is-a and has-a relationships between classes [1]. The class structure is computed in ClassGraph’s constructor using reflection. It is used by the traverse method as a context in which to interpret traversal strategies. The traverse method starts in a given object (this in the example here) and traverses specified paths (“from Company to Salary”) executing any applicable visitor methods along the way (in this situation upon starting, to initialize the sum; upon reaching each Salary 40

October 2001/Vol. 44, No. 10 COMMUNICATIONS OF THE ACM

object, to add the value to the sum; and upon finishing, to build the return value). The separation of traversal strategy (where to go), adaptive visitor (what to do), and class graph (context to evaluate in) allows the method to be reused unchanged in a set of programs. Details such as the number of classes between Company and Salary are unimportant: it could be a company with a big organizational structure (divisions, departments, work groups, and so forth) with many classes between or a company with a small organizational structure (only work groups) with few classes between.

Connection to Aspects and AspectJ AspectJ [2] makes the following key definitions: Join points are principled points in the execution of the program. Pointcuts are a means of referring to collections of join points and certain values at those join points. Advice is a method-like construct that can be attached to pointcuts; and aspects are modular units of crosscutting implementation, comprised of pointcuts, advice, and ordinary Java member declarations. The adaptive method sumSalaries is an aspect in the terminology of AspectJ: it is a modular implementation of a crosscutting concern. It contains a definition of a collection of join points and also contains a definition of advice for those join points. The join points are defined by a traversal strategy and the advice is defined by an adaptive visitor. The join points are defined in an unusual way: instead of referring to points in the execution of a program given to us, we first define a traversal in terms of a class graph given to us and then we consider the join points defined by this traversal. In other words, we use a version of AOP where the pointcut definitions indirectly define a collection of join points based on information in the class graph. The advice is also defined in an unusual way: in the Java method sumSalaries(...) different join points receive different advice while in AspectJ all join points in a pointcut get the same advice. With DJ, some points in a pointcut get no advice at all if they are not mentioned in the adaptive visitor. Conclusion We have highlighted how a family of crosscutting behavioral concerns can be implemented in a modularized way in pure Java using the DJ library. The modular units—known as adaptive methods— cleanly encapsulate behavior that would normally be tangled with information about the class structure or scattered across multiple classes. The behavior of the methods involves groups of participating objects, all

of which can be reached from given source objects. Johan Ovlinger ([email protected]) is a Ph.D. student at We have also briefly explained the core concepts of Northeastern University in Boston and a researcher with SKYVA International in Medford, MA. adaptive methods in the aspectual terminology of AspectJ here. Research supported by Defense Advanced Research Projects Agency under agreement The DJ library permits Java programmers to fol- F33615-00-C-1694. low the Law of Demeter in an optimal way and to Permission to make digital or hard copies of all or part of this work for personal or classexperiment with aspect-oriented ideas without hav- room use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on ing to learn an extension to Java or to preprocess the first page. To copy otherwise, to republish, to post on servers or to redistribute to source files (this is an issue with off-the-shelf inte- lists, requires prior specific permission and/or a fee. grated development environments). This ease of use comes at a price: only a limited class of behavioral concerns can be expressed as adaptive methods. However, in our experience this class of behavior © 2001 ACM 0002-0782/01/1000 $5.00 covers a large portion of real-world programming tasks, rendering the restriction not as onerous as it iniDean of Computer and tially might seem. Lastly, the curCommunication Sciences rent naive implementation relies heavily on reflection, incurring at the Swiss Federal Institute of noticeable slowdowns. A more Technology Lausanne (EPFL) sophisticated implementation is in progress. The EPFL is a growing, dynamic, well- EPFL invites applications for the posiThe DJ library is available in funded university developing research tion of the «Dean of Computer and source and bytecode form at programs in engineering, basic and life Communication Sciences». The ideal www.ccs.neu.edu/research/demeter/DJ. sciences. In the context of a campus- candidate has an outstanding academic A more detailed discussion of DJ, wide reorganization its current depart- record and a strong vision for research, showing how to use adaptive proments of Computer Science and teaching and industrial developments in gramming methods to process XML Communication Systems will be mer- the field. schemas, is available at www.ccs. ged into «Computer and Communicaneu.edu/research/demeter/papers/ c DJ-reflection/. tion Sciences». This field has a high Applications including CV, publication References 1. Booch, G., Rumbaugh, J., and Jacobson, I. The Unified Modeling Language User Guide. Addison Wesley, Reading, MA, 1999. 2. Kiczales, G., Hilsdale, E., Hugunin, J., Kersten, M., Palm, J. and Griswold, W. An overview of AspectJ. In J. Knudsen, Ed., Proceedings of the European Conference on Object-Oriented Programming, (Budapest, 2001), Springer Verlag. 3. Lieberherr, K.J. Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. PWS Publishing Company, Boston, 1996; www.ccs.neu.edu/research/demeter. 4. Lieberherr, K.J. and Patt-Shamir, B. Traversals of Object Structures: Specification and Efficient Implementation. Technical Report NU-CCS97-15, College of Computer Science, Northeastern University, Boston, MA, Sept. 1997; www.ccs.neu.edu/research/demeter/ papers/publications.html.

Karl Lieberherr ([email protected]) is Professor of Computer Science at Northeastern University in Boston, MA. Doug Orleans ([email protected]) is a Ph.D. student at Northeastern University in Boston, MA.

priority within EPFL and it is expanding substantially its student body, faculty and research funding. A National Competence Center of Research, funded by the Swiss National Science Foundation, is being created in the field of mobile communications and information systems with EPFL as a leading house. To lead the new combined faculty,

list, addresses for references, and a vision statement should be sent as soon as possible, but not later than Jan. 10, 2002 to the address below: Prof. Martin Hasler Chairman of the Search Committee Department of Communication Systems Swiss Federal Institute of Technology CH-1015 Lausanne, Switzerland Phone +41.21.693.26.22 e-mail: [email protected] More information on EPFL, the two departments and the National Competence Center of Research can be found at http://www.epfl.ch, http://diwww.epfl.ch, http://dscwww.epfl.ch and http://www.terminodes.org, respectively.

COMMUNICATIONS OF THE ACM October 2001/Vol. 44, No. 10

41