Aspect Oriented Programming Languages Seminar Presenter Bar Aktemur
Aspect Oriented Programming Languages Seminar Presenter: Barış Aktemur University of Illinois 18 Feb. 2004 Mostly taken from Bedir Tekinerdogan’s slides
Outline n n Introduction Problems Terminology Aspect-Oriented Programming Languages/Frameworks n n n Compositional Filters Aspect. J Hyper/J Demeter. J Conclusions 2
Introduction n Evolution of Programming Languages n n n n Assembly/Machine Languages Formula Translation Procedural Programming Structured Programming Functional Programming Logic Programming with abstract data types Evolution of Software Design n Monolithic ---> Modular 3
Design Principles Modularity n Abstraction n n Decomposition n n Group related things together. Information Hiding n n Divide software into separately named and addressable modules Encapsulation n n Focus only on relevant properties Hide implementation details from the outside Separation of Concerns n n Ensure that each module only deals with one concern Low Coupling n n aim for low coupling among the modules High Cohesion n aim for high cohesion within one module 4
Separation of Concerns Cohesion n Maximize cohesion within a component n n i. e. Cohesive component performs only one concern/task required changes can be easily localized and will not propagate Coupling n n Highly coupled components have many dependencies/interactions Minimize coupling between components n n reduces complexity of interactions reduces ‘ripple’ effect 5
Advantages of separation of concerns n n n Understandability Maintainability Extensibility Reusability Adaptability Separation of Concerns directly supports quality factors. Lack of Separation of Concerns negatively impacts quality factors. 6
Example - Figure Editor n A figure consists of several figure elements. A figure element is either a point or a line. Figures are drawn on Display. A point includes X and Y coordinates. A line is defined as two points. 7
Example - Figure Editor - Design Display * Figure. Element Figure Point get. X() 2 Line Components are - Cohesive - Loosely Coupled - Have well-defined interfaces (abstraction, encapsulation) get. P 1 get. Y() set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) Nice Modular Design! 8
Crosscutting Concern - Example Notify Screen. Manager if a figure element moves Display * Figure Point get. X() Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) Display. Tracking 9
Example: Display Tracking Display. Tracker Display * Figure Point get. X() Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) class Point { void set. X(int x) { Display. Tracker. update. Point(this); this. x = x; class Display. Tracker { static void update. Point(Point p) { this. display(p); . . } static void update. Line(Line l) { this. display(l); . . } class Line { void set. P 1(Point p 1 { Display. Tracker. update. Line(this); this. p 1 = p 1; } } Crosscutting Concern } } 10
Example - Tracing - Design Trace the execution of all operations. . . Tracer Display trace. Entry trace. Exit * Figure Point get. X() Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) Tracing 11
Example - Tracing Tracer Tangling Code class Tracer { static void trace. Entry(String str) { System. out. println(str); } static void trace. Exit(String str) { System. out. println(str); } Display * Figure Point get. X() Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) class Point { void set. X(int x) { Tracer. trace. Entry(“Entry Point. set”); _x = x; Tracer. trace. Exit(“Exit Point. set”); } } Scattered Concern } class Line { void set. P 1(Point p 1 { Tracer. trace. Entry(“Entry Line. set”); _p 1 = p 1; Tracer. trace. Exit(“Exit Line. set”); } } 12
Example – Tracing and Display Tracking Tracer Display trace * Figure Point get. X() Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) Tracing Display Tracker 13
Crosscutting, Scattering and Tangling n Crosscutting n n Scattering n n concern that inherently relates to multiple components. results in scattered concern and tangled code non-functional requirements likely to crosscut Single concern affects multiple modules Tangling n multiple concerns are interleaved in a single module 14
Example of crosscutting concerns n n n n Synchronization Real-time constraints Error-checking Object interaction constraints Memory management Persistency Security Caching Logging Monitoring Testing Domain specific optimization. . . 15
Aspect-Oriented Software Development n n n Provides better separation of concerns by explicitly considering crosscutting concerns (as well) Does this by providing explicit abstractions for representing crosscutting concerns, i. e. aspects and composing these into programs, i. e. aspect weaving or aspect composing. As such AOSD improves modularity and supports quality factors such as n n n maintainability adaptability reusability understandability. . . 16
Basic AOP technologies n Composition Filters n n Aspect. J n n XEROX PARC, US Demeter. J/DJ n n University of Twente, The Netherlands Northeastern University, US Multi-dimensional separation of Concerns/Hyper. J n IBM TJ Watson Research Center, US 17
History of AOP languages Scripts (Francez 81) OO languages Reflection (Smith 81) AI (semantic networks 79) MOP (1985) Sina interface predicates (1988) Law of Demeter (1988) CLOS-MOP Composition Filters (1992) Adaptive programming (1992) Crosscutting aspects (1996) Aspect. J (1997) Composition Filters with superimposition (2001) Aspect. J (2000) http: //trese. cs. utwente. nl 18
Aspect. J n A general purpose AO programming language n n just as Java is a general-purpose OO language unlike examples in ECOOP’ 97 paper n n domain specific languages for each aspect an integrated extension to Java n n n accepts all java programs as input outputs. class files compatible with any JVM integrated with tools 19
Example – Without AOP class Line { private Point _p 1, _p 2; class Tracer { Point get. P 1() { return _p 1; } Point get. P 2() { return _p 2; } static void trace. Entry(String str) { System. out. println(str); } static void trace. Exit(String str) { System. out. println(str); } void set. P 1(Point p 1) { Tracer. trace. Entry(“entry set. P 1”); _p 1 = p 1; Tracer. trace. Exit(“exit set. P 1”); } void set. P 2(Point p 2) { Tracer. trace. Entry(“entry set. P 2”); _p 2 = p 2; Tracer. trace. Exit(“exit set. P 2”); } class Point { } private int _x = 0, _y = 0; int get. X() { return _x; } int get. Y() { return _y; } void set. X(int x) { Tracer. trace. Entry(“entry set. X”); _x = x; Tracer. trace. Exit(“exit set. X”) } void set. Y(int y) { Tracer. trace. Entry(“exit set. Y”); _y = y; Tracer. trace. Exit(“exit set. Y”); Tangling Code Scattered Concern } } 20
Example – With AOP class Line { private Point _p 1, _p 2; aspect Tracing { pointcut traced(): call(* Line. * || call(* Point. *); Point get. P 1() { return _p 1; } Point get. P 2() { return _p 2; } void set. P 1(Point p 1) { _p 1 = p 1; } void set. P 2(Point p 2) { _p 2 = p 2; } before(): traced() { println(“Entering: ” + thisjopinpoint); void println(String str) {<write to appropriate stream>} } class Point } { } private int _x = 0, _y = 0; int get. X() { return _x; } int get. Y() { return _y; } void _x } void _y } } set. X(int x) { = x; set. Y(int y) { = y; Aspect is defined in a separate module Crosscutting is localized No scattering; No tangling Improved modularity 21
Aspect Language Elements n join point (JP) model n certain principled points in program execution such as method calls, field accesses, and object construction n means of identifying JPs n n n picking out join points of interest (predicate) pointcuts: set of join points means of specifying behavior at JPs n n what happens advice declarations 22
Modularizing Crosscutting n n Joinpoints: any well-defined point of execution in a program such as method calls, field accesses, and object construction Pointcut: predicate on joinpoints selecting a collection of joinpoints. Tracer Display * Figure Point get. X() pointcut traced(): call(* Line. *) || call(* Point. *); Figure. Element Line 2 get. P 1 get. Y() set. P 1 set. X(int) set. P 1(Point) set. Y(int) set. P 2(Point) 23
Joinpoints n method call join points n n method reception join points n n when a field is set exception handler execution join point n n when a field is accessed field set joint point n n when the body of code for an actual method executes field get joint point n n when an object receives a message method execution join points n n when a method is called when an exception handler executes object creation join point n when an instance of a class is created 24
Some primitive pointcuts n call(Signature) n n execution(Signature) n n picks out join points that are in code contained in Class. Name within. Code(Signature) n n picks out join points of currently executing objects of class Class. Name within(Class. Name) n n picks out an exception handler of any of the Throwable types of Type. Pattern instance. Of(Class. Name) n n picks out a field set join point based on Signature handles(Type. Pattern) n n picks out a field get join point based on Signature set(Signature) n n picks out a method or constructor execution join point based on Signature get(Signature) n n picks out method or constructor call based on Signature picks out join points within the member defined by methor or constructor (Signature) cflow(pointcut) n picks out all the join points in the control flow of the join points picked out by the pointcut 25
Advice n n Piece of code that attaches to a pointcut and thus injects behavior at all joinpoints selected by that pointcut. example: before (args): pointcut { Body } where before represents a before advice type (see next slide). n Can take parameters with pointcuts 26
Advice Types Advice code executes n before, code is injected before the joinpoint Advice JP before (args): pointcut { Body } n after, code is injected after the joinpoint after (args): pointcut JP Advice { Body } n around, code is injected around (in place of) code from joinpoint Return. Type around (args): pointcut { Body } Advice JP 27
Aspect n n n A modular unit of cross-cutting behavior. Like a class, can have methods, fields, initializers. can be abstract, inherit from classes and abstract aspects and implement interfaces. encapsulates pointcuts and advices can introduce new methods / fields to a class x Aspect. X class. X Aspect. Y class. Y 28
Example - Aspect. J class Line { private Point _p 1, _p 2; Point get. P 1() { return _p 1; } Point get. P 2() { return _p 2; } aspect Tracing { void set. P 1(Point p 1) { _p 1 = p 1; } void set. P 2(Point p 2) { _p 2 = p 2; } pointcut traced(): call(* Line. * || call(* Point. *); before(): traced() { println(“Entering: ” + thisjopinpoint); } class Point { aspect pointcut advice after(): traced() { println(“Exit: ” + thisjopinpoint); private int _x = 0, _y = 0; int get. X() { return _x; } int get. Y() { return _y; } void _x } void _y } set. X(int x) { = x; set. Y(int y) { = y; void println(String str) {<write to appropriate stream>} } 29
Code Weaving n n n Before compile-time (pre-processor) During compile-time After compile-time At load time At run-time 30
Example - Aspect. J aspect Move. Tracking { private static boolean _flag = false; public static boolean test. And. Clear() { boolean result = _flag; _flag = false; return result; } pointcut moves(): receptions(void Line. set. P 1(Point)) || receptions(void Line. set. P 2(Point)); static after(): moves() { _flag = true; } } 31
Demeter. J / DJ Law Of Demeter n Each unit should only have limited knowledge about other units: only about units “closely” related to the current unit. n n “Each unit should only talk to its friends. ” “Don’t talk to strangers. ” Goal: Reduce behavioral dependencies between classes. Loose coupling 32
Applying Lo. D n n n A method must be able to traverse links to obtain its neighbors and must be able to call operations on them. But it should not traverse a second link from the neighbor to a third class. Methods should communicate only with preferred suppliers: n n n immediate parts on this objects passed as arguments to method objects which are directly created in method objects in global variables No other calls allowed ---> Scattering 33
Solution is Adaptive Programming n n n Encapsulate operation into one place thereby avoiding scattering Specify traversal over (graph) structure in a succinct way thereby reducing tangling. Navigation strategy 34
Use of Visitors import edu. neu. ccs. demeter. dj. *; // define strategy String strategy=“from Bus. Route through Bus. Stop to Person” class Bus. Route { // define class graph static Classgraph cg = new Class. Graph(); int print. Count. Waiting. Persons(){ // traversal/visitor weaving //define visitor Visitor v = new Visitor() public void before(Person host){ r++; … } public void start() { r = 0; } … } cg. traverse(this, strategy, v); . . . } 35
- Slides: 35