aspect oriented programming - fenix.tecnico.ulisboa.pt · summary problem aop.. aspect oriented...

107
. . . . . . Summary Problem AOP . . Aspect Oriented Programming Ant´ onio Menezes Leit˜ ao April 22, 2013 Ant´ onio Menezes Leit˜ ao Aspect Oriented Programming

Upload: hathuan

Post on 15-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.

...... Aspect Oriented Programming

Antonio Menezes Leitao

April 22, 2013

Antonio Menezes Leitao Aspect Oriented Programming

Page 2: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

...1 Problem

...2 AOPExampleJoin PointPointcutAdviceAspect

Antonio Menezes Leitao Aspect Oriented Programming

Page 3: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Modularization.Concerns..

......

A software systems satisfies a set of concerns (requirements, usecases, features, data structures, patterns, colaborations,contracts, etc).In a functional decomposition, each concern is encapsulated inone or more functions.In an object-oriented decomposition, each concern is encapsulatedin one or more objects (or classes).It is difficult to simultaneously deal with all the concerns: only themost important ones are considered and they guide the principaldecomposition.The remaining concerns are implemented on top of the principaldecomposition, trying not to disturb it.

Antonio Menezes Leitao Aspect Oriented Programming

Page 4: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Modularization.Cross-cutting Concern..

......

Some concerns crosscut the entire system (affecting all objects, classesor functions):

loggingsecuritysynchronizationmonitoring

.Cross-cutting Concern Implementation..

......

It is hard to find a decomposition that allows modularization ofconcerns that crosscut the entire system.The implementation of a cross-cutting concern becomes spread allover the system.

Antonio Menezes Leitao Aspect Oriented Programming

Page 5: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Example: Apache Tomcat

.XML..

......

Antonio Menezes Leitao Aspect Oriented Programming

Page 6: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Example: Apache Tomcat

.URL..

......

Antonio Menezes Leitao Aspect Oriented Programming

Page 7: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Example: Apache Tomcat

.Logging..

......

Antonio Menezes Leitao Aspect Oriented Programming

Page 8: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP

.. Modularization.Problems caused by Cross-cutting Concerns..

......

Code Tangling: Each module implements one fundamentalconcern and parts of other cross-cutting concerns.Code Scattering: Each cross-cutting concern is implemented byseveral fragments placed in different parts of the system.

.Consequences..

......

Low quality: code tangling facilitates errors.Low traceability: where is the code that implements a givenconcern? Which concern is implemented by this code?Low reusability: each module includes fragments that are notrelated to its task.Low evolvability: of modules that are fragmented and of modulesthat include fragments of other modules.

Antonio Menezes Leitao Aspect Oriented Programming

Page 9: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect-Oriented Programming

.History..

......

Invented at Xerox PARC (1995).Author: Gregor Kiczales (yes, he is the same author of the CLOSMOP).

.Fundamental Idea..

......

Separation of Concerns for the concerns that affect the entiresystem.Concern implementation becomes centralized.Concern implementation includes a description of the places ofthe system that are affected by the concern.

Antonio Menezes Leitao Aspect Oriented Programming

Page 10: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Concepts

.Dynamic Cross-Cutting..

......

Modification of the dynamic behavior of the program.Additional behavior that must be executed in well-defined pointsof the program execution.

.Static Cross-Cutting..

......

Modification of the static structure of the program.Modification of the class hierarchy, addition of methods,implementation of new interfaces, etc.

Antonio Menezes Leitao Aspect Oriented Programming

Page 11: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Concepts

.Join Point..

......A join point is a well-defined point in the execution flow of a program.

.Pointcut..

......A pointcut is a set of join points and values in those points.

.Advice..

......An advice is a fragment of code that is executed at a given join point.

Antonio Menezes Leitao Aspect Oriented Programming

Page 12: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Concepts

.Inter-Type Declaration..

......

An inter-type declaration is a declaration that changes the staticstructure of a program (members of classes and relationships betweenclasses).

.Aspect..

......An (aspect) is a module that groups pointcuts, advices and inter-typedeclarations.

Antonio Menezes Leitao Aspect Oriented Programming

Page 13: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Three Steps

.Decomposition..

......Requirements are decomposed in order to separate modular concernsfrom cross-cutting concerns.

.Implementation..

......Each concern is separately implemented, using OOP (for modularconcerns) or AOP (for cross-cutting concerns).

.Recomposition..

......The aspect weaving tool integrates the cross-cutting concerns with themodular concerns.

Antonio Menezes Leitao Aspect Oriented Programming

Page 14: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Coordinates..

......

class XY {int x;int y;

XY(int x, int y) {this.x = x;this.y = y;

}

int getX() {return x;

}

int getY() {return y;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 15: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Shapes..

......class Shape {}

Antonio Menezes Leitao Aspect Oriented Programming

Page 16: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Lines..

......

class Line extends Shape {XY origin;XY end;

XY getOrigin() {return origin;

}

XY getEnd() {return end;

}

void setOrigin(XY newOrigin) {origin = newOrigin;

}

void setEnd(XY newEnd) {end = newEnd;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 17: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Circles..

......

class Circle extends Shape {XY center;int radius;

XY getCenter() {return center;

}

int getRadius() {return radius;

}

void setCenter(XY newCenter) {center = newCenter;

}

void setRadius(int newRadius) {radius = newRadius;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 18: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Problem..

......Every time a shape is modified, we need to redraw.

.Solution..

......

...1 Identify all points where shapes are modified (pointcut).

...2 Execute a code fragment that causes redrawing, e.g.,Screen.redraw() (advice).

Antonio Menezes Leitao Aspect Oriented Programming

Page 19: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Join Point

.Join Points..

......

method callconstructor callmethod executionconstructor executionadvice execution (advice over advice)field readingfield writingexception handlingstatic initialization (of classes)dinamic initialization (of instances)

Antonio Menezes Leitao Aspect Oriented Programming

Page 20: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcut.Pointcut..

......A pointcut selects points in the execution of a program that aredescribed by join points.

.Circle Changes..

......pointcut changeCircle():

call(void Circle.setCenter(XY)) ||call(void Circle.setRadius(int));

.Syntax..

......

Join points can be combined using operators:conjunction &&disjunction ||negation !

Antonio Menezes Leitao Aspect Oriented Programming

Page 21: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcut

.Syntax..

......

call(void Circle.setCenter(XY))Calls to the method named setCenter, defined in class Circle,with return type void and single parameter of type XY.call(void Circle.setCenter(XY)) ||call(void Circle.setRadius(int))Calls to the method named setCenter, defined in class Circle,with return type void and single parameter of type XY or to themethod named setRadius, defined in class Circle, with returntype void and single parameter of type int.

Antonio Menezes Leitao Aspect Oriented Programming

Page 22: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Advice

.Advice..

......

An advice changes the program behavior in a pointcut.beforeafter returning, after throwing, afteraround

.Redraw the screen after each circle modification..

......after() returning: changeCircle() {

Screen.redraw();}

.What about instances of Line?..

......We need more flexible pointcuts.

Antonio Menezes Leitao Aspect Oriented Programming

Page 23: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcut

.Syntax..

......

call(void *.setCenter(XY))Calls to the method named setCenter, defined in any class, withreturn type void and single parameter of type XY.call(* *.set*(XY))Calls to any method whose name begins with set, defined in anyclass, with any return type and single parameter of type XY.call(* *.set*(..))Calls to any method whose name begins with set, defined in anyclass, with any return type and any number of parameters.

Antonio Menezes Leitao Aspect Oriented Programming

Page 24: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcut

.Crosscutting..

......

name-based Explicit enumeration of method signatures (ex:call(void Circle.setCenter(XY))).

property-based Implicit enumeration based on method properties (ex:call(* *.set*(..))).

* Any number of characters except the dot... Any number of characters, including any

number of dots.+ Any subtype of the given type.

.Shape Setters........pointcut changeShape():

call(void Shape+.set*(..));

Antonio Menezes Leitao Aspect Oriented Programming

Page 25: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect

.Aspect..

......An aspect modularizes a cross-cutting concern, grouping pointcuts,advices and inter-type declarations.

.Shape Setters..

......

aspect ScreenUpdate {pointcut changeShape():

call(void Shape+.set*(..));

after() returning: changeShape() {Screen.redraw();

}}

.Problem..

......How to redraw just the modified shape?

Antonio Menezes Leitao Aspect Oriented Programming

Page 26: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect

.Aspect..

......An aspect modularizes a cross-cutting concern, grouping pointcuts,advices and inter-type declarations.

.Shape Setters..

......

aspect ScreenUpdate {pointcut changeShape():

call(void Shape+.set*(..));

after() returning: changeShape() {Screen.redraw();

}}

.Problem..

......How to redraw just the modified shape?

Antonio Menezes Leitao Aspect Oriented Programming

Page 27: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Execution Context..

......

An advice can have parameters that become bound to parts of theexecution context that are exposed by the pointcut (using the joinpoints this, target and args).

.Shape Setters..

......

aspect ScreenUpdate {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

after(Shape s) returning: changeShape(s) {Screen.redraw(s);

}}

.Problem..

......What about multiple screens?

Antonio Menezes Leitao Aspect Oriented Programming

Page 28: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Execution Context..

......

An advice can have parameters that become bound to parts of theexecution context that are exposed by the pointcut (using the joinpoints this, target and args).

.Shape Setters..

......

aspect ScreenUpdate {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

after(Shape s) returning: changeShape(s) {Screen.redraw(s);

}}

.Problem..

......What about multiple screens?

Antonio Menezes Leitao Aspect Oriented Programming

Page 29: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ.Inter-type Declarations........

Allow adding fields and methods to already defined classes.

.Shape Setters..

......

aspect ShapeListening {

private Vector<Screen> Shape.listeners = new Vector<Screen>();

public void Shape.addListener(Screen sc) {listeners.add(sc);

}

pointcut changeShape(Shape s):call(void Shape+.set*(..)) && target(s);

after(Shape sh): changeShape(sh) {for(Screen sc : sh.listeners) {

sc.redraw(sh);}

}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 30: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ.Inter-type Declarations........

Allow adding fields and methods to already defined classes.

.Shape Setters..

......

aspect ShapeListening {private Vector<Screen> Shape.listeners = new Vector<Screen>();

public void Shape.addListener(Screen sc) {listeners.add(sc);

}

pointcut changeShape(Shape s):call(void Shape+.set*(..)) && target(s);

after(Shape sh): changeShape(sh) {for(Screen sc : sh.listeners) {

sc.redraw(sh);}

}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 31: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ.Inter-type Declarations........

Allow adding fields and methods to already defined classes.

.Shape Setters..

......

aspect ShapeListening {private Vector<Screen> Shape.listeners = new Vector<Screen>();

public void Shape.addListener(Screen sc) {listeners.add(sc);

}

pointcut changeShape(Shape s):call(void Shape+.set*(..)) && target(s);

after(Shape sh): changeShape(sh) {

for(Screen sc : sh.listeners) {sc.redraw(sh);

}

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 32: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ.Inter-type Declarations........

Allow adding fields and methods to already defined classes.

.Shape Setters..

......

aspect ShapeListening {private Vector<Screen> Shape.listeners = new Vector<Screen>();

public void Shape.addListener(Screen sc) {listeners.add(sc);

}

pointcut changeShape(Shape s):call(void Shape+.set*(..)) && target(s);

after(Shape sh): changeShape(sh) {for(Screen sc : sh.listeners) {

sc.redraw(sh);}

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 33: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 34: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));

Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 35: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();

c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 36: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);

c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 37: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 38: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321

Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 39: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654

Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 40: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 41: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. AspectJ

.Example..

......

Circle c = new Circle(new XY(10,20), 5);Line l = new Line(new XY(5,7), new XY(9,3));Screen sc0 = new Screen();Screen sc1 = new Screen();c.addListener(sc0);c.addListener(sc1);l.addListener(sc0);c.setCenter(new XY(1,2));l.setOrigin(new XY(3,4));

.Execution..

......

Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Redrawing Line@456 screen Screen@321

.Problem..

......What about multiple advices?

Antonio Menezes Leitao Aspect Oriented Programming

Page 42: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {

pointcut changeShape(Shape s):call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}

}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?

Antonio Menezes Leitao Aspect Oriented Programming

Page 43: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}

}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?

Antonio Menezes Leitao Aspect Oriented Programming

Page 44: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?

Antonio Menezes Leitao Aspect Oriented Programming

Page 45: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654

Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?

Antonio Menezes Leitao Aspect Oriented Programming

Page 46: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?

Antonio Menezes Leitao Aspect Oriented Programming

Page 47: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s);

}}

.Execution..

......

Changing Circle@123Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456Redrawing Line@456 screen Screen@321

.Problem..

......Which call caused the trace?Antonio Menezes Leitao Aspect Oriented Programming

Page 48: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Trace.Trace..

......

public aspect TraceChange {pointcut changeShape(Shape s):

call(void Shape+.set*(..)) && target(s);

before(Shape s): changeShape(s) {System.err.println("Changing " + s .+ " in " + thisJoinPoint);

}}

.Execution..

......

Changing Circle@123 in call(void Circle.setCenter(XY))Redrawing Circle@123 screen Screen@321Redrawing Circle@123 screen Screen@654Changing Line@456 in call(void Line.setOrigin(XY))Redrawing Line@456 screen Screen@321

.thisJoinPoint..

......Is bound to an object that describes the join point.Antonio Menezes Leitao Aspect Oriented Programming

Page 49: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Profiling

.New Operations for Circle..

......

class Circle extends Shape {...

void translate(XY offset) {setCenter(new XY(center.getX() + offset.getX(),

center.getY() + offset.getY()));}

void scale(int f) {setRadius(f * radius);translate(new XY(center.getX() * f,

center.getY() * f));}

}

.Challenge..

......How many shape changes are needed to implement a change of scale?

Antonio Menezes Leitao Aspect Oriented Programming

Page 50: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Profiling

.Aspect ScaleProfile..

......

aspect ScaleProfile {static int changeCount = 0;

pointcut changesMadeFromScale():call(void Shape+.set*(..)) && cflow(call(void *.scale(int)));

after(): changesMadeFromScale() {changeCount++;

}}

.cflow(call(void *.scale(int)))..

......Any join point in the control flow of the join pointcall(void *.scale(int)).

Antonio Menezes Leitao Aspect Oriented Programming

Page 51: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Profiling

.Example..

......

Circle c = new Circle(new XY(10,20), 5);System.err.println("(1) Shape changes: " + ScaleProfile.changeCount);c.setCenter(new XY(1,2));c.setRadius(1);System.err.println("(2) Shape changes: " + ScaleProfile.changeCount);c.scale(2);System.err.println("(3) Shape changes: " + ScaleProfile.changeCount);

.Execution..

......

(1) Shape changes: 0Changing Circle@123 in call(void Circle.setCenter(XY))Changing Circle@123 in call(void Circle.setRadius(int))(2) Shape changes: 0Changing Circle@123 in call(void Circle.setRadius(int))Changing Circle@123 in call(void Circle.setCenter(XY))(3) Shape changes: 2

Antonio Menezes Leitao Aspect Oriented Programming

Page 52: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing.Example..

......

public class Page {

Header header;Menu leftMenu;Content content;Footer footer;

Page() {header = new Header();leftMenu = new Menu();content = new Content();footer = new Footer();createBorder();

}

void createBorder() {new Line(new XY(0,0), new XY(10,0));new Line(new XY(10,0), new XY(10,10));new Line(new XY(10,10), new XY(0,10));new Line(new XY(0,10), new XY(0,0));

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 53: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

class Header {

Header() {createLeftHeader();createHeaderSeparator(2);createMiddleHeader();createHeaderSeparator(8);createRightHeader();

}

void createHeaderSeparator(int x) {new Line(new XY(x,8), new XY(x,10));

}

void createLeftHeader() { }

void createMiddleHeader() { }

void createRightHeader() { }}

Antonio Menezes Leitao Aspect Oriented Programming

Page 54: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing.Example..

......

public class ColoredPage extends Page {String color;

ColoredPage(String color) {super();this.color = color;

}

public static void main(String[] args) {ColoredPage cp = new ColoredPage("blue");

}}

.Problem..

......

How can we use the color of the ColoredPage as the color ofeach created Shape?It is not practical to pass the color as argument to each and everymethod that might create Shapes.

Antonio Menezes Leitao Aspect Oriented Programming

Page 55: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Aspect..

......

public aspect ColorizingAspect {

String Shape.color;

pointcut ShapeConstruction():call(Shape+.new(..));

pointcut ColoredPageConstructionCFlow(String color):cflow(call(ColoredPage.new(String))&& args(color));

after (String color) returning (Shape s):ShapeConstruction() &&ColoredPageConstructionCFlow(color) {

System.err.println("Changing color for " + s);s.color = color;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 56: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Execution..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@13e205f

.Problem..

......What about changes after Shape construction?

Antonio Menezes Leitao Aspect Oriented Programming

Page 57: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

public class ColoredPage extends Page {String color;

ColoredPage(String color) {super();this.color = color;

}

void fillContent() {.new Circle(new XY(1,1), 4);

}

public static void main(String[] args) {ColoredPage cp = new ColoredPage("blue");System.err.println("About to fill content");cp.fillContent();

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 58: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

public class ColoredPage extends Page {String color;

ColoredPage(String color) {super();this.color = color;

}

void fillContent() {new Circle(new XY(1,1), 4);

}

public static void main(String[] args) {ColoredPage cp = new ColoredPage("blue");.System.err.println("About to fill content");.cp.fillContent();

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 59: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Execution..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@13e205fAbout to fill content

.Problem..

......The created Circle didn’t get the correct color?

.Solution..

......Another pointcut and another advice.

Antonio Menezes Leitao Aspect Oriented Programming

Page 60: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Aspect..

......

public aspect ColorizingAspect {

String Shape.color;

pointcut ShapeConstruction(): ...

pointcut ColoredPageConstructionCFlow(String color): ...

after (String color) returning (Shape s): ...

pointcut ColoredPageCFlow(ColoredPage cp):cflow(call(* *(..)) && target(cp));

after (ColoredPage cp) returning (Shape s):ShapeConstruction() &&ColoredPageCFlow(cp) {

System.err.println("Changing color for " + s);s.color = cp.color;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 61: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@16a55faChanging color for Line@32c41aChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@e89b94Changing color for Line@13e205fChanging color for Line@13e205fAbout to fill contentChanging color for Circle@5740bb

.Problem..

......The last four cases of Line are duplicated!

Antonio Menezes Leitao Aspect Oriented Programming

Page 62: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@16a55faChanging color for Line@32c41aChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@e89b94Changing color for Line@13e205fChanging color for Line@13e205fAbout to fill contentChanging color for Circle@5740bb

.Problem..

......The last four cases of Line are duplicated!

Antonio Menezes Leitao Aspect Oriented Programming

Page 63: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Aspect..

......

public aspect ColorizingAspect {

String Shape.color;

pointcut ShapeConstruction(): ...

pointcut ColoredPageConstructionCFlow(String color): ...

after (String color) returning (Shape s): ...

pointcut ColoredPageCFlow(ColoredPage cp):cflow(call(* *(..)) && target(cp));

after (ColoredPage cp) returning (Shape s):ShapeConstruction() &&ColoredPageCFlow(cp) {

System.err.println("Changing color for " + s);s.color = cp.color;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 64: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Aspect..

......

public aspect ColorizingAspect {

String Shape.color;

pointcut ShapeConstruction(): ...

pointcut ColoredPageConstructionCFlow(String color): ...

after (String color) returning (Shape s): ...

pointcut ColoredPageCFlow(ColoredPage cp):cflow(call(* *(..)) && target(cp));

after (ColoredPage cp) returning (Shape s):ShapeConstruction() &&ColoredPageCFlow(cp) &&! cflow(call(ColoredPage.new(String))) {

System.err.println("Changing color for " + s);s.color = cp.color;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 65: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@16a55faChanging color for Line@32c41aChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@e89b94Changing color for Line@13e205fChanging color for Line@13e205fAbout to fill contentChanging color for Circle@5740bb

Antonio Menezes Leitao Aspect Oriented Programming

Page 66: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Aspect: Context Passing

.Example..

......

Changing color for Line@2e7263Changing color for Line@a59698Changing color for Line@16a55faChanging color for Line@32c41aChanging color for Line@e89b94Changing color for Line@13e205fAbout to fill contentChanging color for Circle@5740bb

Antonio Menezes Leitao Aspect Oriented Programming

Page 67: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Objects..

......Represent all execution points where an object is an executor or areceptor or an argument or an exception.

.Example..

......

this(Foo)All execution points where this is an instance of class Foo.target(Foo)All execution points where the receptor of a method call is aninstance of class Foo.args(String,..,int)All execution points where the first argument is a String and thelast argument is an int.

Antonio Menezes Leitao Aspect Oriented Programming

Page 68: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts.Context Capture........Identical to the previous ones but these pointcuts can capture context..Example..

......

this(x)All execution points where this is an instance of the type ofparameter x, binding that parameter to that instance.target(x)All execution points where the receptor of a method call is aninstance of the type of the parameter x, binding that parameterto the receptor.args(String,..,x)All execution points where the first argument is a String and thelast argument is of the type of the parameter x, binding thatparameter to the argument.

Antonio Menezes Leitao Aspect Oriented Programming

Page 69: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Method or Constructor Call..

......Represents the execution point after the argument evaluation butbefore execution of the called method or constructor..Example..

......

call(public void Foo.bar(Baz))All calls to a public method bar defined in class Foo that has aparameter of type Baz and that returns void.call(Foo+.new(..))All calls to constructors of class Foo or of any of its subclasses.

Antonio Menezes Leitao Aspect Oriented Programming

Page 70: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Method or Construtor Execution..

......Represents the execution of a method or of a constructor.

.Example..

......

execution(public void Foo.bar(Baz))All executions of the public method bar defined in class Foo thathas a parameter of type Baz and that returns void.execution(Foo+.new(..))All executions of constructors of class Foo or of any of itssubclasses.

Antonio Menezes Leitao Aspect Oriented Programming

Page 71: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Field Access..

......Represents field reading or writing.The assigned value can be captured with the pointcut args.

.Example..

......

set(int Foo.bar)All writings to field bar of an instance of Foo.get(PrintStream System.out)All readings from field out of class System.

Antonio Menezes Leitao Aspect Oriented Programming

Page 72: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Field Access..

......

aspect LimitedBarFieldChange {

static final int MAX_BAR_CHANGE = 100;

before(Foo foo, int newBar):set(int Foo.bar) &&target(foo) &&args(newBar) {

if (Math.abs(newBar - foo.bar) > MAX_BAR_CHANGE) {throw new RuntimeException();

}}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 73: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Exception Handling..

......

Represents the execution of an exception handler for a given type ofexception.The exception can be captured with the pointcut args.

.Example..

......

handler(IOException+)All executions of catch block that handles an IOException orany of its subclasses.handler(Foo*)All executions of catch block that handles exceptions of anyclasses whose name begins with Foo.

Antonio Menezes Leitao Aspect Oriented Programming

Page 74: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Exception Handling..

......

aspect DumpFooException {

before(FooException e):handler(FooException) && args(e) {

e.printStackTrace();}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 75: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts.Class and Instance Initialization........Represents the execution of static initializers or instance initializers..Example..

......

staticinitialization(Foo)Execution of the static block of class Foo.preinitialization(Foo.new(..)))All executions of an instance initialization, from the beginning ofthe execution of the class constructor until the beginning of theexecution of the superclass constructor.initialization(Foo.new(..))All executions of an instance initialization, from the return of theexecution of the superclass constructor until the return of theexecution of the class constructor.

Antonio Menezes Leitao Aspect Oriented Programming

Page 76: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Lexical Structure..

......

Represents all execution points within the lexical structure of a class(within(TypePattern)) or method(withincode(MethodOrConstructorSignature)), including internalclasses (annonymous or not).

.Example..

......

within(Foo)All execution points in the lexical scope of class Foo.withincode(* Foo.bar(..))All execution points in the lexical scope of any method namedbar of class Foo.

Antonio Menezes Leitao Aspect Oriented Programming

Page 77: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Pointcuts

.Control Flow..

......Represents all execution points in the control flow of a given executionpoint.

.Example..

......

cflow(call(* Foo.bar(..)))All execution points in the control flow of a call to any methodnamed bar of class Foo, including the method call itself.cflowbelow(call(* Foo.bar(..)))All execution points in the control flow of a call to any methodnamed bar of class Foo, excluding the method call itself.

Antonio Menezes Leitao Aspect Oriented Programming

Page 78: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Problem..

......

We want to be able to undo the execution of Java programs.We want to be able to create checkpoints representing theexecution state of a Java program.We want to be able to force a program to go back in time until itreaches a given checkpoint.

Antonio Menezes Leitao Aspect Oriented Programming

Page 79: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.A person has a name, an age, and a friend - Java..

......

class Person {String name;int age;Person friend;

public String toString() {return "[" + name + "," + age +

((friend == null) ? "" : " with friend " + friend) +"]";

}}

.Yes, I know:..

......

Missing constructor.Missing getters and setters.They are not relevant for the example.

Antonio Menezes Leitao Aspect Oriented Programming

Page 80: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.Paul, John and Mary..

......

Person p0 = new Person() {{ name = "John"; age = 21; }};Person p1 = new Person() {{ name = "Paul"; age = 23; }};//Paul has friend named Johnp1.friend = p0;println(p1);//[Paul,23 with friend [John,21]]

int state0 = History.currentState();//32 years later, John changed is name to 'Louis'//and got a friendp0.age = 53;p1.age = 55;p0.name = "Louis";p0.friend = new Person() {{ name = "Mary"; age = 19; }};println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]int state1 = History.currentState();//15 years later, John (hum, I mean 'Louis') diedp1.age = 70;p1.friend = null;println(p1);//[Paul,70]//Let's go back in timeHistory.restoreState(state1);println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]//and againHistory.restoreState(state0);println(p1);//[Paul,23 with friend [John,21]]

Antonio Menezes Leitao Aspect Oriented Programming

Page 81: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.Paul, John and Mary..

......

Person p0 = new Person() {{ name = "John"; age = 21; }};Person p1 = new Person() {{ name = "Paul"; age = 23; }};//Paul has friend named Johnp1.friend = p0;println(p1);//[Paul,23 with friend [John,21]]int state0 = History.currentState();//32 years later, John changed is name to 'Louis'//and got a friendp0.age = 53;p1.age = 55;p0.name = "Louis";p0.friend = new Person() {{ name = "Mary"; age = 19; }};println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]

int state1 = History.currentState();//15 years later, John (hum, I mean 'Louis') diedp1.age = 70;p1.friend = null;println(p1);//[Paul,70]//Let's go back in timeHistory.restoreState(state1);println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]//and againHistory.restoreState(state0);println(p1);//[Paul,23 with friend [John,21]]

Antonio Menezes Leitao Aspect Oriented Programming

Page 82: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.Paul, John and Mary..

......

Person p0 = new Person() {{ name = "John"; age = 21; }};Person p1 = new Person() {{ name = "Paul"; age = 23; }};//Paul has friend named Johnp1.friend = p0;println(p1);//[Paul,23 with friend [John,21]]int state0 = History.currentState();//32 years later, John changed is name to 'Louis'//and got a friendp0.age = 53;p1.age = 55;p0.name = "Louis";p0.friend = new Person() {{ name = "Mary"; age = 19; }};println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]int state1 = History.currentState();//15 years later, John (hum, I mean 'Louis') diedp1.age = 70;p1.friend = null;println(p1);//[Paul,70]

//Let's go back in timeHistory.restoreState(state1);println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]//and againHistory.restoreState(state0);println(p1);//[Paul,23 with friend [John,21]]

Antonio Menezes Leitao Aspect Oriented Programming

Page 83: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.Paul, John and Mary..

......

Person p0 = new Person() {{ name = "John"; age = 21; }};Person p1 = new Person() {{ name = "Paul"; age = 23; }};//Paul has friend named Johnp1.friend = p0;println(p1);//[Paul,23 with friend [John,21]]int state0 = History.currentState();//32 years later, John changed is name to 'Louis'//and got a friendp0.age = 53;p1.age = 55;p0.name = "Louis";p0.friend = new Person() {{ name = "Mary"; age = 19; }};println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]int state1 = History.currentState();//15 years later, John (hum, I mean 'Louis') diedp1.age = 70;p1.friend = null;println(p1);//[Paul,70]//Let's go back in timeHistory.restoreState(state1);println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]

//and againHistory.restoreState(state0);println(p1);//[Paul,23 with friend [John,21]]

Antonio Menezes Leitao Aspect Oriented Programming

Page 84: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.Paul, John and Mary..

......

Person p0 = new Person() {{ name = "John"; age = 21; }};Person p1 = new Person() {{ name = "Paul"; age = 23; }};//Paul has friend named Johnp1.friend = p0;println(p1);//[Paul,23 with friend [John,21]]int state0 = History.currentState();//32 years later, John changed is name to 'Louis'//and got a friendp0.age = 53;p1.age = 55;p0.name = "Louis";p0.friend = new Person() {{ name = "Mary"; age = 19; }};println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]int state1 = History.currentState();//15 years later, John (hum, I mean 'Louis') diedp1.age = 70;p1.friend = null;println(p1);//[Paul,70]//Let's go back in timeHistory.restoreState(state1);println(p1);//[Paul,55 with friend [Louis,53 with friend [Mary,19]]]//and againHistory.restoreState(state0);println(p1);//[Paul,23 with friend [John,21]]

Antonio Menezes Leitao Aspect Oriented Programming

Page 85: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Save Program State..

......

import java.util.Stack;import java.lang.reflect.*;

public class History {

static Stack<ObjectFieldValue> undoTrail =new Stack<ObjectFieldValue>();

public static void storePrevious(Object object,String className,String fieldName,Object value) {

undoTrail.push(new ObjectFieldValue(object,className,fieldName,value));

}

. . .}

Antonio Menezes Leitao Aspect Oriented Programming

Page 86: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Save Program State..

......

import java.util.Stack;import java.lang.reflect.*;

public class History {

. . .

public static int currentState() {return undoTrail.size();

}

public static void restoreState(int state) {//undo all actions until size == statewhile (undoTrail.size() != state) {

undoTrail.pop().restore();}

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 87: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Save Program State..

......

class ObjectFieldValue {Object object;String className;String fieldName;Object value;

ObjectFieldValue(Object object,String className,String fieldName,Object value) {

this.object = object;this.className = className;this.fieldName = fieldName;this.value = value;

}

. . .}

Antonio Menezes Leitao Aspect Oriented Programming

Page 88: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Save Program State..

......

class ObjectFieldValue {

. . .

void restore() {try {

Field field =Class.forName(className).getDeclaredField(fieldName);

field.setAccessible(true);field.set(object, value);

} catch (ClassNotFoundException e) {throw new RuntimeException(e);

} catch (NoSuchFieldException e) {throw new RuntimeException(e);

} catch (IllegalAccessException e) {throw new RuntimeException(e);

}}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 89: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Javassist..

......

import javassist.*;import javassist.expr.*;import java.io.*;import java.lang.reflect.*;

public class Undoable {

public static void main(String[] args) throws . . . {if (args.length < 1) {

. . .} else {

Translator translator = new UndoableTranslator();ClassPool pool = ClassPool.getDefault();Loader classLoader = new Loader();classLoader.addTranslator(pool, translator);String[] restArgs = new String[args.length - 1];System.arraycopy(args, 1, restArgs, 0, restArgs.length);classLoader.run(args[0], restArgs);

}}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 90: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Javassist..

......

class UndoableTranslator implements Translator {

public void start(ClassPool pool)throws NotFoundException, CannotCompileException {

}

public void onLoad(ClassPool pool, String className)throws NotFoundException, CannotCompileException {CtClass ctClass = pool.get(className);makeUndoable(ctClass);

}

void makeUndoable(CtClass ctClass) {. . .

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 91: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.Javassist..

......

void makeUndoable(CtClass ctClass)throws NotFoundException, CannotCompileException {final String template =

"{" +" History.storePrevious($0, \"%s\",\"%s\", ($w)$0.%s);" +" $0.%s = $1;" +"}";

for (CtMethod ctMethod : ctClass.getDeclaredMethods()) {ctMethod.instrument(new ExprEditor() {

public void edit(FieldAccess fa)throws CannotCompileException {if (fa.isWriter()) {

String name = fa.getFieldName();fa.replace(String.format(template,

fa.getClassName(),name, name, name));

}}

});}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 92: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Undoable Programs

.AspectJ..

......

public aspect Undoable {

pointcut fieldWrite():set(* *.*) && within(UndoTest);

before(): fieldWrite() {Object obj = thisJoinPoint.getTarget();FieldSignature sig =

(FieldSignature)thisJoinPoint.getSignature();Field field = sig.getField();Class cls = field.getDeclaringClass();try {

History.storePrevious(obj, cls.getName(),field.getName(), field.get(obj));

} catch (IllegalAccessException e) {throw new RuntimeException(e);

}}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 93: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {

Object around(HttpRequest request) :execution(* HttpServlet.do*(..)) && args(request,..) {

long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 94: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 95: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {

long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 96: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();

Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 97: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();Object retValue = proceed(request);

long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 98: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();

monitorAgent.record(request.getRequestURI(),endTime - startTime);

return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 99: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);

return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 100: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Example

.Profiling..

......Measure the time taken to process requests.

.advice around..

......

public aspect ServletPerformanceMonitor {Object around(HttpRequest request) :

execution(* HttpServlet.do*(..)) && args(request,..) {long startTime = System.nanoTime();Object retValue = proceed(request);long endTime = System.nanoTime();monitorAgent.record(request.getRequestURI(),

endTime - startTime);return retValue;

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 101: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. (Bad) Example

.Factorial (wrong)..

......

public class MyMath {

int fact (int n) {return -1;

}

public static void main(String[] args) {MyMath math = new MyMath();System.out.println(math.fact(10));

}}

Antonio Menezes Leitao Aspect Oriented Programming

Page 102: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. (Bad) Example

.Correcting Aspect..

......

aspect AroundFactorial {

int around (MyMath mm, int n):call(int fact(int)) &&target(mm) &&args(n) {

if (n == 0) {return 1;

} else {return n * mm.fact(n - 1);

}}

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 103: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Abstract Aspects

.AuthenticationAspect..

......

abstract aspect AuthenticationAspect {

abstract pointcut operationsNeeddingAuthentication();

before() : operationsNeeddingAuthentication() {

Authenticator.authenticate();}

}

.DatabaseAuthenticationAspect..

......

aspect DatabaseAuthenticationAspect extends AuthenticationAspect {

pointcut operationsNeeddingAuthentication():call(* DatabaseServer.connect());

}

Antonio Menezes Leitao Aspect Oriented Programming

Page 104: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.. Attention

.Slot Access and Arithmetic Operation?..

......

class Foo {void main(String[] args) {

System.out.println(Foo.class);System.out.println(args[0] + args[1]);

}}

.Looks can be Deceiving..

......

Foo.class is implemented via Class.forName protected by anexception handler that catches ClassNotFoundException.The operador + applied to Strings is implemented via calls toStringBuffer.append.

Antonio Menezes Leitao Aspect Oriented Programming

Page 105: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

.AOP Problems..

......

Aspects might have excessive applicability. Java Annotationsmight provide a solution.Changes in the code might prevent the application of an aspect.IDEs (such as Eclipse) can detect those situations and inform theprogrammer.Programmers do not fully understand a program fragment unlessthey see all the injected advices. IDEs (such as Eclipse) allow easynavigation between injected advices.Some secury concerns: what if we inject return true in amethod that checks a password?

Antonio Menezes Leitao Aspect Oriented Programming

Page 106: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

G. Kiczales.Aspect-oriented programming.ACM Computing Surveys, 28(4es), December 1996.

G. Kiczales.AspectJ(tm): Aspect-oriented programming in Java.Lecture Notes in Computer Science, 2591:1, 2003.Gregor Kiczales.Aspect-oriented programming: A position paper from the XeroxPARC aspect-oriented programming project.In Max Muehlhauser, editor, Special Issues in Object-OrientedProgramming. Dpunkt Verlag, 1996.Gregor Kiczales and Erik Hilsdale.Aspect-oriented programming.In ESEC / SIGSOFT FSE, page 313, 2001.

Cristina Videira Lopes and Gregor Kiczales.Recent developments in AspectJ.

Antonio Menezes Leitao Aspect Oriented Programming

Page 107: Aspect Oriented Programming - fenix.tecnico.ulisboa.pt · Summary Problem AOP.. Aspect Oriented Programming Ant´onioMenezesLeit ˜ao April22,2013 Ant´onio Menezes Leit ˜ao Aspect

. . . . . .

Summary Problem AOP Example Join Point Pointcut Advice Aspect

In Cristina Lopes, Gregor Kiczales, Bedir Tekinerdogan,Wolfgang De Meuter, and Marco Meijers, editors, Workshop onAspect Oriented Programming (ECOOP 1998), June 1998.

Antonio Menezes Leitao Aspect Oriented Programming