Chapitre 6
Programmation orientée aspect (AOP)
2I1AC3 : Génie logiciel et Patrons
de conception
Régis Clouard, ENSICAEN - GREYC
« L'homme est le meilleur ordinateur que l'on puisse
embarquer dans un engin spatial...
et le seul qui puisse être produit en masse
par du travail non qualifié. »
Wernher von Braun
06
Chapitre
Introduction
2
■ OOP limitations
● Code redundancy
● Code tangling
▶ mix-up of multiple concerns at one place
● Code scattering
▶ a concern is scattered over many places
● Cross-cutting
▶ a concern than spans multiple units of OO modularity
▶ cannot be implemented without code-tangling and code-
scattering
06
Chapitre
Cross-Cutting
3
Class A Class B Class C
Action1 Action1
Action 2
Action2
Action 1
Action1
■ Example : Functional Integrity Constraint
06
Chapitre
Code-Scattering
4
Class A Class B Class C
call method 1 definition method 1
call method 2
call mthod 2
definition method 2
call method 1
call method 2
■ Example: Data Persistence
06
Chapitre
Solutions
5
■ Respect design principles of Chapter 1 !
■ Aspect oriented Programming
● Gregor Kiczales en 1996 à Xerox PARC
● Inspiration: Artificial Intelligence
▶ Object language
▶ Meta-programming
▶ Reflection
● New programming paradigm
● Software programming history
▶ Procedural paradigm
▶ OOP paradigm
▶ OAP paradigm
■ Separates business concerns from crosscutting concerns
06
Chapitre
Aspect Oriented Programming
6
OOP without aspect OOP with aspect
Class A Class B Class C Class A Class B Class C Aspect
06
Chapitre
Definitions
7
■ Aspect
● Unit of modularity.
● Can contain fields and methods like a regular Java class.
■ Joint Point
● Places where crosscutting concerns can be woven in.
● Ex. method call, field access, object creation.
■ Pointcut
● Declaration that selects join points and collects contexts at that
point (regular expressions)
■ Code Advice
● Code to be executed at a join point.
06
Chapitre
Illustration
8
Object program Aspect program
Class A Class B Class C Aspect
Joint Point Joint Point Joint Point Pointcut1(A1,A2,B1,C2)
A1 B1 C1 Pointcut2(A3,B2,C1,C2)
Joint Point Joint Point Joint Point
A2 B2 C2
Pointcut1
Joint Point Code advice 1
A3
Pointcut1 & Pointcut2
Code advice 2
06
Chapitre
Joint point models
9
Method call
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint point models
10
Exception
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint point models
11
Constructor call
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint Point Models
12
Field access
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Code Advice
13
■ Code advice is a method like construct that expresses the
action to be taken at the join points that are captured by a
pointcut.
■ Before advice
● executes prior to the join point
■ After advice
● executes following the join point
■ Around advice
● In such code advice, the keyword 'proceed' is used to specify
where to insert the join point’s execution
▶ can continue original execution, bypass execution or cause
execution with an altered context
▶ can cause execution of the join point multiple times
06
Chapitre
Weaving
14
■ An OAP application
● Set of classes
● Set of aspects
■ Weaving
● Weaving rules specify how to integrate the final system
06
Chapitre
Types of Weaver
15
■ 1/ Static weaver
● Source to source translation.
Aspect code
Intermediate
Class code Weaver Compiler bytecode
source code
06
Chapitre
Types of Weaver
16
■ 2/ Dynamic weaver
● Bytecode enhancement, first compile source with original
compiler, then weave aspects into class files.
Weaver
Determine if a
code advice must
be executed
Pre-Weaver
Modifier Virtual
Class code modifies the Bytecode
class code Machine
class code
06
Chapitre
AOP Paradigm
17
■ Separation functional / non functional parts
● Functional part: business concerns
▶ eg, Add or delete an employee
● Non functional part: crosscutting concerns
▶ eg, Security and access control
06
Chapitre
Examples
18
■ AspectJ (Open Source)
● Static weaver
● URL
▶ AspectJ
■ http://www.eclipse.org/aspectj
▶ Plug-in Eclipse
■ http://eclipse.org/ajdt
06
Chapitre
Example 1: HelloWorld (before
weaving) 19
Hello.java World.aj
public class Hello { public aspect World {
public static void main(String a[]){
pointcut salutation() : execution
new Hello().sayHello();
(*Hello.sayHello(..));
}
public void sayHello() { after() :salutation() {
System.out.println("Hello!");
} System.out.println("Salut à toi
} aussi");
}
}
06
Chapitre
Example 1: HelloWorld (after
weaving) 20
Hello.java World.aj
public class Hello { public aspect World {
public static void main(String a[]) { pointcut salutation() :execution
(*Hello.sayHello(..));
new Hello().sayHello();
System.out.println("Salut à toi
aussi"); after() :salutation() {
} System.out.println("Salut à toi
aussi");
}
public void sayHello() { }
System.out.println("Hello!");
}
}
06
Chapitre
Example 2: Debug (before
weaving) 21
Debug.aj
Example2.java
public aspect Debug {
public class Exemple2 {
pointcut methodEx2() :
private int _x=0;
execution(*Exemple2.*(..));
public static void main(String args){
for (int i=0;i<10;i++) { pointcut CallIncr() :
Exemple2.increment();
execution(*increment(..));
}
}
pointcut Ensemble() :
public void increment() { methodEx2() & CallIncr();
_x++;
} around() :Ensemble() {
} System.out.println(x);
proceed
System.out.println(x);
}
}
06
Chapitre
Example 2: Debug (after weaving)
22
Example2.java Debug.aj
After weaving Before weaving
public class Exemple2 { public aspect Debug {
private int _x=0; pointcut methodEx2() :
public static void main(String a[]){ execution(*Exemple2.*(..));
for (int i=0;i<10;i++){
System.out.println(_x); pointcut CallIncr() :
Exemple2.increment(); execution(*increment(..));
System.out.println(_x);
} pointcut Ensemble() :
} methodEx2() & CallIncr();
public void increment() {
_x++; around() :Ensemble() {
} System.out.println(_x);
} proceed
System.out.println(_x);
}
}
06
Chapitre
Example 3: Design Pattern
Revisited 23
■ Singleton OOP : Drawbacks
● Use of non classical constructor
● Restrict inheritance
■ Singleton AOP
● Add a joint point cut on the constructor
● An attribute stores the instance reference
● The code advice returns a new instance (1st call) or the attribute
reference
06
Chapitre
Conclusion
24
■ OOP + AOP = OOP
■ AOP is available as extensions of many programming
languages
● Java
● C++
● Php
● C#
● D
● Smalltalk