1 R R R A Brief Introduction to !Aspect-Oriented Programming"R R R Historical View Of Languages"• Procedural language"• Functional language"• Object-Oriented language"2 R R R Acknowledgements"• Zhenxiao Yang"• Gregor Kiczales"• Eclipse website for AspectJ(www.eclipse.org/aspectj)"R R R Procedural Language"• Also termed imperative language"• Describe"– An explicit sequence of steps to follow to produce a result"• Examples: Basic, Pascal, C, Fortran"3 R R R Functional Language"• Describe everything as a function (e.g., data, operations)"• (+ 3 4); (add (prod 4 5) 3)"• Examples"– LISP, Scheme, ML, Haskell"R R R Logical Language"• Also termed declarative language"• Establish causal relationships between terms"– Conclusion :- Conditions"– Read as: If Conditions then Conclusion"• Examples: Prolog, Parlog"4 R R R Object-Oriented Programming"• Describe "– A set of user-defined objects "– And communications among them to produce a (user-defined) result"• Basic features"– Encapsulation"– Inheritance"– Polymorphism "R R R OOP (contʼd)"• Example languages"– First OOP language: SIMULA-67 (1970)"– Smalltalk, C++, Java"– Many other:"• Ada, Object Pascal, Objective C, DRAGOON, BETA, Emerald, POOL, Eiffel, Self, Oblog, ESP, POLKA, Loops, Perl, VB"• Are OOP languages procedural? "5 R R R We Need More"• Major advantage of OOP"– Modular structure"• Potential problems with OOP"– Issues distributed in different modules result in tangled code."– Example: error logging, failure handling, performance optimizations"• Potential result: Tangled Code"– Change in the way logging is done affects many classes"R R R Example of Tangled Code"Red indicates the error-logging code6 R R R Untangling the Tangled Code"• Constraint:"– Want to preserve the benefits of OOP (encapsulation, modularity, inheritance, etc.)"• Potential Solution:"– Aspect-Oriented Programming"R R R Basic Concepts in AOP"• Crosscutting: straddle across functional and hierarchical boundaries "• Aspect: "– Property cannot be cleanly encapsulated into a single procedure"– Tend to affect performance or semantics of components in systematic ways "7 R R R AOP: Languages"• Components:!– Component program"– Aspect definition"– Aspect Weaver"• Constructs:!– Join point: execution point in component program for integrating aspects"– Pointcuts: refers to collection of join points and values of variables at those points"– Advice: method-like constructs that define additional behavior at join points"– Aspects: “units of modular cross-cutting implementation”"• Pointcuts"• Advice"• Regular (Java) code "R R R Pictorial Representation"Weaver Executable Compiler Compiler Executable Hook points for cross-cutting concern Aspect8 R R R AspectJ by Example"http://www.eclipse.org/aspectj R R R AspectJ by Example (contʼd)"• Define pointcuts"• Define advice"• Introduction"9 R R R Name-Based Pointcuts"• Identify specific join point within a program flow"pointcut call(void Point.setX(int))!– Looks for specific signature • Identify a collection of multiple signatures according to Boolean operators (&&, ||, !) • pointcut call(void Point.setX(int)) ||!call(void Point.setY(int))!http://www.eclipse.org/aspectj R R R Name-based Pointcut"• Namingamorecomplicatedpointcutpointcut move(): ! call(void FigureElement.setXY(int,int)) ! || call(void Point.setX(int)) ! || call(void Point.setY(int)) ! || call(void Line.setP1(Point)) ! || call(void Line.setP2(Point)); !• Perform action if any of these pointcuts are encountered"http://www.eclipse.org/aspectj10 R R R Property-based Pointcuts"• Identify pointcuts by properties of methods (e.g., use wildcards *)"– Identify by name prefix: pointcut call(void Figure.make*(..))!Pick out join point whose method starts with “make” – Identify by visibility pointcut call(public * Figure.* (..))!Source: www.eclipse.org http://www.eclipse.org/aspectj R R R Advice"• Advice links together a pointcut with a segment of code (to be run at each join point)"• 3 types of advice:"– before: runs as join point is reached, before transferring control at join point"– after: after execution of join point (before control is returned to caller)"after():move(){System.out.println(“Afigureelementmoved.”);}– around: runs as the join point is reached, maintains control of program."http://www.eclipse.org/aspectj11 R R R Advice and Context"• Pointcuts:"– Pick out join points"– Also expose part of execution context at join point"– Values from context can be used in advice body."R R R Advice"• Exampleofcontextfrompointcutpointcut setXY(FigureElement fe, int x, int y):! call(void FigureElement.setXY(x, y))!!! && target (fe)! && args (x,y); !– Naming FigureElement as fe, identify parameters (arguments) x and y!• Context used in Advice"after(FigureElementfe,intx,inty)returning:setXY(fe,x,y){System.out.println(fe+“movedto”+x,+“,”+y);}http://www.eclipse.org/aspectj12 R R R Advice and pointcut "after(FigureElement fe, int x, int y) returning: !! !call(void FigureElement.setXY(int, int)) !&& target(fe) !! !&& args(x, y) { !! System.out.println(fe + " moved to (" + x + ", " + y + ")");}!http://www.eclipse.org/aspectj R R R Aspect Definition Example"aspect FigureLog{! pointcut setXY(FigureElement fe, int x, int y): ! calls(void fe.setXY(x, y)); ! after(FigureElement fe, int x, int y): !! !setXY(fe, x, y){ ! System.out.println(fe + " moved to (" ! + x + ", " + y + ")."); ! } !} !http://www.eclipse.org/aspectj13 R R R Introduction: Inter-type Declarations"• Declarations that cut across classes and their hierarchies"– Declare members across multiple classes"– Change inheritance relationship"• Operates statically at compile-time. "R R R Introduction (contʼd)"aspect
View Full Document